我使用JMX从服务器获取mbeans值(Weblogic JMS配置)
并且diferrent属性具有不同的返回类型,例如:
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())));
}
}
}
答案 0 :(得分:0)
我自己做。对我有用
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()));
}
}
}
}