KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import javax.management.JMException JavaDoc;
18 import javax.management.MBeanServerInvocationHandler JavaDoc;
19 import javax.management.ObjectInstance JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21 import javax.management.relation.RelationServiceMBean JavaDoc;
22 import javax.management.relation.RoleInfo JavaDoc;
23
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Text JavaDoc;
27
28 /**
29  * CreateMBeanCommandProcessor, processes a request for creating and registering
30  * an MBean
31  *
32  * @version $Revision: 1.4 $
33  */

34 public class RelationCommandProcessor extends HttpCommandProcessorAdaptor
35 {
36    private ObjectName JavaDoc m_relationObjectName = null;
37    private RelationServiceMBean JavaDoc m_proxy = null;
38
39    public RelationCommandProcessor()
40    {
41    }
42
43    public Document JavaDoc executeRequest(HttpInputStream in) throws IOException JavaDoc, JMException JavaDoc
44    {
45       Document JavaDoc document = builder.newDocument();
46       Element JavaDoc root = document.createElement("RelationServer");
47       document.appendChild(root);
48
49       // check relationService is registered by finding its class if not return a default message
50
if (!checkRelationServiceIsRegistered())
51       {
52          Element JavaDoc defaultElement = document.createElement("default");
53          Text JavaDoc defaultNode = document.createTextNode("RelationService is not registered!");
54          defaultElement.appendChild(defaultNode);
55          root.appendChild(defaultElement);
56          return document;
57       }
58       m_proxy = (RelationServiceMBean JavaDoc)MBeanServerInvocationHandler.newProxyInstance(server, m_relationObjectName, RelationServiceMBean JavaDoc.class, true);
59
60       // get all the defined relationTypeNames
61
List JavaDoc allRelationNames = m_proxy.getAllRelationTypeNames();
62       addRelationTypeNames(root, document, allRelationNames);
63       return document;
64    }
65
66    /**
67     * put everything into a map as we need to maintain the correct relationTypeName to relationId
68     * TODO: determine how to make relationService tab useful!!
69     */

70    protected void addRelationTypeNames(Element JavaDoc node, Document JavaDoc document, List JavaDoc allRelationNames) throws JMException JavaDoc
71    {
72       Map JavaDoc namesMap = new HashMap JavaDoc();
73       Iterator JavaDoc i = allRelationNames.iterator();
74       while (i.hasNext())
75       {
76          String JavaDoc name = (String JavaDoc)i.next();
77          List JavaDoc values = m_proxy.findRelationsOfType(name);
78          if (namesMap.containsKey(name))
79          {
80
81             ((List JavaDoc)namesMap.get(name)).add(values);
82          }
83          else
84          {
85             namesMap.put(name, values);
86          }
87       }
88       // loop through the map assigning keys to Elements: values to child elements
89
i = namesMap.keySet().iterator();
90       while (i.hasNext())
91       {
92          String JavaDoc relationName = (String JavaDoc)i.next();
93          Element JavaDoc typeNameElement = document.createElement("relation-type-name");
94          typeNameElement.setAttribute("name", relationName);
95          node.appendChild(typeNameElement);
96
97          List JavaDoc infos = m_proxy.getRoleInfos(relationName);
98          Iterator JavaDoc x = infos.iterator();
99          // build the metadata for the relationTypeName
100
while (x.hasNext())
101          {
102             // get the Relation meta-data for specified RelationTypeName
103
RoleInfo JavaDoc roleInfo = (RoleInfo JavaDoc)x.next();
104             String JavaDoc roleName = roleInfo.getName();
105             String JavaDoc mbeanClassName = roleInfo.getRefMBeanClassName();
106             String JavaDoc description = roleInfo.getDescription();
107             int minDegree = roleInfo.getMinDegree();
108             int maxDegree = roleInfo.getMaxDegree();
109             boolean reading = roleInfo.isReadable();
110             boolean writing = roleInfo.isWritable();
111
112             // creating the element relation-meta-data
113
Element JavaDoc roleInfoElement = document.createElement("relation-meta-data");
114
115             // create the roleName element and text node child of relation-meta-data
116
Element JavaDoc roleNameElement = document.createElement("role-name");
117             Text JavaDoc roleNameNode = document.createTextNode(roleName);
118             roleNameElement.appendChild(roleNameNode);
119             roleInfoElement.appendChild(roleNameElement);
120
121             // create the mbeanclass element child of relation-meta-data
122
Element JavaDoc mbeanClassElement = document.createElement("mbean-classname");
123             Text JavaDoc mbeanClassNode = document.createTextNode(mbeanClassName);
124             mbeanClassElement.appendChild(mbeanClassNode);
125             roleInfoElement.appendChild(mbeanClassElement);
126
127             // create the description element child of relation-meta-data
128
Element JavaDoc descriptionElement = document.createElement("description");
129             if (description == null) description = "no description";
130             Text JavaDoc descriptionNode = document.createTextNode(description);
131             descriptionElement.appendChild(descriptionNode);
132             roleInfoElement.appendChild(descriptionElement);
133
134             // create text node for min element
135
Element JavaDoc minDegreeElement = document.createElement("min-degree");
136             Text JavaDoc minDegreeNode = document.createTextNode(Integer.toString(minDegree));
137             minDegreeElement.appendChild(minDegreeNode);
138             roleInfoElement.appendChild(minDegreeElement);
139
140             Element JavaDoc maxDegreeElement = document.createElement("max-degree");
141             Text JavaDoc maxDegreeNode = document.createTextNode(Integer.toString(maxDegree));
142             maxDegreeElement.appendChild(maxDegreeNode);
143             roleInfoElement.appendChild(maxDegreeElement);
144
145             Element JavaDoc readingElement = document.createElement("is-readable");
146             String JavaDoc readable = null;
147             if (reading)
148                readable = "true";
149             else
150                readable = "false";
151             Text JavaDoc readingNode = document.createTextNode(readable);
152             readingElement.appendChild(readingNode);
153             roleInfoElement.appendChild(readingElement);
154
155             Element JavaDoc writingElement = document.createElement("is-writable");
156             String JavaDoc writable = null;
157             if (writing)
158                writable = "true";
159             else
160                writable = "false";
161             Text JavaDoc writingNode = document.createTextNode(writable);
162             writingElement.appendChild(writingNode);
163             roleInfoElement.appendChild(writingElement);
164
165             typeNameElement.appendChild(roleInfoElement);
166
167          }
168          // get the list of relationIds (the values of the Map)
169
List JavaDoc references = (List JavaDoc)namesMap.get(relationName);
170          Iterator JavaDoc j = references.iterator();
171          while (j.hasNext())
172          {
173             String JavaDoc relationId = (String JavaDoc)j.next();
174             Element JavaDoc idElement = document.createElement("relation-id");
175             Text JavaDoc idNode = document.createTextNode(relationId);
176             idElement.appendChild(idNode);
177             typeNameElement.appendChild(idElement);
178          }
179       }
180    }
181
182    // checks the relationService is registered
183
protected boolean checkRelationServiceIsRegistered()
184    {
185       Set JavaDoc allMBeans = server.queryMBeans(null, null);
186       for (Iterator JavaDoc i = allMBeans.iterator(); i.hasNext();)
187       {
188          ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc)i.next();
189          if (instance.getClassName().equals("javax.management.relation.RelationService"))
190          {
191             // create a reference the the RelationService ObjetcName in order to create the proxy
192
m_relationObjectName = instance.getObjectName();
193             return true;
194          }
195       }
196       return false;
197    }
198 }
199
Popular Tags