KickJava   Java API By Example, From Geeks To Geeks.

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


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
9 package mx4j.tools.adaptor.http;
10
11 import java.io.IOException JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14 import java.util.SortedMap JavaDoc;
15 import java.util.TreeMap JavaDoc;
16 import java.util.TreeSet JavaDoc;
17 import javax.management.JMException JavaDoc;
18 import javax.management.MBeanAttributeInfo JavaDoc;
19 import javax.management.MBeanConstructorInfo JavaDoc;
20 import javax.management.MBeanInfo JavaDoc;
21 import javax.management.MBeanNotificationInfo JavaDoc;
22 import javax.management.MBeanOperationInfo JavaDoc;
23 import javax.management.MBeanParameterInfo JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
26
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * MBeanCommandProcessor, processes a request for getting data about a MBean
32  *
33  * @version $Revision: 1.3 $
34  */

35 public class MBeanCommandProcessor extends HttpCommandProcessorAdaptor
36 {
37    public MBeanCommandProcessor()
38    {
39    }
40
41    public Document JavaDoc executeRequest(HttpInputStream in) throws IOException JavaDoc, JMException JavaDoc
42    {
43       Document JavaDoc document = builder.newDocument();
44
45       String JavaDoc name = in.getVariable("objectname");
46       ObjectName JavaDoc objectName = null;
47
48       if (name != null)
49       {
50          objectName = new ObjectName JavaDoc(name);
51          if (!objectName.isPattern())
52          {
53             // not a pattern - assume a single MBean
54
if (server.isRegistered(objectName))
55             {
56                Element JavaDoc mb = createMBeanElement(document, objectName, in);
57                document.appendChild(mb);
58             }
59          }
60          else
61          {
62             // A pattern - return all matching MBeans
63
Set JavaDoc names = new TreeSet JavaDoc(CommandProcessorUtil.createObjectNameComparator());
64             names.addAll(server.queryNames(objectName, null));
65             Element JavaDoc root = document.createElement("Server");
66             root.setAttribute("pattern", objectName.toString());
67             for (Iterator JavaDoc it = names.iterator(); it.hasNext();)
68             {
69                Element JavaDoc mb = createMBeanElement(document, (ObjectName JavaDoc)it.next(), in);
70                root.appendChild(mb);
71             }
72             document.appendChild(root);
73          }
74       }
75       return document;
76    }
77
78    private Element JavaDoc createMBeanElement(Document JavaDoc document, ObjectName JavaDoc objectName, HttpInputStream in)
79            throws JMException JavaDoc
80    {
81       Element JavaDoc root = document.createElement("MBean");
82
83       MBeanInfo JavaDoc info = server.getMBeanInfo(objectName);
84       root.setAttribute("description", info.getDescription());
85       root.setAttribute("classname", info.getClassName());
86       root.setAttribute("objectname", objectName.toString());
87
88       if (info instanceof ModelMBeanInfo JavaDoc)
89       {
90          root.setAttribute("model", "true");
91       }
92       if (HttpUtil.booleanVariableValue(in, "attributes", true))
93       {
94          MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
95          if (attributes != null)
96          {
97             SortedMap JavaDoc sortedAttributes = new TreeMap JavaDoc();
98             for (int i = 0; i < attributes.length; i++)
99             {
100                Element JavaDoc attribute = document.createElement("Attribute");
101                attribute.setAttribute("name", attributes[i].getName());
102                attribute.setAttribute("type", attributes[i].getType());
103                attribute.setAttribute("description", attributes[i].getDescription());
104                attribute.setAttribute("strinit", String.valueOf(CommandProcessorUtil.canCreateParameterValue(attributes[i].getType())));
105                if (attributes[i].isReadable() && attributes[i].isWritable())
106                {
107                   attribute.setAttribute("availability", "RW");
108                }
109                if (attributes[i].isReadable() && !attributes[i].isWritable())
110                {
111                   attribute.setAttribute("availability", "RO");
112                }
113                if (!attributes[i].isReadable() && attributes[i].isWritable())
114                {
115                   attribute.setAttribute("availability", "WO");
116                }
117                try
118                {
119                   Object JavaDoc attributeValue = server.getAttribute(objectName, attributes[i].getName());
120                   attribute.setAttribute("isnull", (attributeValue == null) ? "true" : "false");
121                   if (attributeValue != null)
122                   {
123                      attribute.setAttribute("value", attributeValue.toString());
124                      if (attributeValue.getClass().isArray())
125                      {
126                         attribute.setAttribute("aggregation", "array");
127                      }
128                      if (attributeValue instanceof java.util.Collection JavaDoc)
129                      {
130                         attribute.setAttribute("aggregation", "collection");
131                      }
132                      if (attributeValue instanceof java.util.Map JavaDoc)
133                      {
134                         attribute.setAttribute("aggregation", "map");
135                      }
136                   }
137                   else
138                   {
139                      attribute.setAttribute("value", "null");
140                   }
141
142                }
143                catch (JMException JavaDoc e)
144                {
145                   attribute.setAttribute("value", e.getMessage());
146                }
147                sortedAttributes.put(attributes[i].getName(), attribute);
148             }
149             Iterator JavaDoc keys = sortedAttributes.keySet().iterator();
150             while (keys.hasNext())
151             {
152                root.appendChild((Element JavaDoc)sortedAttributes.get(keys.next()));
153             }
154          }
155       }
156       if (HttpUtil.booleanVariableValue(in, "constructors", true))
157       {
158          MBeanConstructorInfo JavaDoc[] constructors = info.getConstructors();
159          if (constructors != null)
160          {
161             // How to order contructors?
162
for (int i = 0; i < constructors.length; i++)
163             {
164                Element JavaDoc constructor = document.createElement("Constructor");
165                constructor.setAttribute("name", constructors[i].getName());
166                constructor.setAttribute("description", constructors[i].getDescription());
167                addParameters(constructor, document, constructors[i].getSignature());
168                root.appendChild(constructor);
169             }
170          }
171       }
172       if (HttpUtil.booleanVariableValue(in, "operations", true))
173       {
174          MBeanOperationInfo JavaDoc[] operations = info.getOperations();
175          if (operations != null)
176          {
177             for (int i = 0; i < operations.length; i++)
178             {
179                Element JavaDoc operation = document.createElement("Operation");
180                operation.setAttribute("name", operations[i].getName());
181                operation.setAttribute("description", operations[i].getDescription());
182                operation.setAttribute("return", operations[i].getReturnType());
183                switch (operations[i].getImpact())
184                {
185                   case MBeanOperationInfo.UNKNOWN:
186                      operation.setAttribute("impact", "unknown");
187                      break;
188                   case MBeanOperationInfo.ACTION:
189                      operation.setAttribute("impact", "action");
190                      break;
191                   case MBeanOperationInfo.INFO:
192                      operation.setAttribute("impact", "info");
193                      break;
194                   case MBeanOperationInfo.ACTION_INFO:
195                      operation.setAttribute("impact", "action_info");
196                      break;
197                }
198                addParameters(operation, document, operations[i].getSignature());
199                root.appendChild(operation);
200             }
201          }
202       }
203       if (HttpUtil.booleanVariableValue(in, "notifications", true))
204       {
205          MBeanNotificationInfo JavaDoc[] notifications = info.getNotifications();
206          if (notifications != null)
207          {
208             for (int i = 0; i < notifications.length; i++)
209             {
210                Element JavaDoc notification = document.createElement("Notification");
211                notification.setAttribute("name", notifications[i].getName());
212                notification.setAttribute("description", notifications[i].getDescription());
213                String JavaDoc[] types = notifications[i].getNotifTypes();
214                for (int j = 0; j < types.length; j++)
215                {
216                   Element JavaDoc type = document.createElement("Type");
217                   type.setAttribute("name", types[j]);
218                   notification.appendChild(type);
219                }
220                root.appendChild(notification);
221             }
222          }
223       }
224       return root;
225    }
226
227    protected void addParameters(Element JavaDoc node, Document JavaDoc document, MBeanParameterInfo JavaDoc[] parameters)
228    {
229       for (int j = 0; j < parameters.length; j++)
230       {
231          Element JavaDoc parameter = document.createElement("Parameter");
232          parameter.setAttribute("name", parameters[j].getName());
233          parameter.setAttribute("description", parameters[j].getDescription());
234          parameter.setAttribute("type", parameters[j].getType());
235          parameter.setAttribute("strinit", String.valueOf(CommandProcessorUtil.canCreateParameterValue(parameters[j].getType())));
236          // add id since order is relevant
237
parameter.setAttribute("id", "" + j);
238          node.appendChild(parameter);
239       }
240    }
241
242 }
243
Popular Tags