java - 获取 mbeans 属性值树
问题描述
我使用 JMX 从服务器获取 mbeans 值(weblogic JMS 配置)
不同的属性有不同的返回类型:
Ljavax.management.ObjectName;
javax.management.ObjectName
Ljava.lang.String;
对于每个返回值列表的属性,我会在代码中循环 for/if/else。
最后是非常嵌套的代码。有没有更好的办法?
//JMS modules
public static void getJMSmodulleRuntimes() throws Exception {
ObjectName domain = getJMSserverRuntimes();
String DomainName = (String)connection.getAttribute(domain, "Name");
ObjectName[] JMSModules = (ObjectName[]) connection.getAttribute(domain,"JMSSystemResources");
for (int i = 0; i < JMSModules.length; i++){
String JMSMonudelsName = (String)connection.getAttribute(JMSModules[i],"Name");
ObjectName JMSSystemResources = new ObjectName("com.bea:Name=" + JMSMonudelsName+",Location="+DomainName+",Type=JMSSystemResource");
MBeanAttributeInfo[] JMSSystemResourcesBeans = (connection.getMBeanInfo(JMSSystemResources)).getAttributes();
System.out.println(" -");
for(MBeanAttributeInfo JMSSystemResourcesBeansAttribute:JMSSystemResourcesBeans) {
if (JMSSystemResourcesBeansAttribute.getName().equals("JMSResource"))
{
ObjectName JMSResourceURL = (ObjectName)connection.getAttribute(JMSSystemResources,JMSSystemResourcesBeansAttribute.getName());
MBeanAttributeInfo[] JMSResourceParams = (connection.getMBeanInfo(JMSResourceURL)).getAttributes();
System.out.println(" JMSResourceParams:");
for(MBeanAttributeInfo JMSResourceParamsBeansAttribute:JMSResourceParams) {
if (JMSResourceParamsBeansAttribute.getType().equals("[Ljavax.management.ObjectName;"))
{
ObjectName[] JMSResourceParamsList = (ObjectName[])connection.getAttribute(JMSResourceURL,JMSResourceParamsBeansAttribute.getName());
if (JMSResourceParamsList.length == 0)
{
System.out.println(" " + JMSResourceParamsBeansAttribute.getName() + ": Null");
}
else
{
System.out.println(" " + JMSResourceParamsBeansAttribute.getName() + ": " );
for (int j = 0; j < JMSResourceParamsList.length; j++) {
// String AttributeObject = (String)connection.getAttribute(JMSResourceParamsList[j],"Name");
System.out.println(" - "); // + AttributeObject+":"
MBeanAttributeInfo[] JMSResourceParamsListObjects = (connection.getMBeanInfo(JMSResourceParamsList[j])).getAttributes();
for(MBeanAttributeInfo JMSResourceParamsListObjectsAttribute:JMSResourceParamsListObjects) {
if (JMSResourceParamsListObjectsAttribute.getName().matches("Password|PasswordEncrypted|JNDIPropertiesCredential"))
{
System.out.println(" "+JMSResourceParamsListObjectsAttribute.getName()+": (clear or encrypt text)");
}
else if (JMSResourceParamsListObjectsAttribute.getType().equals("[Ljavax.management.ObjectName;"))
{
ObjectName[] JMSResourceParamsListL2 = (ObjectName[])connection.getAttribute(JMSResourceParamsList[j],JMSResourceParamsListObjectsAttribute.getName());
System.out.println(" " + JMSResourceParamsListObjectsAttribute.getName() + ": " );
//System.out.println(" - ");
for (int k = 0; k < JMSResourceParamsListL2.length; k++) {
MBeanAttributeInfo[] JMSResourceParamsListObjectsL2 = (connection.getMBeanInfo(JMSResourceParamsListL2[k])).getAttributes();
System.out.println(" - ");
for(MBeanAttributeInfo JMSResourceParamsListObjectsAttributeL2:JMSResourceParamsListObjectsL2) {
if (JMSResourceParamsListObjectsAttributeL2.getName().matches("Password|PasswordEncrypted|JNDIPropertiesCredential"))
System.out.println(" "+JMSResourceParamsListObjectsAttributeL2.getName()+": (clear or encrypt text)");
else
System.out.println(" " + JMSResourceParamsListObjectsAttributeL2.getName() + ": " + (connection.getAttribute(JMSResourceParamsListL2[k],JMSResourceParamsListObjectsAttributeL2.getName())));
}
}
}
else if (JMSResourceParamsListObjectsAttribute.getType().equals("javax.management.ObjectName") && (connection.getAttribute(JMSResourceParamsList[j],JMSResourceParamsListObjectsAttribute.getName())) != null)
{
System.out.println(" " + JMSResourceParamsListObjectsAttribute.getName() + ":");
ObjectName javaxObjectNameURL = (ObjectName)connection.getAttribute(JMSResourceParamsList[j],JMSResourceParamsListObjectsAttribute.getName());
MBeanAttributeInfo[] javaxObjectNameURLAttributes = (connection.getMBeanInfo(javaxObjectNameURL)).getAttributes();
for(MBeanAttributeInfo javaxObjectNameURLAttributesInfo:javaxObjectNameURLAttributes) {
if (javaxObjectNameURLAttributesInfo.getName().equals("ErrorDestination") && (connection.getAttribute(javaxObjectNameURL,javaxObjectNameURLAttributesInfo.getName())) != null)
{
ObjectName objnames = (ObjectName) connection.getAttribute(javaxObjectNameURL,javaxObjectNameURLAttributesInfo.getName());
String objname = (String)connection.getAttribute(objnames,"Name");
System.out.println(" " + javaxObjectNameURLAttributesInfo.getName() + ": " + objname);
}
else
{
System.out.println(" " + javaxObjectNameURLAttributesInfo.getName() + ": "+(connection.getAttribute(javaxObjectNameURL,javaxObjectNameURLAttributesInfo.getName())));
}
}
// String objname = (String)connection.getAttribute(objnameurl,"Name");
}
else if (JMSResourceParamsListObjectsAttribute.getType().equals("[Ljava.lang.String;"))
{
String[] tagnames = (String[])connection.getAttribute(JMSResourceParamsList[j],JMSResourceParamsListObjectsAttribute.getName());
if (tagnames.length == 0) {
System.out.println(" " + JMSResourceParamsListObjectsAttribute.getName()+": Null");
}
else
{
System.out.println(" " + JMSResourceParamsListObjectsAttribute.getName()+":");
for (int p = 0; p < tagnames.length; p++){
System.out.println(" - " + tagnames[p]);
}
}
}
else
System.out.println(" " + JMSResourceParamsListObjectsAttribute.getName()+": " + (connection.getAttribute(JMSResourceParamsList[j],JMSResourceParamsListObjectsAttribute.getName())));
}
}
}
}
else
{
System.out.println(" " + JMSResourceParamsBeansAttribute.getName() + ": " + (connection.getAttribute(JMSResourceURL,JMSResourceParamsBeansAttribute.getName())));
}
}
}
else if (JMSSystemResourcesBeansAttribute.getType().equals("[Ljava.lang.String;"))
{
String[] tagnames = (String[])connection.getAttribute(JMSSystemResources,JMSSystemResourcesBeansAttribute.getName());
if (tagnames.length == 0) {
System.out.println(" " + JMSSystemResourcesBeansAttribute.getName()+": No Data");
}
else
{
System.out.println(" " + JMSSystemResourcesBeansAttribute.getName()+":");
for (int p = 0; p < tagnames.length; p++){
System.out.println(" - " + tagnames[p]);
}
}
}
else if (JMSSystemResourcesBeansAttribute.getType().equals("[Ljavax.management.ObjectName;"))
{
if (JMSSystemResourcesBeansAttribute.getName().equals("SubDeployments"))
{
ObjectName[] SubDeploymentsParamList = (ObjectName[])connection.getAttribute(JMSSystemResources,JMSSystemResourcesBeansAttribute.getName());
//System.out.println(" - ");
if (SubDeploymentsParamList.length == 0)
{
System.out.println(" " + JMSSystemResourcesBeansAttribute.getName() + ": Null" );
}
else
System.out.println(" " + JMSSystemResourcesBeansAttribute.getName() + ": " );
for (int k = 0; k < SubDeploymentsParamList.length; k++) {
MBeanAttributeInfo[] SubDeploymentsParamListMbeans = (connection.getMBeanInfo(SubDeploymentsParamList[k])).getAttributes();
System.out.println(" - ");
for(MBeanAttributeInfo info:SubDeploymentsParamListMbeans) {
if (info.getType().equals("[Ljavax.management.ObjectName;"))
{
ObjectName[] objnames = (ObjectName[]) connection.getAttribute(SubDeploymentsParamList[k],info.getName());
for (int j = 0; j < objnames.length; j++){
String objname = (String)connection.getAttribute(objnames[j],"Name");
System.out.println(" " + info.getName() + ": " + objname);
}
}
else if (info.getType().equals("[Ljava.lang.String;"))
{
String[] tagnames = (String[])connection.getAttribute(SubDeploymentsParamList[k],info.getName());
if (tagnames.length == 0) {
System.out.println(" " + info.getName()+": No Data");
}
else
{
System.out.println(" " + info.getName()+":");
for (int p = 0; p < tagnames.length; p++){
System.out.println(" - " + tagnames[p]);
}
}
}
else
System.out.println(" " + info.getName() + ": " + (connection.getAttribute(SubDeploymentsParamList[k],info.getName())));
}
}
}
else
{
ObjectName[] objnames = (ObjectName[]) connection.getAttribute(JMSSystemResources,JMSSystemResourcesBeansAttribute.getName());
for (int j = 0; j < objnames.length; j++){
String objname = (String)connection.getAttribute(objnames[j],"Name");
System.out.println(" " + JMSSystemResourcesBeansAttribute.getName() + ": " + objname);
}
}
}
else if (JMSSystemResourcesBeansAttribute.getName().equals("Parent") || JMSSystemResourcesBeansAttribute.getName().equals("Resource"))
{
}
else
System.out.println(" " + JMSSystemResourcesBeansAttribute.getName() + ": " + (connection.getAttribute(JMSSystemResources,JMSSystemResourcesBeansAttribute.getName())));
}
}
}
解决方案
我自己做。它对我有用
public static void parser(String javaxExcept, String LjavaxExcept, HashMap < String, Object > [] atrs, ObjectName ObjUrl, int ng, ObjectName[] origObjUrl) throws Exception {
if (ng > 20) {
System.out.println("Possibly loop detected");
for (int t = 0; t < origObjUrl.length; t++) {
System.out.println(origObjUrl[t]);
}
System.exit(1);
}
origObjUrl[ng] = ObjUrl;
MBeanAttributeInfo[] attributes = (connection.getMBeanInfo(ObjUrl)).getAttributes();
atrs[ng].put("ObjUrl", ObjUrl.toString());
for (MBeanAttributeInfo attribute: attributes) {
if (attribute.getType().equals("[Ljava.lang.String;")) {
String[] Ljava = (String[]) connection.getAttribute(ObjUrl, attribute.getName());
if (Ljava.length == 0) {
atrs[ng].put(attribute.getName(), "null");
} else {
for (int p = 0; p < Ljava.length; p++) {
atrs[ng].put(attribute.getName(), Ljava[p]);
}
}
} else if (attribute.getName().matches(".*(Password|KeyStorePass|PassPhrase|JNDIPropertiesCredential|ClearText|EncryptedValue).*")) {
atrs[ng].put(attribute.getName(), "clear or encrypt text");
} else if (attribute.getType().matches("java.util.Properties")) {
Map < String, String > propmap = (Map < String, String > ) connection.getAttribute(ObjUrl, attribute.getName());
if (attribute.getName().matches("BootProperties")) {
propmap.put("password", "{AES} encrypt text");
propmap.put("username", "{AES} encrypt text"); }
atrs[ng].put(attribute.getName(), propmap);
} else if (attribute.getType().equals("javax.management.ObjectName") && attribute.getName().matches(javaxExcept) && (connection.getAttribute(ObjUrl, attribute.getName())) != null) {
atrs[ng].put(attribute.getName(), (connection.getAttribute(ObjUrl, attribute.getName()).toString()));
} else if (attribute.getType().matches("java.lang.String|java.lang.Integer|java.lang.Boolean")) {
atrs[ng].put(attribute.getName(), (connection.getAttribute(ObjUrl, attribute.getName())));
} else if (attribute.getType().equals("javax.management.ObjectName") && (connection.getAttribute(ObjUrl, attribute.getName())) != null) {
atrs[ng].put(attribute.getName(), attribute.getName());
ng = ng + 1;
atrs[ng] = new HashMap < String, Object > ();
ObjUrl = (ObjectName) connection.getAttribute(ObjUrl, attribute.getName());
parser(javaxExcept, LjavaxExcept, atrs, ObjUrl, ng, origObjUrl);
atrs[ng - 1].put(attribute.getName(), atrs[ng]);
ng = ng - 1;
ObjUrl = origObjUrl[ng];
} else if (attribute.getType().equals("[Ljavax.management.ObjectName;") && attribute.getName().matches(LjavaxExcept)) {
ObjectName[] Ljavax = (ObjectName[]) connection.getAttribute(ObjUrl, attribute.getName());
atrs[ng].put(attribute.getName(), attribute.getName());
String[] Ljavaxarray = new String[Ljavax.length];
for (int j = 0; j < Ljavax.length; j++) {
Ljavaxarray[j] = Ljavax[j].toString();
}
atrs[ng].put(attribute.getName(), Ljavaxarray);
} else if (attribute.getType().equals("javax.management.openmbean.ArrayType")) {
CompositeData[] compdata = (CompositeData[]) connection.getAttribute(ObjUrl, attribute.getName());
String[] kvcompdata = new String[compdata.length];
int l = 0;
for (CompositeData kv: compdata) {
kvcompdata[l] = kv.get("DisplayName").toString();
l = l + 1;
}
atrs[ng].put(attribute.getName(), kvcompdata);
} else if (attribute.getType().equals("[Ljavax.management.ObjectName;")) {
ObjectName[] Ljavax = (ObjectName[]) connection.getAttribute(ObjUrl, attribute.getName());
atrs[ng].put(attribute.getName(), attribute.getName());
HashMap < String, Object > Ljavaxmap = new HashMap < String, Object > ();
for (int j = 0; j < Ljavax.length; j++) {
ng = ng + 1;
atrs[ng] = new HashMap < String, Object > ();
ObjUrl = Ljavax[j];
parser(javaxExcept, LjavaxExcept, atrs, ObjUrl, ng, origObjUrl);
boolean success = false;
String Ljavaxaname = new String();
try {
Ljavaxaname = (connection.getAttribute(Ljavax[j], "Name")).toString();
success = true;
} catch (AttributeNotFoundException e) {}
if (success) {
Ljavaxaname = (connection.getAttribute(Ljavax[j], "Name")).toString();
} else {
Ljavaxaname = (connection.getAttribute(Ljavax[j], "Key")).toString();
}
Ljavaxmap.put(Ljavaxaname, atrs[ng]);
ng = ng - 1;
}
atrs[ng].put(attribute.getName(), Ljavaxmap);
ObjUrl = origObjUrl[ng];
} else {
if (connection.getAttribute(ObjUrl, attribute.getName()) == null) {
atrs[ng].put(attribute.getName(), "null");
} else {
atrs[ng].put(attribute.getName(), (connection.getAttribute(ObjUrl, attribute.getName()).toString()));
}
}
}
}
推荐阅读
- typescript - Safari 浏览器 xlsx 下载不起作用,而是在新选项卡中打开 blob
- visual-studio-code - NativeScript VSCode 扩展智能感知不起作用
- c++ - 使用 gcov 显式刷新 gcda 文件
- android - 如何在android的textview中显示与JsonObject的距离和位置?
- node.js - Angular `CORS` 策略 Eroro
- python - 如何使用 MPI 使用多个进程读取大型 JSON 文件。每个进程可以同时读取其文件块
- c - 为什么在链表中按排序顺序插入节点时使用双指针?
- knockout.js - 我无法从 Knockout JS 数组中删除标记
- python - 在 Python 中将嵌套的 JSON 转换为 CSV
- create-react-app - create-react-app 创建的 package.json 中的 browserslist 有什么意义