1 /*
2  * Copyright 2013-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.jmx;
16
17 import java.lang.management.ManagementFactory;
18 import java.util.List;
19
20 import javax.management.InstanceAlreadyExistsException;
21 import javax.management.InstanceNotFoundException;
22 import javax.management.MBeanRegistrationException;
23 import javax.management.MBeanServer;
24 import javax.management.MBeanServerFactory;
25 import javax.management.MalformedObjectNameException;
26 import javax.management.NotCompliantMBeanException;
27 import javax.management.ObjectName;
28
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  * MBean related utilities.
33  */

34 public enum MBeans {
35     ;
36     /**
37      * Registers the given MBean under the given object name to the first
38      * registered MBean server, or the platform MBean server if there is no
39      * explicitly registered MBean server.
40      * 
41      * @return true if the registration succeeded, or false if an MBean already
42      *         exists under the given object name.
43      * @throws MBeanRegistrationException
44      *             The preRegister (MBeanRegistration interface) method of the
45      *             MBean has thrown an exception. The MBean will not be
46      *             registered.
47      */

48     public static <T> boolean registerMBean(String objectName, T mbean)
49             throws MBeanRegistrationException {
50         MBeanServer server = getMBeanServer();
51         try {
52             server.registerMBean(mbean, new ObjectName(objectName));
53         } catch (MalformedObjectNameException e) {
54             throw new IllegalArgumentException(e);
55         } catch (NotCompliantMBeanException e) {
56             throw new IllegalArgumentException(e);
57         } catch (InstanceAlreadyExistsException e) {
58             LogFactory.getLog(MBeans.class).debug(
59                 "Failed to register mbean " + objectName, e);
60             return false;
61         }
62         return true;
63     }
64
65     /**
66      * Unregisters the MBean under the given object name to the first MBean
67      * server, or the platform MBean server if there is no explicitly registered
68      * MBean server.
69      * 
70      * @return true if the unregistration succeeded, or false if the MBean
71      *         doesn't exist under the given object name.
72      * @throws MBeanRegistrationException
73      *             Wraps exceptions thrown by the preRegister(), preDeregister()
74      *             methods of the MBeanRegistration interface.
75      */

76     public static <T> boolean unregisterMBean(String objectName)
77             throws MBeanRegistrationException {
78         MBeanServer server = getMBeanServer();
79         try {
80             server.unregisterMBean(new ObjectName(objectName));
81         } catch (MalformedObjectNameException e) {
82             throw new IllegalArgumentException(e);
83         } catch (InstanceNotFoundException e) {
84             LogFactory.getLog(MBeans.class).debug(
85                     "Failed to unregister mbean " + objectName, e);
86             return false;
87         }
88         return true;
89     }
90
91     /**
92      * Returns true if an MBean identified by the specified object name is
93      * already registered with the first MBean server, or the platform MBean
94      * server if there is no explicitly registered MBean server; false
95      * otherwise.
96      */

97     public static boolean isRegistered(String objectName) {
98         MBeanServer server = getMBeanServer();
99         try {
100             return server.isRegistered(new ObjectName(objectName));
101         } catch (MalformedObjectNameException e) {
102             throw new IllegalArgumentException(e);
103         }
104     }
105
106     /**
107      * Returns the first registered MBean server, or the platform MBean server
108      * if there is none.
109      */

110     public static MBeanServer getMBeanServer() {
111         List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
112         MBeanServer server = servers.size() > 0
113             ? servers.get(0)
114             : ManagementFactory.getPlatformMBeanServer();
115         return server;
116     }
117 }
118