-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMBeansMonitor.java
More file actions
359 lines (331 loc) · 14 KB
/
Copy pathMBeansMonitor.java
File metadata and controls
359 lines (331 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright 2008-2014 by Emeric Vernat
*
* This file is part of Java Melody.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit.sysinfo;
import javax.management.Attribute;
import javax.management.InstanceNotFoundException;
import javax.management.JMException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* This code was extracted from JavaMelody and refactored.
*
* @author Emeric Vernat
* @author James Moger
*/
public final class MBeansMonitor {
private static final String JAVA_LANG_MBEAN_DESCRIPTION = "Information on the management interface of the MBean";
private static final Comparator<MBeanNode> NODE_COMPARATOR = (o1, o2) -> o1.getName() != null ? o1.getName().compareTo(o2.getName()) : 0;
private static final Comparator<MBeanNode.MBeanAttribute> ATTRIBUTE_COMPARATOR = (o1, o2) -> o1.getName().compareTo(o2.getName());
private final MBeanServer mbeanServer;
public MBeansMonitor() {
this(ManagementFactory.getPlatformMBeanServer());
}
private MBeansMonitor(MBeanServer mbeanServer) {
super();
this.mbeanServer = mbeanServer;
}
Object getAttribute(ObjectName name, String attribute) throws JMException {
return mbeanServer.getAttribute(name, attribute);
}
public List<MBeanNode> getAllMBeanNodes() throws JMException {
initJRockitMBeansIfNeeded();
List<MBeanNode> result = new ArrayList<>();
MBeanServer platformMBeanServer = mbeanServer;
MBeanNode platformNode = new MBeanNode("");
MBeansMonitor platformMBeansMonitor = new MBeansMonitor();
platformNode.getChildren().addAll(platformMBeansMonitor.getMBeanNodes());
result.add(platformNode);
for (MBeanServer mbeanServer : getMBeanServers()) {
if (!mbeanServer.equals(platformMBeanServer)) {
MBeanNode node = new MBeanNode(mbeanServer.getDefaultDomain());
MBeansMonitor mbeans = new MBeansMonitor(mbeanServer);
node.getChildren().addAll(mbeans.getMBeanNodes());
result.add(node);
}
}
return result;
}
private void initJRockitMBeansIfNeeded() {
if (System.getProperty("java.vendor").contains("BEA")) {
try {
// http://blogs.oracle.com/hirt/jrockit/
try {
mbeanServer.getMBeanInfo(new ObjectName("bea.jrockit.management:type=JRockitConsole"));
} catch (InstanceNotFoundException e1) {
mbeanServer.createMBean("bea.jrockit.management.JRockitConsole", null);
}
} catch (JMException e) {
throw new IllegalStateException(e);
}
}
}
private List<MBeanNode> getMBeanNodes() throws JMException {
List<MBeanNode> result = new ArrayList<>();
Set<ObjectName> names = mbeanServer.queryNames(null, null);
for (ObjectName name : names) {
String domain = name.getDomain();
if ("jboss.deployment".equals(domain)) {
continue;
}
MBeanNode domainNode = getMBeanNodeFromList(result, domain);
if (domainNode == null) {
domainNode = new MBeanNode(domain);
result.add(domainNode);
}
String keyPropertyListString = name.getKeyPropertyListString();
String firstPropertyValue;
int indexOf = keyPropertyListString.indexOf('=');
if (indexOf == -1) {
firstPropertyValue = null;
} else {
firstPropertyValue = name.getKeyProperty(keyPropertyListString
.substring(0, indexOf));
}
if ("Servlet".equals(firstPropertyValue) && "jonas".equals(domain)) {
continue;
}
MBeanNode firstPropertyNode = getMBeanNodeFromList(domainNode.getChildren(),
firstPropertyValue);
if (firstPropertyNode == null) {
firstPropertyNode = new MBeanNode(firstPropertyValue);
domainNode.getChildren().add(firstPropertyNode);
}
final MBeanNode mbean = getMBeanNode(name);
firstPropertyNode.getChildren().add(mbean);
}
sortMBeanNodes(result);
return result;
}
private void sortMBeanNodes(List<MBeanNode> nodes) {
if (nodes.size() > 1) {
Collections.sort(nodes, NODE_COMPARATOR);
}
for (MBeanNode node : nodes) {
List<MBeanNode> children = node.getChildren();
if (children != null) {
sortMBeanNodes(children);
}
List<MBeanNode.MBeanAttribute> attributes = node.getAttributes();
if (attributes != null && attributes.size() > 1) {
Collections.sort(attributes, ATTRIBUTE_COMPARATOR);
}
}
}
private MBeanNode getMBeanNodeFromList(List<MBeanNode> list, String name) {
for (MBeanNode node : list) {
if (node.getName().equals(name)) {
return node;
}
}
return null;
}
private MBeanNode getMBeanNode(ObjectName name) throws JMException {
String mbeanName = name.toString();
MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(name);
String description = formatDescription(mbeanInfo.getDescription());
MBeanAttributeInfo[] attributeInfos = mbeanInfo.getAttributes();
List<MBeanNode.MBeanAttribute> attributes = getAttributes(name, attributeInfos);
return new MBeanNode(mbeanName, description, attributes);
}
private List<MBeanNode.MBeanAttribute> getAttributes(ObjectName name, MBeanAttributeInfo[] attributeInfos) {
List<String> attributeNames = new ArrayList<>(attributeInfos.length);
for (MBeanAttributeInfo attribute : attributeInfos) {
if (attribute.isReadable() && !"password".equalsIgnoreCase(attribute.getName())) {
attributeNames.add(attribute.getName());
}
}
String[] attributeNamesArray = attributeNames.toArray(new String[attributeNames.size()]);
List<MBeanNode.MBeanAttribute> result = new ArrayList<>();
try {
List<Object> attributes = mbeanServer.getAttributes(name, attributeNamesArray);
for (Object object : attributes) {
Attribute attribute = (Attribute) object;
Object value = convertValueIfNeeded(attribute.getValue());
String attributeDescription = getAttributeDescription(attribute.getName(), attributeInfos);
String formattedAttributeValue = formatAttributeValue(value);
MBeanNode.MBeanAttribute mbeanAttribute = new MBeanNode.MBeanAttribute(attribute.getName(),
attributeDescription, formattedAttributeValue);
result.add(mbeanAttribute);
}
} catch (Exception e) {
MBeanNode.MBeanAttribute mbeanAttribute = new MBeanNode.MBeanAttribute("exception", null, e.toString());
result.add(mbeanAttribute);
}
return result;
}
private String formatAttributeValue(Object attributeValue) {
try {
if (attributeValue instanceof List) {
StringBuilder sb = new StringBuilder();
sb.append('[');
boolean first = true;
for (Object value : (List<?>) attributeValue) {
if (first) {
first = false;
} else {
sb.append(",\n");
}
sb.append(String.valueOf(value));
}
sb.append(']');
return sb.toString();
}
return String.valueOf(attributeValue);
} catch (Exception e) {
return e.toString();
}
}
private String formatDescription(String description) {
if (description == null || JAVA_LANG_MBEAN_DESCRIPTION.equals(description)) {
return null;
}
int indexOf = description.indexOf(" ");
if (indexOf != -1) {
StringBuilder sb = new StringBuilder(description);
while (indexOf != -1) {
sb.deleteCharAt(indexOf);
indexOf = sb.indexOf(" ");
}
return sb.toString();
}
return description;
}
private Object convertValueIfNeeded(Object value) {
if (value instanceof CompositeData) {
CompositeData data = (CompositeData) value;
Map<String, Object> values = new TreeMap<>();
for (String key : data.getCompositeType().keySet()) {
values.put(key, convertValueIfNeeded(data.get(key)));
}
return values;
} else if (value instanceof CompositeData[]) {
List<Object> list = new ArrayList<>();
for (CompositeData data : (CompositeData[]) value) {
list.add(convertValueIfNeeded(data));
}
return list;
} else if (value instanceof Object[]) {
return Arrays.asList((Object[]) value);
} else if (value instanceof TabularData) {
TabularData tabularData = (TabularData) value;
return convertValueIfNeeded(tabularData.values());
} else if (value instanceof Collection) {
List<Object> list = new ArrayList<>();
for (Object data : (Collection<?>) value) {
list.add(convertValueIfNeeded(data));
}
return list;
}
return convertJRockitValueIfNeeded(value);
}
private Object convertJRockitValueIfNeeded(Object value) {
if (value instanceof double[]) {
List<Double> list = new ArrayList<>();
for (double data : (double[]) value) {
list.add(data);
}
return list;
} else if (value instanceof int[]) {
List<Integer> list = new ArrayList<>();
for (int data : (int[]) value) {
list.add(data);
}
return list;
}
return value;
}
private List<Object> getConvertedAttributes(List<String> mbeanAttributes) {
initJRockitMBeansIfNeeded();
List<Object> result = new ArrayList<>();
List<MBeanServer> mBeanServers = getMBeanServers();
for (String mbeansAttribute : mbeanAttributes) {
int lastIndexOfPoint = mbeansAttribute.lastIndexOf('.');
if (lastIndexOfPoint <= 0) {
throw new IllegalArgumentException(mbeansAttribute);
}
String name = mbeansAttribute.substring(0, lastIndexOfPoint);
String attribute = mbeansAttribute.substring(lastIndexOfPoint + 1);
if ("password".equalsIgnoreCase(attribute)) {
throw new IllegalArgumentException(name + '.' + attribute);
}
InstanceNotFoundException instanceNotFoundException = null;
for (MBeanServer mbeanServer : mBeanServers) {
try {
MBeansMonitor mbeans = new MBeansMonitor(mbeanServer);
Object jmxValue = mbeans.convertValueIfNeeded(mbeans.getAttribute(new ObjectName(name), attribute));
result.add(jmxValue);
instanceNotFoundException = null;
break;
} catch (InstanceNotFoundException e) {
instanceNotFoundException = e;
} catch (JMException e) {
throw new IllegalArgumentException(name + '.' + attribute, e);
}
}
if (instanceNotFoundException != null) {
throw new IllegalArgumentException(name + '.' + attribute, instanceNotFoundException);
}
}
return result;
}
String getConvertedAttributes(String jmxValueParameter) {
List<String> mbeanAttributes = Arrays.asList(jmxValueParameter.split("[|]"));
List<Object> jmxValues = getConvertedAttributes(mbeanAttributes);
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Object jmxValue : jmxValues) {
if (first) {
first = false;
} else {
sb.append('|');
}
sb.append(jmxValue);
}
return sb.toString();
}
private String getAttributeDescription(String name, MBeanAttributeInfo[] attributeInfos) {
for (MBeanAttributeInfo attributeInfo : attributeInfos) {
if (name.equals(attributeInfo.getName())) {
String attributeDescription = formatDescription(attributeInfo.getDescription());
if (attributeDescription == null || attributeDescription.isEmpty() || name.equals(attributeDescription)) {
return null;
}
return attributeDescription;
}
}
return null;
}
private List<MBeanServer> getMBeanServers() {
return MBeanServerFactory.findMBeanServer(null);
}
}