KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Array JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import javax.management.JMException JavaDoc;
17 import javax.management.MBeanAttributeInfo JavaDoc;
18 import javax.management.MBeanInfo JavaDoc;
19 import javax.management.ObjectName JavaDoc;
20
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.Element JavaDoc;
23
24 /**
25  * GetAttributeCommandProcessor, processes a request for getting one attribute
26  * of a specific MBean. It also support some formats for types like Arrays
27  *
28  * @version $Revision: 1.4 $
29  */

30 public class GetAttributeCommandProcessor extends HttpCommandProcessorAdaptor
31 {
32    public GetAttributeCommandProcessor()
33    {
34    }
35
36    public Document JavaDoc executeRequest(HttpInputStream in) throws IOException JavaDoc, JMException JavaDoc
37    {
38       Document JavaDoc document = builder.newDocument();
39
40       String JavaDoc name = in.getVariable("objectname");
41       String JavaDoc attributeVariable = in.getVariable("attribute");
42       String JavaDoc formatVariable = in.getVariable("format");
43       ObjectName JavaDoc objectName = null;
44       MBeanAttributeInfo JavaDoc targetAttribute = null;
45       // special case
46
boolean validMBean = false;
47       if (name != null)
48       {
49          objectName = new ObjectName JavaDoc(name);
50          if (server.isRegistered(objectName))
51          {
52             validMBean = true;
53          }
54       }
55       if (validMBean && attributeVariable != null)
56       {
57          validMBean = false;
58          MBeanInfo JavaDoc info = server.getMBeanInfo(objectName);
59          MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
60
61          if (attributes != null)
62          {
63             for (int i = 0; i < attributes.length; i++)
64             {
65                if (attributes[i].getName().equals(attributeVariable))
66                {
67                   targetAttribute = attributes[i];
68                   validMBean = true;
69                   break;
70                }
71             }
72          }
73       }
74       if (validMBean)
75       {
76          Element JavaDoc root = document.createElement("MBean");
77          document.appendChild(root);
78
79          root.setAttribute("objectname", objectName.toString());
80          MBeanInfo JavaDoc info = server.getMBeanInfo(objectName);
81          root.setAttribute("classname", info.getClassName());
82          root.setAttribute("description", info.getDescription());
83
84          Element JavaDoc attribute = document.createElement("Attribute");
85          attribute.setAttribute("name", attributeVariable);
86          attribute.setAttribute("classname", targetAttribute.getType());
87          Object JavaDoc attributeValue = server.getAttribute(objectName, attributeVariable);
88          attribute.setAttribute("isnull", (attributeValue == null) ? "true" : "false");
89          root.appendChild(attribute);
90
91          if ("array".equals(formatVariable) && attributeValue.getClass().isArray())
92          {
93             Element JavaDoc array = document.createElement("Array");
94             array.setAttribute("componentclass", attributeValue.getClass().getComponentType().getName());
95             int length = Array.getLength(attributeValue);
96             array.setAttribute("length", "" + length);
97             for (int i = 0; i < length; i++)
98             {
99                Element JavaDoc arrayElement = document.createElement("Element");
100                arrayElement.setAttribute("index", "" + i);
101                if (Array.get(attributeValue, i) != null)
102                {
103                   arrayElement.setAttribute("element", Array.get(attributeValue, i).toString());
104                   arrayElement.setAttribute("isnull", "false");
105                }
106                else
107                {
108                   arrayElement.setAttribute("element", "null");
109                   arrayElement.setAttribute("isnull", "true");
110                }
111                array.appendChild(arrayElement);
112             }
113             attribute.appendChild(array);
114          }
115          else if ("collection".equals(formatVariable) && attributeValue instanceof Collection JavaDoc)
116          {
117             Collection JavaDoc collection = (Collection JavaDoc)attributeValue;
118             Element JavaDoc collectionElement = document.createElement("Collection");
119             collectionElement.setAttribute("length", "" + collection.size());
120             Iterator JavaDoc i = collection.iterator();
121             int j = 0;
122             while (i.hasNext())
123             {
124                Element JavaDoc collectionEntry = document.createElement("Element");
125                collectionEntry.setAttribute("index", "" + j++);
126                Object JavaDoc obj = i.next();
127                if (obj != null)
128                {
129                   collectionEntry.setAttribute("elementclass", obj.getClass().getName());
130                   collectionEntry.setAttribute("element", obj.toString());
131                }
132                else
133                {
134                   collectionEntry.setAttribute("elementclass", "null");
135                   collectionEntry.setAttribute("element", "null");
136                }
137                collectionElement.appendChild(collectionEntry);
138             }
139             attribute.appendChild(collectionElement);
140          }
141          else if ("map".equals(formatVariable) && attributeValue instanceof Map JavaDoc)
142          {
143             Map JavaDoc map = (Map JavaDoc)attributeValue;
144             Element JavaDoc mapElement = document.createElement("Map");
145             mapElement.setAttribute("length", "" + map.size());
146             Iterator JavaDoc i = map.keySet().iterator();
147             int j = 0;
148             while (i.hasNext())
149             {
150                Element JavaDoc mapEntry = document.createElement("Element");
151                mapEntry.setAttribute("index", "" + j++);
152                Object JavaDoc key = i.next();
153                Object JavaDoc entry = map.get(key);
154                if (entry != null && key != null)
155                {
156                   mapEntry.setAttribute("keyclass", key.getClass().getName());
157                   mapEntry.setAttribute("key", key.toString());
158                   mapEntry.setAttribute("elementclass", entry.getClass().getName());
159                   mapEntry.setAttribute("element", entry.toString());
160                }
161                else
162                {
163                   mapEntry.setAttribute("keyclass", "null");
164                   mapEntry.setAttribute("key", "null");
165                   mapEntry.setAttribute("elementclass", "null");
166                   mapEntry.setAttribute("element", "null");
167                }
168                mapElement.appendChild(mapEntry);
169             }
170             attribute.appendChild(mapElement);
171          }
172          else
173          {
174             attribute.setAttribute("value", (attributeValue == null) ? "null" : attributeValue.toString());
175          }
176
177       }
178       return document;
179    }
180 }
181
Popular Tags