1 package org.apache.tools.ant.taskdefs.optional.jmx; 2 3 52 53 54 import javax.management.MBeanParameterInfo ; 55 import org.apache.tools.ant.BuildException; 56 57 58 59 71 public class ShowMBeanTask extends AbstractMBeanTask { 72 73 74 77 private static final int ATTR_NAME_COL_LENGTH = 35; 78 79 82 private static final int ATTR_TYPE_COL_LENGTH = 40; 83 84 87 private static final int ATTR_PERM_COL_LENGTH = 6; 88 89 92 private static final int ATTR_VALUE_COL_LENGTH = 20; 93 94 100 protected void execute(javax.management.MBeanServer mbserver) 101 throws BuildException { 102 103 try { 104 javax.management.ObjectName mbeanName = getObjectName(); 105 106 if (mbserver.isRegistered(mbeanName)) { 107 108 try { 109 110 javax.management.MBeanInfo mbeanInfo = mbserver.getMBeanInfo(mbeanName); 111 112 if (mbeanInfo != null) { 113 log("Description of MBean [" + mbeanName+ "]"); 114 115 showAttributes(mbserver,mbeanInfo); 116 showOperations(mbserver,mbeanInfo); 117 showConstructors(mbserver,mbeanInfo); 118 119 } else { 120 log("Warning! MBeanInfo was null for mbean at " + toString(),org.apache.tools.ant.Project.MSG_WARN); 121 } 122 123 } catch (Exception x) { 124 String message = "Cannot show mbean " + mbeanName; 125 throw new BuildException(message); 126 } 127 128 } else { 129 throw new BuildException("Cannot find MBean. " + mbeanName.getCanonicalName()); 130 } 131 132 } catch (javax.management.MalformedObjectNameException x) { 133 x.printStackTrace(); 134 throw new BuildException(x); 135 } 136 } 137 138 private static void repeat(StringBuffer buffer, char toRepeat, int repeatTimes) { 139 for (int counter = 0; counter < repeatTimes; counter++) { 140 buffer.append(toRepeat); 141 } 142 } 143 144 private static void pad(StringBuffer buffer, String toPad, int length) { 145 int diff = length - toPad.length(); 146 if (diff <= 0) { 147 buffer.append(toPad.substring(0,length)); 148 } else { 149 buffer.append(toPad); 150 repeat(buffer,' ',diff); 151 } 152 } 153 154 protected void showAttributes(javax.management.MBeanServer mbserver, 155 javax.management.MBeanInfo mbeanInfo) 156 throws javax.management.MalformedObjectNameException , 157 javax.management.MBeanException , 158 javax.management.AttributeNotFoundException , 159 javax.management.InstanceNotFoundException , 160 javax.management.ReflectionException 161 { 162 163 log("\nList of attributes for MBean [" + getObjectName() + "]"); 164 StringBuffer header = new StringBuffer (); 165 pad(header,"Name",ATTR_NAME_COL_LENGTH+1); 166 pad(header,"Type",ATTR_TYPE_COL_LENGTH+1); 167 pad(header,"Access",ATTR_PERM_COL_LENGTH+1); 168 header.append("Value"); 169 log(header.toString()); 170 171 header = new StringBuffer (); 172 repeat(header,'-',ATTR_NAME_COL_LENGTH); 173 header.append(" "); 174 repeat(header,'-',ATTR_TYPE_COL_LENGTH); 175 header.append(" "); 176 repeat(header,'-',ATTR_PERM_COL_LENGTH); 177 header.append(" "); 178 repeat(header,'-',ATTR_VALUE_COL_LENGTH); 179 log(header.toString()); 180 181 javax.management.MBeanAttributeInfo [] info = mbeanInfo.getAttributes(); 182 if (info != null) { 183 for (int counter = 0; counter < info.length; counter++) { 188 String attribValueAsString = ""; 192 if (info[counter].isReadable()) { 193 Object attribValue = mbserver.getAttribute(getObjectName(),info[counter].getName()); 194 attribValueAsString = org.apache.tools.ant.taskdefs.optional.jmx.converter.ValueFactory.toString(attribValue); 195 } 196 StringBuffer message = new StringBuffer (); 197 198 pad(message,info[counter].getName(),ATTR_NAME_COL_LENGTH); 199 message.append(" "); 200 pad(message,info[counter].getType(),ATTR_TYPE_COL_LENGTH); 201 message.append(" "); 202 if (info[counter].isReadable() && info[counter].isWritable()) { 203 pad(message,"RW",ATTR_PERM_COL_LENGTH); 204 } else if (info[counter].isReadable()) { 205 pad(message,"R-",ATTR_PERM_COL_LENGTH); 206 } else if (info[counter].isWritable()) { 207 pad(message,"-W",ATTR_PERM_COL_LENGTH); 208 } else { 209 pad(message,"--",ATTR_PERM_COL_LENGTH); 210 } 211 message.append(" "); 212 message.append(attribValueAsString != null ? attribValueAsString : "null"); 213 log(message.toString()); 214 } 215 216 } 217 } 218 219 protected void showOperations(javax.management.MBeanServer mbserver, 220 javax.management.MBeanInfo mbeanInfo) 221 throws javax.management.MalformedObjectNameException { 222 223 log("\nList of operations for MBean [" + getObjectName() + "]"); 224 javax.management.MBeanOperationInfo [] info = mbeanInfo.getOperations(); 225 if (info != null) { 226 for (int counter = 0; counter < info.length; counter++) { 231 StringBuffer opDesc = new StringBuffer (); 232 233 opDesc.append(info[counter].getReturnType() + " " + info[counter].getName() + "("); 234 MBeanParameterInfo [] paramInfo = info[counter].getSignature(); 235 for (int paramCounter = 0; paramCounter < paramInfo.length; paramCounter++) { 236 opDesc.append(paramInfo[paramCounter].getType()); 237 if ((paramInfo[paramCounter].getName() == null) || (paramInfo[paramCounter].getName().length() == 0) 238 || (paramInfo[paramCounter].getType() == paramInfo[paramCounter].getName())) { 239 opDesc.append(" arg" + paramCounter); 240 } else { 241 opDesc.append(" " + paramInfo[paramCounter].getName()); 242 } 243 if ((paramCounter+1) < paramInfo.length) { 244 opDesc.append(", "); 245 } 246 } 247 opDesc.append(");"); 248 log("\t"+opDesc.toString()); 249 } 250 } 251 } 252 253 protected void showConstructors(javax.management.MBeanServer mbserver, 254 javax.management.MBeanInfo mbeanInfo) 255 throws javax.management.MalformedObjectNameException { 256 257 log("\nList of constructors for MBean [" + getObjectName() + "]"); 258 javax.management.MBeanConstructorInfo [] info = mbeanInfo.getConstructors(); 259 if (info != null) { 260 for (int counter = 0; counter < info.length; counter++) { 265 StringBuffer opDesc = new StringBuffer (); 266 267 opDesc.append(info[counter].getName() + "("); 268 MBeanParameterInfo [] paramInfo = info[counter].getSignature(); 269 for (int paramCounter = 0; paramCounter < paramInfo.length; paramCounter++) { 270 opDesc.append(paramInfo[paramCounter].getType()); 271 if ((paramInfo[paramCounter].getName() == null) || (paramInfo[paramCounter].getName().length() == 0) 272 || (paramInfo[paramCounter].getType() == paramInfo[paramCounter].getName())) { 273 opDesc.append(" arg" + paramCounter); 274 } else { 275 opDesc.append(" " + paramInfo[paramCounter].getName()); 276 } 277 if ((paramCounter+1) < paramInfo.length) { 278 opDesc.append(", "); 279 } 280 } 281 opDesc.append(");"); 282 log("\t"+opDesc.toString()); 283 } 284 } 285 } 286 287 288 } 289 290 | Popular Tags |