View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools;
12  
13  import javax.management.AttributeNotFoundException;
14  import javax.management.InstanceNotFoundException;
15  import javax.management.IntrospectionException;
16  import javax.management.MBeanAttributeInfo;
17  import javax.management.MBeanException;
18  import javax.management.MBeanInfo;
19  import javax.management.MBeanServer;
20  import javax.management.ObjectName;
21  import javax.management.ReflectionException;
22  import javax.management.RuntimeOperationsException;
23  import javax.management.openmbean.CompositeData;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   * The Class JmxTools.
30   */
31  public final class JmxTools {
32  
33    /** The Constant logger. */
34    private static final Logger logger = LoggerFactory.getLogger(JmxTools.class);
35  
36    /**
37     * Prevent Instantiation.
38     */
39    private JmxTools() {
40      // Prevent Instantiation
41    }
42  
43    /**
44     * Gets the attribute. All exceptions default to null.
45     *
46     * @param mbeanServer the mbean server
47     * @param objectName the object name
48     * @param attributeName the attribute name
49     *
50     * @return the attribute
51     */
52    public static Object getAttribute(MBeanServer mbeanServer, ObjectName objectName,
53        String attributeName) {
54      try {
55        return mbeanServer.getAttribute(objectName, attributeName);
56      } catch (AttributeNotFoundException e) {
57        logger.error("MBean Object '{}' does not have '{}' attribute", objectName, attributeName);
58        logger.trace("", e);
59      } catch (RuntimeOperationsException e) {
60        logger.error("MBean Object '{}' or Attribute '{}' are null", objectName, attributeName);
61        logger.trace("", e);
62      } catch (InstanceNotFoundException e) {
63        logger.error("MBean Object '{}' not registered", objectName);
64        logger.trace("", e);
65      } catch (MBeanException | ReflectionException e) {
66        logger.error("MBean Object '{}' not accessible", objectName);
67        logger.trace("", e);
68      }
69      return null;
70    }
71  
72    /**
73     * Invoke mbean server method. All exceptions default to null.
74     *
75     * @param mbeanServer the mbean server
76     * @param objectName the object name
77     * @param method the method
78     * @param parameters the parameters
79     * @param signatures the signatures
80     *
81     * @return the object
82     */
83    public static Object invoke(MBeanServer mbeanServer, ObjectName objectName, String method,
84        Object[] parameters, String[] signatures) {
85      try {
86        return mbeanServer.invoke(objectName, method, parameters, signatures);
87      } catch (InstanceNotFoundException e) {
88        logger.error("MBean Object '{}' not registered", objectName);
89        logger.trace("", e);
90      } catch (MBeanException | ReflectionException e) {
91        logger.error("MBean Object '{}' not accessible", objectName);
92        logger.trace("", e);
93      }
94      return null;
95    }
96  
97    /**
98     * Gets the long attribute. Default value used if null or any exceptions.
99     *
100    * @param mbeanServer the mbean server
101    * @param objectName the object name
102    * @param attributeName the attribute name
103    * @param defaultValue the default value
104    *
105    * @return the long attr
106    */
107   public static long getLongAttr(MBeanServer mbeanServer, ObjectName objectName,
108       String attributeName, long defaultValue) {
109     Object object = JmxTools.getAttribute(mbeanServer, objectName, attributeName);
110     return object == null ? defaultValue : (Long) object;
111   }
112 
113   /**
114    * Gets the long attribute. Default value '0' if null or any exceptions.
115    *
116    * @param compositeData the composite data
117    * @param name the name
118    *
119    * @return the long attribute
120    */
121   public static long getLongAttr(CompositeData compositeData, String name) {
122     Object object = compositeData.get(name);
123     return object instanceof Long ? (Long) object : 0;
124   }
125 
126   /**
127    * Gets the long attribute. Default value '0' if null or any exceptions.
128    *
129    * @param mbeanServer the mbean server
130    * @param objectName the object name
131    * @param attributeName the attribute name
132    *
133    * @return the long attribute
134    */
135   public static long getLongAttr(MBeanServer mbeanServer, ObjectName objectName,
136       String attributeName) {
137     Object object = JmxTools.getAttribute(mbeanServer, objectName, attributeName);
138     return object == null ? 0 : (Long) object;
139   }
140 
141   /**
142    * Gets the int attribute. Default value '0' if null or any exceptions.
143    *
144    * @param mbeanServer the mbean server
145    * @param objectName the object name
146    * @param attributeName the attribute name
147    *
148    * @return the int attribute
149    */
150   public static int getIntAttr(MBeanServer mbeanServer, ObjectName objectName,
151       String attributeName) {
152     Object object = JmxTools.getAttribute(mbeanServer, objectName, attributeName);
153     return object == null ? 0 : (Integer) object;
154   }
155 
156   /**
157    * Gets the int attribute. Default to default value if not found.
158    *
159    * @param compositeData the composite data
160    * @param name the name
161    * @param defaultValue the default value
162    *
163    * @return the int attribute
164    */
165   public static int getIntAttr(CompositeData compositeData, String name, int defaultValue) {
166     Object object = compositeData.get(name);
167     return object instanceof Integer ? (Integer) object : defaultValue;
168   }
169 
170   /**
171    * Gets the string attribute. All exceptions default to null.
172    *
173    * @param mbeanServer the mbean server
174    * @param objectName the object name
175    * @param attributeName the attribute name
176    *
177    * @return the string attribute
178    */
179   public static String getStringAttr(MBeanServer mbeanServer, ObjectName objectName,
180       String attributeName) {
181     Object object = JmxTools.getAttribute(mbeanServer, objectName, attributeName);
182     return object == null ? null : object.toString();
183   }
184 
185   /**
186    * Gets the string attribute.
187    *
188    * @param compositeData the composite data
189    * @param name the name
190    *
191    * @return the string attribute
192    */
193   public static String getStringAttr(CompositeData compositeData, String name) {
194     Object object = compositeData.get(name);
195     return object == null ? null : object.toString();
196   }
197 
198   /**
199    * Gets the boolean attribute. All exceptions default to false.
200    *
201    * @param mbeanServer the mbean server
202    * @param objectName the object name
203    * @param attributeName the attribute name
204    *
205    * @return the string attribute
206    */
207   public static boolean getBooleanAttr(MBeanServer mbeanServer, ObjectName objectName,
208       String attributeName) {
209     Object object = JmxTools.getAttribute(mbeanServer, objectName, attributeName);
210     return object instanceof Boolean && (Boolean) object;
211   }
212 
213   /**
214    * Gets the boolean attribute.
215    *
216    * @param compositeData the composite data
217    * @param name the name
218    *
219    * @return the boolean attribute
220    */
221   public static boolean getBooleanAttr(CompositeData compositeData, String name) {
222     Object object = compositeData.get(name);
223     return object instanceof Boolean && (Boolean) object;
224   }
225 
226   /**
227    * Checks for attribute. All exceptions default to false.
228    *
229    * @param server the server
230    * @param mbean the mbean
231    * @param attributeName the attribute name
232    *
233    * @return true, if successful
234    */
235   public static boolean hasAttribute(MBeanServer server, ObjectName mbean, String attributeName) {
236     try {
237       MBeanInfo info = server.getMBeanInfo(mbean);
238       for (MBeanAttributeInfo attributeInfo : info.getAttributes()) {
239         if (attributeInfo.getName().equals(attributeName)) {
240           return true;
241         }
242       }
243     } catch (InstanceNotFoundException e) {
244       logger.error("MBean Object '{}' not registered", mbean);
245       logger.trace("", e);
246     } catch (IntrospectionException | ReflectionException e) {
247       logger.error("MBean Object '{}' not accessible", mbean);
248       logger.trace("", e);
249     }
250     return false;
251   }
252 
253 }