获取 mbean 属性值树

我使用 JMX 从服务器获取 mbeans 值(weblogic JMS 配置)

不同的属性有不同的返回类型:

Ljavax.management.ObjectName;
javax.management.ObjectName
Ljava.lang.String;

对于每个返回值列表的属性,我在我的代码中循环 for/if/else。

最后是非常嵌套的代码。有更好的办法吗?


慕码人2483693
浏览 98回答 1
1回答

不负相思意

我自己做。它对我有用public static void parser(String javaxExcept, String LjavaxExcept, HashMap < String, Object > [] atrs, ObjectName ObjUrl, int ng, ObjectName[] origObjUrl) throws Exception {&nbsp; &nbsp; if (ng > 20) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Possibly loop detected");&nbsp; &nbsp; &nbsp; &nbsp; for (int t = 0; t < origObjUrl.length; t++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(origObjUrl[t]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.exit(1);&nbsp; &nbsp; }&nbsp; &nbsp; origObjUrl[ng] = ObjUrl;&nbsp; &nbsp; MBeanAttributeInfo[] attributes = (connection.getMBeanInfo(ObjUrl)).getAttributes();&nbsp; &nbsp; atrs[ng].put("ObjUrl", ObjUrl.toString());&nbsp; &nbsp; for (MBeanAttributeInfo attribute: attributes) {&nbsp; &nbsp; &nbsp; &nbsp; if (attribute.getType().equals("[Ljava.lang.String;")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] Ljava = (String[]) connection.getAttribute(ObjUrl, attribute.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Ljava.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), "null");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int p = 0; p < Ljava.length; p++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), Ljava[p]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else if (attribute.getName().matches(".*(Password|KeyStorePass|PassPhrase|JNDIPropertiesCredential|ClearText|EncryptedValue).*")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), "clear or encrypt text");&nbsp; &nbsp; &nbsp; &nbsp; } else if (attribute.getType().matches("java.util.Properties")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map < String, String > propmap = (Map < String, String > ) connection.getAttribute(ObjUrl, attribute.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (attribute.getName().matches("BootProperties")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; propmap.put("password", "{AES} encrypt text");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; propmap.put("username", "{AES} encrypt text"); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), propmap);&nbsp; &nbsp; &nbsp; &nbsp; } else if (attribute.getType().equals("javax.management.ObjectName") && attribute.getName().matches(javaxExcept) && (connection.getAttribute(ObjUrl, attribute.getName())) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), (connection.getAttribute(ObjUrl, attribute.getName()).toString()));&nbsp; &nbsp; &nbsp; &nbsp; } else if (attribute.getType().matches("java.lang.String|java.lang.Integer|java.lang.Boolean")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), (connection.getAttribute(ObjUrl, attribute.getName())));&nbsp; &nbsp; &nbsp; &nbsp; } else if (attribute.getType().equals("javax.management.ObjectName") && (connection.getAttribute(ObjUrl, attribute.getName())) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), attribute.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ng = ng + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng] = new HashMap < String, Object > ();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjUrl = (ObjectName) connection.getAttribute(ObjUrl, attribute.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parser(javaxExcept, LjavaxExcept, atrs, ObjUrl, ng, origObjUrl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng - 1].put(attribute.getName(), atrs[ng]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ng = ng - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjUrl = origObjUrl[ng];&nbsp; &nbsp; &nbsp; &nbsp; } else if (attribute.getType().equals("[Ljavax.management.ObjectName;") && attribute.getName().matches(LjavaxExcept)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectName[] Ljavax = (ObjectName[]) connection.getAttribute(ObjUrl, attribute.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), attribute.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] Ljavaxarray = new String[Ljavax.length];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < Ljavax.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ljavaxarray[j] = Ljavax[j].toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), Ljavaxarray);&nbsp; &nbsp; &nbsp; &nbsp; } else if (attribute.getType().equals("javax.management.openmbean.ArrayType")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CompositeData[] compdata = (CompositeData[]) connection.getAttribute(ObjUrl, attribute.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] kvcompdata = new String[compdata.length];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int l = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (CompositeData kv: compdata) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kvcompdata[l] = kv.get("DisplayName").toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l = l + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), kvcompdata);&nbsp; &nbsp; &nbsp; &nbsp; } else if (attribute.getType().equals("[Ljavax.management.ObjectName;")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectName[] Ljavax = (ObjectName[]) connection.getAttribute(ObjUrl, attribute.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), attribute.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashMap < String, Object > Ljavaxmap = new HashMap < String, Object > ();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < Ljavax.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ng = ng + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng] = new HashMap < String, Object > ();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjUrl = Ljavax[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parser(javaxExcept, LjavaxExcept, atrs, ObjUrl, ng, origObjUrl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean success = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String Ljavaxaname = new String();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ljavaxaname = (connection.getAttribute(Ljavax[j], "Name")).toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (AttributeNotFoundException e) {}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (success) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ljavaxaname = (connection.getAttribute(Ljavax[j], "Name")).toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ljavaxaname = (connection.getAttribute(Ljavax[j], "Key")).toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ljavaxmap.put(Ljavaxaname, atrs[ng]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ng = ng - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), Ljavaxmap);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjUrl = origObjUrl[ng];&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (connection.getAttribute(ObjUrl, attribute.getName()) == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), "null");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atrs[ng].put(attribute.getName(), (connection.getAttribute(ObjUrl, attribute.getName()).toString()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java