1
2
3
4
5
6
7
8
9
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
30
31 public final class JmxTools {
32
33
34 private static final Logger logger = LoggerFactory.getLogger(JmxTools.class);
35
36
37
38
39 private JmxTools() {
40
41 }
42
43
44
45
46
47
48
49
50
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
74
75
76
77
78
79
80
81
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
99
100
101
102
103
104
105
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
115
116
117
118
119
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
128
129
130
131
132
133
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
143
144
145
146
147
148
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
158
159
160
161
162
163
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
172
173
174
175
176
177
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
187
188
189
190
191
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
200
201
202
203
204
205
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
215
216
217
218
219
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
228
229
230
231
232
233
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 }