1 23 24 31 package com.sun.enterprise.admin.monitor; 32 33 import java.util.ArrayList ; 34 import java.util.Iterator ; 35 36 import javax.management.Attribute ; 37 import javax.management.AttributeList ; 38 import javax.management.AttributeNotFoundException ; 39 import javax.management.InstanceNotFoundException ; 40 import javax.management.MBeanAttributeInfo ; 41 import javax.management.MBeanInfo ; 42 import javax.management.ObjectName ; 43 44 47 public class MonitorGetCommand extends MonitorCommand { 48 49 52 private static final int GET_ATTR = 2; 53 54 57 private static final int GET_ALL_ATTR = 4; 58 59 63 private static final int GET_ATTR_FOR_TYPE = 6; 64 65 69 private static final int GET_ALL_ATTR_FOR_TYPE = 8; 70 71 76 private String attrName; 77 78 84 MonitorGetCommand(ObjectName mbeanName, String attrName) { 85 this.objectName = mbeanName; 86 this.attrName = attrName; 87 if (WILDCARD.equals(attrName)) { 88 actionCode = GET_ALL_ATTR; 89 } else { 90 actionCode = GET_ATTR; 91 } 92 } 93 94 101 MonitorGetCommand(ObjectName mbeanName, MonitoredObjectType type, 102 String attrName) { 103 this.objectName = mbeanName; 104 this.monitoredObjectType = type.getTypeName(); 105 this.attrName = attrName; 106 if (WILDCARD.equals(attrName)) { 107 actionCode = GET_ALL_ATTR_FOR_TYPE; 108 } else { 109 actionCode = GET_ATTR_FOR_TYPE; 110 } 111 } 112 113 119 Object runCommand() throws InstanceNotFoundException , 120 AttributeNotFoundException { 121 BaseMonitorMBean mbean = MonitoringHelper.getMonitorMBean(objectName); 122 MonitoredObjectType type = null; 123 if (monitoredObjectType != null) { 124 type = MonitoredObjectType.getMonitoredObjectType(monitoredObjectType); 125 } 126 ArrayList mbeanList = null; 127 if (actionCode == GET_ATTR_FOR_TYPE 128 || actionCode == GET_ALL_ATTR_FOR_TYPE) { 129 mbeanList = mbean.getChildList(type); 130 } else { 131 mbeanList = new ArrayList (); 132 mbeanList.add(mbean); 133 } 134 AttributeList result = new AttributeList (); 135 Iterator iter = mbeanList.iterator(); 136 while (iter.hasNext()) { 137 BaseMonitorMBean bmb = (BaseMonitorMBean)iter.next(); 138 if (actionCode == GET_ATTR || actionCode == GET_ATTR_FOR_TYPE) { 139 Object val = bmb.getAttribute(attrName); 140 Attribute attr = null; 141 if (actionCode == GET_ATTR_FOR_TYPE) { 142 attr = new Attribute (getQualifiedName(bmb, type, attrName), 143 val); 144 } else { 145 attr = new Attribute (attrName, val); 146 } 147 result.add(attr); 148 } else if (actionCode == GET_ALL_ATTR 149 || actionCode == GET_ALL_ATTR_FOR_TYPE) { 150 String [] attrNames = bmb.getAllAttributeNames(); 151 AttributeList attrList = bmb.getAttributes(attrNames); 152 if (actionCode == GET_ALL_ATTR_FOR_TYPE) { 153 AttributeList newAttrList = new AttributeList (); 154 int numAttr = attrList.size(); 155 for (int i = 0; i < numAttr; i++) { 156 Attribute attr = (Attribute )attrList.get(i); 157 attr = new Attribute ( 158 getQualifiedName(bmb, type, attr.getName()), 159 attr.getValue()); 160 newAttrList.add(attr); 161 } 162 attrList = newAttrList; 163 } 164 result.addAll(attrList); 165 } 166 } 167 return result; 168 } 169 170 179 private String getQualifiedName(BaseMonitorMBean mbean, 180 MonitoredObjectType type, String attrName) { 181 StringBuffer fullName = new StringBuffer (); 182 fullName.append(mbean.getNodeType()); 183 if (!type.isSingleton()) { 184 fullName.append("." + mbean.getNodeName()); 185 } 186 fullName.append("." + attrName); 187 return fullName.toString(); 188 } 189 190 193 private static final String WILDCARD = "*"; 194 195 } 196 | Popular Tags |