KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > adaptor > http > ServerByDomainCommandProcessor


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8 package mx4j.tools.adaptor.http;
9
10 import java.io.IOException JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14 import java.util.TreeMap JavaDoc;
15 import java.util.TreeSet JavaDoc;
16 import javax.management.JMException JavaDoc;
17 import javax.management.MBeanInfo JavaDoc;
18 import javax.management.MalformedObjectNameException JavaDoc;
19 import javax.management.ObjectInstance JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24
25 /**
26  * ServerByDomainCommandProcessor, processes a request for getting all the
27  * MBeans of the current server grouped by domains
28  *
29  * @version $Revision: 1.3 $
30  */

31 public class ServerByDomainCommandProcessor extends HttpCommandProcessorAdaptor
32 {
33    public ServerByDomainCommandProcessor()
34    {
35    }
36
37    public Document JavaDoc executeRequest(HttpInputStream in) throws IOException JavaDoc, JMException JavaDoc
38    {
39       Document JavaDoc document = builder.newDocument();
40
41       Element JavaDoc root = document.createElement("Server");
42       document.appendChild(root);
43
44       String JavaDoc targetClass = in.getVariable("instanceof");
45       String JavaDoc queryNames = in.getVariable("querynames");
46       ObjectName JavaDoc query = null;
47       if (queryNames != null)
48       {
49          try
50          {
51             query = new ObjectName JavaDoc(queryNames);
52          }
53          catch (MalformedObjectNameException JavaDoc e)
54          {
55             Element JavaDoc exceptionElement = document.createElement("Exception");
56             exceptionElement.setAttribute("errorMsg", e.getMessage());
57             root.appendChild(exceptionElement);
58             return document;
59          }
60       }
61       Set JavaDoc mbeans = server.queryMBeans(query, null);
62       Iterator JavaDoc i = mbeans.iterator();
63       // this will order the domains
64
Map JavaDoc domains = new TreeMap JavaDoc();
65       while (i.hasNext())
66       {
67          ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc)i.next();
68          ObjectName JavaDoc name = instance.getObjectName();
69          String JavaDoc domain = name.getDomain();
70          if (domains.containsKey(domain))
71          {
72             ((Set JavaDoc)domains.get(domain)).add(name);
73          }
74          else
75          {
76             Set JavaDoc objects = new TreeSet JavaDoc(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 JavaDoc domain = (String JavaDoc)i.next();
85          Element JavaDoc domainElement = document.createElement("Domain");
86          root.appendChild(domainElement);
87          domainElement.setAttribute("name", domain);
88          Set JavaDoc names = (Set JavaDoc)domains.get(domain);
89          Iterator JavaDoc j = names.iterator();
90          while (j.hasNext())
91          {
92             ObjectName JavaDoc targetName = (ObjectName JavaDoc)j.next();
93             if (targetClass != null && !server.isInstanceOf(targetName, targetClass))
94             {
95                continue;
96             }
97             Element JavaDoc mBeanElement = document.createElement("MBean");
98             mBeanElement.setAttribute("objectname", targetName.toString());
99             MBeanInfo JavaDoc 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