1 8 package mx4j.tools.adaptor.http; 9 10 import java.io.IOException ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import java.util.Set ; 14 import java.util.TreeMap ; 15 import java.util.TreeSet ; 16 import javax.management.JMException ; 17 import javax.management.MBeanInfo ; 18 import javax.management.MalformedObjectNameException ; 19 import javax.management.ObjectInstance ; 20 import javax.management.ObjectName ; 21 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.Element ; 24 25 31 public class ServerByDomainCommandProcessor extends HttpCommandProcessorAdaptor 32 { 33 public ServerByDomainCommandProcessor() 34 { 35 } 36 37 public Document executeRequest(HttpInputStream in) throws IOException , JMException 38 { 39 Document document = builder.newDocument(); 40 41 Element root = document.createElement("Server"); 42 document.appendChild(root); 43 44 String targetClass = in.getVariable("instanceof"); 45 String queryNames = in.getVariable("querynames"); 46 ObjectName query = null; 47 if (queryNames != null) 48 { 49 try 50 { 51 query = new ObjectName (queryNames); 52 } 53 catch (MalformedObjectNameException e) 54 { 55 Element exceptionElement = document.createElement("Exception"); 56 exceptionElement.setAttribute("errorMsg", e.getMessage()); 57 root.appendChild(exceptionElement); 58 return document; 59 } 60 } 61 Set mbeans = server.queryMBeans(query, null); 62 Iterator i = mbeans.iterator(); 63 Map domains = new TreeMap (); 65 while (i.hasNext()) 66 { 67 ObjectInstance instance = (ObjectInstance )i.next(); 68 ObjectName name = instance.getObjectName(); 69 String domain = name.getDomain(); 70 if (domains.containsKey(domain)) 71 { 72 ((Set )domains.get(domain)).add(name); 73 } 74 else 75 { 76 Set objects = new TreeSet (CommandProcessorUtil.createObjectNameComparator()); 77 objects.add(name); 78 domains.put(domain, objects); 79 } 80 } 81 i = domains.keySet().iterator(); 82 while (i.hasNext()) 83 { 84 String domain = (String )i.next(); 85 Element domainElement = document.createElement("Domain"); 86 root.appendChild(domainElement); 87 domainElement.setAttribute("name", domain); 88 Set names = (Set )domains.get(domain); 89 Iterator j = names.iterator(); 90 while (j.hasNext()) 91 { 92 ObjectName targetName = (ObjectName )j.next(); 93 if (targetClass != null && !server.isInstanceOf(targetName, targetClass)) 94 { 95 continue; 96 } 97 Element mBeanElement = document.createElement("MBean"); 98 mBeanElement.setAttribute("objectname", targetName.toString()); 99 MBeanInfo info = server.getMBeanInfo(targetName); 100 mBeanElement.setAttribute("description", info.getDescription()); 101 mBeanElement.setAttribute("classname", info.getClassName()); 102 domainElement.appendChild(mBeanElement); 103 } 104 } 105 return document; 106 } 107 108 } 109 | Popular Tags |