KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set JavaDoc;
13 import java.util.TreeSet JavaDoc;
14 import javax.management.JMException JavaDoc;
15 import javax.management.MBeanInfo JavaDoc;
16 import javax.management.MalformedObjectNameException JavaDoc;
17 import javax.management.ObjectInstance JavaDoc;
18 import javax.management.ObjectName JavaDoc;
19
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22
23 /**
24  * ServerCommandProcessor, processes a request for getting all the
25  * MBeans of the current server
26  *
27  * @version $Revision: 1.3 $
28  */

29 public class ServerCommandProcessor extends HttpCommandProcessorAdaptor
30 {
31    public ServerCommandProcessor()
32    {
33    }
34
35    public Document JavaDoc executeRequest(HttpInputStream in) throws IOException JavaDoc, JMException JavaDoc
36    {
37       Document JavaDoc document = builder.newDocument();
38
39       Element JavaDoc root = document.createElement("Server");
40       document.appendChild(root);
41
42       String JavaDoc classVariable = in.getVariable("instanceof");
43       String JavaDoc queryNames = in.getVariable("querynames");
44       Set JavaDoc mbeans = null;
45       ObjectName JavaDoc query = null;
46       if (queryNames != null)
47       {
48          try
49          {
50             query = new ObjectName JavaDoc(queryNames);
51             mbeans = new TreeSet JavaDoc(CommandProcessorUtil.createObjectInstanceComparator());
52             mbeans.addAll(server.queryMBeans(query, null));
53          }
54          catch (MalformedObjectNameException JavaDoc e)
55          {
56             Element JavaDoc exceptionElement = document.createElement("Exception");
57             exceptionElement.setAttribute("errorMsg", e.getMessage());
58             root.appendChild(exceptionElement);
59             return document;
60          }
61       }
62       else
63       {
64          mbeans = new TreeSet JavaDoc(CommandProcessorUtil.createObjectInstanceComparator());
65          mbeans.addAll(server.queryMBeans(null, null));
66       }
67       Iterator JavaDoc i = mbeans.iterator();
68       while (i.hasNext())
69       {
70          ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc)i.next();
71          if (classVariable != null && !classVariable.equals(instance.getClassName()))
72          {
73             continue;
74          }
75          Element JavaDoc mBeanElement = document.createElement("MBean");
76          mBeanElement.setAttribute("objectname", instance.getObjectName().toString());
77          mBeanElement.setAttribute("classname", instance.getClassName());
78          MBeanInfo JavaDoc info = server.getMBeanInfo(instance.getObjectName());
79          mBeanElement.setAttribute("description", info.getDescription());
80          root.appendChild(mBeanElement);
81       }
82       return document;
83    }
84
85 }
86
Popular Tags