1 8 package mx4j.tools.adaptor.http; 9 10 import java.io.IOException ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import javax.management.JMException ; 18 import javax.management.MBeanServerInvocationHandler ; 19 import javax.management.ObjectInstance ; 20 import javax.management.ObjectName ; 21 import javax.management.relation.RelationServiceMBean ; 22 import javax.management.relation.RoleInfo ; 23 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.Text ; 27 28 34 public class RelationCommandProcessor extends HttpCommandProcessorAdaptor 35 { 36 private ObjectName m_relationObjectName = null; 37 private RelationServiceMBean m_proxy = null; 38 39 public RelationCommandProcessor() 40 { 41 } 42 43 public Document executeRequest(HttpInputStream in) throws IOException , JMException 44 { 45 Document document = builder.newDocument(); 46 Element root = document.createElement("RelationServer"); 47 document.appendChild(root); 48 49 if (!checkRelationServiceIsRegistered()) 51 { 52 Element defaultElement = document.createElement("default"); 53 Text defaultNode = document.createTextNode("RelationService is not registered!"); 54 defaultElement.appendChild(defaultNode); 55 root.appendChild(defaultElement); 56 return document; 57 } 58 m_proxy = (RelationServiceMBean )MBeanServerInvocationHandler.newProxyInstance(server, m_relationObjectName, RelationServiceMBean .class, true); 59 60 List allRelationNames = m_proxy.getAllRelationTypeNames(); 62 addRelationTypeNames(root, document, allRelationNames); 63 return document; 64 } 65 66 70 protected void addRelationTypeNames(Element node, Document document, List allRelationNames) throws JMException 71 { 72 Map namesMap = new HashMap (); 73 Iterator i = allRelationNames.iterator(); 74 while (i.hasNext()) 75 { 76 String name = (String )i.next(); 77 List values = m_proxy.findRelationsOfType(name); 78 if (namesMap.containsKey(name)) 79 { 80 81 ((List )namesMap.get(name)).add(values); 82 } 83 else 84 { 85 namesMap.put(name, values); 86 } 87 } 88 i = namesMap.keySet().iterator(); 90 while (i.hasNext()) 91 { 92 String relationName = (String )i.next(); 93 Element typeNameElement = document.createElement("relation-type-name"); 94 typeNameElement.setAttribute("name", relationName); 95 node.appendChild(typeNameElement); 96 97 List infos = m_proxy.getRoleInfos(relationName); 98 Iterator x = infos.iterator(); 99 while (x.hasNext()) 101 { 102 RoleInfo roleInfo = (RoleInfo )x.next(); 104 String roleName = roleInfo.getName(); 105 String mbeanClassName = roleInfo.getRefMBeanClassName(); 106 String 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 Element roleInfoElement = document.createElement("relation-meta-data"); 114 115 Element roleNameElement = document.createElement("role-name"); 117 Text roleNameNode = document.createTextNode(roleName); 118 roleNameElement.appendChild(roleNameNode); 119 roleInfoElement.appendChild(roleNameElement); 120 121 Element mbeanClassElement = document.createElement("mbean-classname"); 123 Text mbeanClassNode = document.createTextNode(mbeanClassName); 124 mbeanClassElement.appendChild(mbeanClassNode); 125 roleInfoElement.appendChild(mbeanClassElement); 126 127 Element descriptionElement = document.createElement("description"); 129 if (description == null) description = "no description"; 130 Text descriptionNode = document.createTextNode(description); 131 descriptionElement.appendChild(descriptionNode); 132 roleInfoElement.appendChild(descriptionElement); 133 134 Element minDegreeElement = document.createElement("min-degree"); 136 Text minDegreeNode = document.createTextNode(Integer.toString(minDegree)); 137 minDegreeElement.appendChild(minDegreeNode); 138 roleInfoElement.appendChild(minDegreeElement); 139 140 Element maxDegreeElement = document.createElement("max-degree"); 141 Text maxDegreeNode = document.createTextNode(Integer.toString(maxDegree)); 142 maxDegreeElement.appendChild(maxDegreeNode); 143 roleInfoElement.appendChild(maxDegreeElement); 144 145 Element readingElement = document.createElement("is-readable"); 146 String readable = null; 147 if (reading) 148 readable = "true"; 149 else 150 readable = "false"; 151 Text readingNode = document.createTextNode(readable); 152 readingElement.appendChild(readingNode); 153 roleInfoElement.appendChild(readingElement); 154 155 Element writingElement = document.createElement("is-writable"); 156 String writable = null; 157 if (writing) 158 writable = "true"; 159 else 160 writable = "false"; 161 Text writingNode = document.createTextNode(writable); 162 writingElement.appendChild(writingNode); 163 roleInfoElement.appendChild(writingElement); 164 165 typeNameElement.appendChild(roleInfoElement); 166 167 } 168 List references = (List )namesMap.get(relationName); 170 Iterator j = references.iterator(); 171 while (j.hasNext()) 172 { 173 String relationId = (String )j.next(); 174 Element idElement = document.createElement("relation-id"); 175 Text idNode = document.createTextNode(relationId); 176 idElement.appendChild(idNode); 177 typeNameElement.appendChild(idElement); 178 } 179 } 180 } 181 182 protected boolean checkRelationServiceIsRegistered() 184 { 185 Set allMBeans = server.queryMBeans(null, null); 186 for (Iterator i = allMBeans.iterator(); i.hasNext();) 187 { 188 ObjectInstance instance = (ObjectInstance )i.next(); 189 if (instance.getClassName().equals("javax.management.relation.RelationService")) 190 { 191 m_relationObjectName = instance.getObjectName(); 193 return true; 194 } 195 } 196 return false; 197 } 198 } 199 | Popular Tags |