1 22 package org.jboss.console.twiddle.command; 23 24 import java.io.PrintWriter ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 import javax.management.Attribute ; 31 import javax.management.AttributeList ; 32 import javax.management.MBeanServerConnection ; 33 import javax.management.ObjectName ; 34 35 import org.jboss.management.j2ee.J2EETypeConstants; 36 37 43 public class JSR77Command extends MBeanServerCommand 44 { 45 private static final String INDENT = " "; 46 47 private static final Map j2eeTypeMap = new HashMap (); 48 static 49 { 50 j2eeTypeMap.put(J2EETypeConstants.J2EEDomain, 51 new J2EEType(J2EETypeConstants.J2EEDomain, null, new String [] { "servers" }, null)); 52 j2eeTypeMap.put(J2EETypeConstants.J2EEServer, 53 new J2EEType(J2EETypeConstants.J2EEServer, new String [] { "serverVendor", "serverVersion" }, new String [] { "deployedObjects", "resources", "javaVMs" }, null)); 54 j2eeTypeMap.put(J2EETypeConstants.JVM, 55 new J2EEType(J2EETypeConstants.JVM, new String [] { "javaVersion", "javaVendor", "node" }, null, null)); 56 j2eeTypeMap.put(J2EETypeConstants.J2EEApplication, 57 new J2EEType(J2EETypeConstants.J2EEApplication, new String [] { }, null, new String [] { "modules" })); 58 j2eeTypeMap.put(J2EETypeConstants.AppClientModule, 59 new J2EEType(J2EETypeConstants.AppClientModule, null, null, null)); 60 j2eeTypeMap.put(J2EETypeConstants.EJBModule, 61 new J2EEType(J2EETypeConstants.EJBModule, null, new String [] { "ejbs" }, null)); 62 j2eeTypeMap.put(J2EETypeConstants.EntityBean, 63 new J2EEType(J2EETypeConstants.EntityBean, new String [] { "JndiName" }, null, null)); 64 j2eeTypeMap.put(J2EETypeConstants.MessageDrivenBean, 65 new J2EEType(J2EETypeConstants.MessageDrivenBean, new String [] { "JndiName" }, null, null)); 66 j2eeTypeMap.put(J2EETypeConstants.StatelessSessionBean, 67 new J2EEType(J2EETypeConstants.StatelessSessionBean, new String [] { "JndiName" }, null, null)); 68 j2eeTypeMap.put(J2EETypeConstants.StatefulSessionBean, 69 new J2EEType(J2EETypeConstants.StatefulSessionBean, new String [] { "JndiName" }, null, null)); 70 j2eeTypeMap.put(J2EETypeConstants.WebModule, 71 new J2EEType(J2EETypeConstants.WebModule, new String [] { }, new String [] { "servlets" }, null)); 72 j2eeTypeMap.put(J2EETypeConstants.Servlet, 73 new J2EEType(J2EETypeConstants.Servlet, null, null, null)); 74 j2eeTypeMap.put(J2EETypeConstants.ServiceModule, 75 new J2EEType(J2EETypeConstants.ServiceModule, new String [] { }, new String [] { "MBeans" }, null)); 76 j2eeTypeMap.put(J2EETypeConstants.MBean, 77 new J2EEType(J2EETypeConstants.MBean, new String [] { "stateMonitored", "StateString" }, null, null)); 78 j2eeTypeMap.put(J2EETypeConstants.ResourceAdapterModule, 79 new J2EEType(J2EETypeConstants.ResourceAdapterModule, new String [] { }, new String [] { "resourceAdapters" }, null)); 80 j2eeTypeMap.put(J2EETypeConstants.ResourceAdapter, 81 new J2EEType(J2EETypeConstants.ResourceAdapter, null, new String [] { "jcaResource" }, null)); 82 j2eeTypeMap.put(J2EETypeConstants.JCAResource, 83 new J2EEType(J2EETypeConstants.JCAResource, null, new String [] { "connectionFactories" }, null)); 84 j2eeTypeMap.put(J2EETypeConstants.JCAConnectionFactory, 85 new J2EEType(J2EETypeConstants.JCAConnectionFactory, null, new String [] { "managedConnectionFactory" }, null)); 86 j2eeTypeMap.put(J2EETypeConstants.JCAManagedConnectionFactory, 87 new J2EEType(J2EETypeConstants.JCAManagedConnectionFactory, null, null, null)); 88 j2eeTypeMap.put(J2EETypeConstants.JNDIResource, 89 new J2EEType(J2EETypeConstants.JNDIResource, new String [] { "StateString" }, null, null)); 90 j2eeTypeMap.put(J2EETypeConstants.JTAResource, 91 new J2EEType(J2EETypeConstants.JTAResource, null, null, null)); 92 j2eeTypeMap.put(J2EETypeConstants.RMI_IIOPResource, 93 new J2EEType(J2EETypeConstants.RMI_IIOPResource, null, null, null)); 94 j2eeTypeMap.put(J2EETypeConstants.JavaMailResource, 95 new J2EEType(J2EETypeConstants.JavaMailResource, new String [] { "StateString" }, null, null)); 96 j2eeTypeMap.put(J2EETypeConstants.JMSResource, 97 new J2EEType(J2EETypeConstants.JMSResource, null, null, null)); 98 } 99 100 103 public JSR77Command() 104 { 105 super("jsr77", "Print out JSR77 related information"); 106 } 107 108 public void displayHelp() 109 { 110 PrintWriter out = context.getWriter(); 111 112 out.println(desc); 113 out.println(); 114 out.println("Print out JSR77 related information"); 115 out.println(); 116 out.println("Usage: " + name); 117 out.println(); 118 119 out.flush(); 120 } 121 122 public void execute(String [] args) throws Exception 123 { 124 130 MBeanServerConnection server = super.getMBeanServer(); 131 PrintWriter out = context.getWriter(); 132 133 Set jsr77Domains = locateJSR77Domains(server); 134 for (Iterator i = jsr77Domains.iterator(); i.hasNext(); ) 135 { 136 printJsr77Node(server, (ObjectName )i.next(), out, 0); 137 } 138 out.flush(); 139 } 140 141 148 private static Set locateJSR77Domains(MBeanServerConnection server) throws Exception 149 { 150 ObjectName domainNameQuery = new ObjectName ("*:j2eeType=J2EEDomain,*"); 154 Set domainNames = server.queryNames(domainNameQuery, null); 155 156 for (Iterator i = domainNames.iterator(); i.hasNext(); ) 157 { 158 ObjectName objectName = (ObjectName )i.next(); 159 160 if (objectName.getDomain().equals(objectName.getKeyProperty("name")) == false) 161 { 162 i.remove(); 165 } 166 } 167 return domainNames; 168 } 169 170 173 private static void printJsr77Node(MBeanServerConnection server, ObjectName node, PrintWriter out, int depth) throws Exception 174 { 175 String j2eeType = node.getKeyProperty("j2eeType"); 176 String name = node.getKeyProperty("name"); 177 178 J2EEType type = (J2EEType)j2eeTypeMap.get(j2eeType); 179 if (type == null) 180 { 181 println(out, depth, "Unknown j2eeType=" + j2eeType); 182 } 183 else 184 { 185 println(out, depth, j2eeType + "=" + name); 187 188 if (type.attributes != null) 190 { 191 AttributeList attrs = server.getAttributes(node, type.attributes); 192 for (int i = 0; i < attrs.size(); i++) 193 { 194 Attribute attr = (Attribute )attrs.get(i); 195 println(out, depth + 1, attr.getName() + "=" + attr.getValue()); 196 } 197 } 198 199 if (type.associations != null) 201 { 202 for (int i = 0; i < type.associations.length; i++) 203 { 204 String association = type.associations[i]; 205 String [] children = (String [])server.getAttribute(node, association); 206 for (int j = 0; j < children.length; j++) 207 { 208 String child = children[j]; 209 ObjectName subnode = new ObjectName (child); 210 String subnodeType = subnode.getKeyProperty("j2eeType"); 211 String subnodeName = subnode.getKeyProperty("name"); 212 213 println(out, depth + 1, subnodeType + "=" + subnodeName); 214 } 215 } 216 } 217 218 if (type.containment != null) 220 { 221 for (int i = 0; i < type.containment.length; i++) 222 { 223 String containment = type.containment[i]; 224 Object result = server.getAttribute(node, containment); 225 if (result instanceof ObjectName ) 227 { 228 printJsr77Node(server, (ObjectName )result, out, depth + 1); 229 } 230 else if (result instanceof String ) 231 { 232 printJsr77Node(server, new ObjectName ((String )result), out, depth + 1); 233 } 234 else if (result instanceof String []) 235 { 236 String [] children = (String [])result; 237 for (int j = 0; j < children.length; j++) 238 { 239 String child = children[j]; 240 ObjectName subnode = new ObjectName (child); 241 if (depth < 2) 242 out.println(); 243 printJsr77Node(server, subnode, out, depth + 1); 244 } 245 } 246 } 247 } 248 } 249 } 250 251 254 private static void println(PrintWriter out, int depth, String msg) 255 { 256 for (int i = 0; i < depth; i++) 257 { 258 out.print(INDENT); 259 } 260 out.println(msg); 261 } 262 263 267 private static class J2EEType 268 { 269 public String type; 270 public String [] attributes; 271 public String [] containment; 272 public String [] associations; 273 274 public J2EEType(String type, String [] attributes, String [] containment, String [] associations) 275 { 276 this.type = type; 277 this.attributes = attributes; 278 this.containment = containment; 279 this.associations = associations; 280 } 281 } 282 } 283 | Popular Tags |