1 22 package org.jboss.console.twiddle.command; 23 24 import java.io.PrintWriter ; 25 import java.util.Date ; 26 27 import javax.management.MBeanAttributeInfo ; 28 import javax.management.MBeanConstructorInfo ; 29 import javax.management.MBeanInfo ; 30 import javax.management.MBeanNotificationInfo ; 31 import javax.management.MBeanOperationInfo ; 32 import javax.management.MBeanParameterInfo ; 33 import javax.management.ObjectName ; 34 35 45 public class XMBeanCommand extends MBeanServerCommand 46 { 47 50 public XMBeanCommand() 51 { 52 super("xmbean", "Print out mbean metadata as an xmbean descriptor"); 53 } 54 55 public void displayHelp() 56 { 57 PrintWriter out = context.getWriter(); 58 59 out.println(desc); 60 out.println(); 61 out.println("Redirect the output and use it as a skeleton for"); 62 out.println("writing an xmbean descriptor for an existing mbean."); 63 out.println(); 64 out.println("Usage: " + name + " <object-name>"); 65 out.println(); 66 67 out.flush(); 68 } 69 70 public void execute(String [] args) throws Exception 71 { 72 if (args.length != 1) 73 { 74 throw new CommandException("Missing object name"); 75 } 76 ObjectName target = super.createObjectName(args[0]); 77 78 MBeanInfo mbeanInfo = getMBeanServer().getMBeanInfo(target); 79 MBeanConstructorInfo [] ctors = mbeanInfo.getConstructors(); 80 MBeanAttributeInfo [] attrs = mbeanInfo.getAttributes(); 81 MBeanOperationInfo [] ops = mbeanInfo.getOperations(); 82 MBeanNotificationInfo [] notifs = mbeanInfo.getNotifications(); 83 84 PrintWriter out = context.getWriter(); 85 86 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 88 out.println("<!DOCTYPE mbean PUBLIC"); 89 out.println(" \"-//JBoss//DTD JBOSS XMBEAN 1.2//EN\""); 90 out.println(" \"http://www.jboss.org/j2ee/dtd/jboss_xmbean_1_2.dtd\">"); 91 out.println("<!--"); 92 out.println(" xmbean descriptor generated by 'twiddle'"); 93 out.println(" on " + new Date ()); 94 out.println(" for '" + target + "'"); 95 out.println("-->"); 96 97 out.println("<mbean>"); 98 99 out.println(" <description>" + mbeanInfo.getDescription() + "</description>"); 101 out.println(" <class>" + mbeanInfo.getClassName() + "</class>"); 102 out.println(); 103 104 if (ctors.length > 0) 106 { 107 for (int i = 0; i < ctors.length; i++) 108 { 109 MBeanConstructorInfo ctorInfo = ctors[i]; 110 out.println(" <constructor>"); 111 out.println(" <description>" + ctorInfo.getDescription() + "</description>"); 112 out.println(" <name>" + ctorInfo.getName() + "</name>"); 113 outputParameters(out, ctorInfo.getSignature()); 114 out.println(" </constructor>"); 115 } 116 out.println(); 117 } 118 119 if (attrs.length > 0) 121 { 122 for (int i = 0; i < attrs.length; i++) 123 { 124 MBeanAttributeInfo attrInfo = attrs[i]; 125 126 String access = "read-write"; 128 access = attrInfo.isReadable() ? access : "write-only"; 129 access = attrInfo.isWritable() ? access : "read-only"; 130 String accessString = " access='" + access + "'"; 131 132 String getMethodString = ""; 134 if (attrInfo.isReadable()) 135 { 136 getMethodString = " getMethod='" + 137 (attrInfo.isIs() ? "is" : "get") + attrInfo.getName() + "'"; 138 } 139 140 String setMethodString = ""; 142 if (attrInfo.isWritable()) 143 { 144 setMethodString = " setMethod='set" + attrInfo.getName() + "'"; 145 146 } 147 148 out.println(" <attribute" + accessString + getMethodString + setMethodString + ">"); 149 out.println(" <description>" + attrInfo.getDescription() + "</description>"); 150 out.println(" <name>" + attrInfo.getName() + "</name>"); 151 out.println(" <type>" + attrInfo.getType() + "</type>"); 152 out.println(" </attribute>"); 153 } 154 out.println(); 155 } 156 157 if (ops.length > 0) 159 { 160 for (int i = 0; i < ops.length; i++) 161 { 162 MBeanOperationInfo opInfo = ops[i]; 163 out.println(" <operation>"); 165 out.println(" <description>" + opInfo.getDescription() + "</description>"); 166 out.println(" <name>" + opInfo.getName() + "</name>"); 167 outputParameters(out, opInfo.getSignature()); 168 out.println(" <return-type>" + opInfo.getReturnType() + "</return-type>"); 169 out.println(" </operation>"); 170 } 171 out.println(); 172 } 173 174 if (notifs.length > 0) 176 { 177 for (int i = 0; i < notifs.length; i++) 178 { 179 MBeanNotificationInfo notifInfo = notifs[i]; 180 String [] types = notifInfo.getNotifTypes(); 181 out.println(" <notification>"); 182 out.println(" <description>" + notifInfo.getDescription() + "</description>"); 183 out.println(" <name>" + notifInfo.getName() + "</name>"); 184 for (int j = 0; j < types.length; j++) 185 { 186 out.println(" <notification-type>" + types[j] + "</notification-type>"); 187 } 188 out.println(" </notification>"); 189 } 190 out.println(); 191 } 192 193 out.println("</mbean>"); 194 195 out.flush(); 196 } 197 198 private void outputParameters(PrintWriter out, MBeanParameterInfo [] params) 199 { 200 for (int i = 0; i < params.length; i++) 201 { 202 MBeanParameterInfo paramInfo = params[i]; 203 out.println(" <parameter>"); 204 out.println(" <description>" + paramInfo.getDescription() + "</description>"); 205 out.println(" <name>" + paramInfo.getName() + "</name>"); 206 out.println(" <type>" + paramInfo.getType() + "</type>"); 207 out.println(" </parameter>"); 208 } 209 } 210 } 211 | Popular Tags |