1 25 26 package org.objectweb.easybeans.jmx; 27 28 import java.io.InputStream ; 29 30 import org.apache.commons.modeler.AttributeInfo; 31 import org.apache.commons.modeler.ConstructorInfo; 32 import org.apache.commons.modeler.FieldInfo; 33 import org.apache.commons.modeler.ManagedBean; 34 import org.apache.commons.modeler.NotificationInfo; 35 import org.apache.commons.modeler.OperationInfo; 36 import org.apache.commons.modeler.ParameterInfo; 37 import org.apache.commons.modeler.Registry; 38 import org.apache.commons.modeler.util.DomUtil; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.NamedNodeMap ; 41 import org.w3c.dom.Node ; 42 43 47 public final class CommonsModelerExtension { 48 49 52 private CommonsModelerExtension() { } 53 54 60 public static void updateDescriptors(final Registry registry, final InputStream stream) throws Exception { 61 62 try { 63 Document doc = DomUtil.readXml(stream); 64 Node descriptorsN = doc.getDocumentElement(); 66 if (descriptorsN == null) { 68 return; 69 } 70 71 Node firstMbeanN = null; 72 if ("mbean".equals(descriptorsN.getNodeName())) { 73 firstMbeanN = descriptorsN; 74 } else { 75 firstMbeanN = DomUtil.getChild(descriptorsN, "mbean"); 76 } 77 78 if (firstMbeanN == null) { 79 return; 80 } 81 82 for (Node mbeanN = firstMbeanN; mbeanN != null; mbeanN = DomUtil.getNext(mbeanN)) { 84 85 processMBeanNode(registry, mbeanN); 86 } 87 88 } catch (Exception ex) { 89 } 91 } 92 93 97 private static void processMBeanNode(final Registry registry, final Node mbeanNode) { 98 NamedNodeMap attrs = mbeanNode.getAttributes(); 101 Node n = attrs.getNamedItem("name"); 102 String mbeanName = n.getNodeValue(); 103 104 ManagedBean managed = registry.findManagedBean(mbeanName); 106 108 DomUtil.setAttributes(managed, mbeanNode); 109 110 processMBeanDescriptorNode(managed, mbeanNode); 112 113 processMBeanAttributeNode(managed, mbeanNode); 115 116 processMBeanConstructorNode(managed, mbeanNode); 118 119 processMBeanNotificationNode(managed, mbeanNode); 121 122 processMBeanOperationNode(managed, mbeanNode); 124 } 125 126 130 private static void processMBeanOperationNode(final ManagedBean managed, final Node mbeanNode) { 131 Node firstN; 132 firstN = DomUtil.getChild(mbeanNode, "operation"); 133 for (Node descN = firstN; descN != null; descN = DomUtil.getNext(descN)) { 134 135 OperationInfo oi = new OperationInfo(); 137 DomUtil.setAttributes(oi, descN); 138 139 Node firstDescriptorN = DomUtil.getChild(descN, "descriptor"); 141 if (firstDescriptorN != null) { 142 Node firstFieldN = DomUtil.getChild(firstDescriptorN, "field"); 143 for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil.getNext(fieldN)) { 144 FieldInfo fi = new FieldInfo(); 145 DomUtil.setAttributes(fi, fieldN); 146 oi.addField(fi); 147 } 148 } 149 150 Node firstParamN = DomUtil.getChild(descN, "parameter"); 152 for (Node paramN = firstParamN; paramN != null; paramN = DomUtil.getNext(paramN)) { 153 ParameterInfo pi = new ParameterInfo(); 154 DomUtil.setAttributes(pi, paramN); 155 oi.addParameter(pi); 156 } 157 158 managed.addOperation(oi); 160 161 } 162 } 163 164 168 private static void processMBeanNotificationNode(final ManagedBean managed, final Node mbeanNode) { 169 Node firstN; 170 firstN = DomUtil.getChild(mbeanNode, "notification"); 171 for (Node descN = firstN; descN != null; descN = DomUtil .getNext(descN)) { 172 173 NotificationInfo ni = new NotificationInfo(); 175 DomUtil.setAttributes(ni, descN); 176 177 Node firstDescriptorN = DomUtil.getChild(descN, "descriptor"); 179 if (firstDescriptorN != null) { 180 Node firstFieldN = DomUtil.getChild(firstDescriptorN, "field"); 181 for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil.getNext(fieldN)) { 182 FieldInfo fi = new FieldInfo(); 183 DomUtil.setAttributes(fi, fieldN); 184 ni.addField(fi); 185 } 186 } 187 188 Node firstParamN = DomUtil.getChild(descN, "notification-type"); 190 for (Node paramN = firstParamN; paramN != null; paramN = DomUtil.getNext(paramN)) { 191 ni.addNotifType(DomUtil.getContent(paramN)); 192 } 193 194 managed.addNotification(ni); 196 197 } 198 } 199 200 204 private static void processMBeanConstructorNode(final ManagedBean managed, final Node mbeanNode) { 205 Node firstN; 206 firstN = DomUtil.getChild(mbeanNode, "constructor"); 207 for (Node descN = firstN; descN != null; descN = DomUtil.getNext(descN)) { 208 209 ConstructorInfo ci = new ConstructorInfo(); 211 DomUtil.setAttributes(ci, descN); 212 213 Node firstDescriptorN = DomUtil.getChild(descN, "descriptor"); 215 if (firstDescriptorN != null) { 216 Node firstFieldN = DomUtil.getChild(firstDescriptorN, "field"); 217 for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil.getNext(fieldN)) { 218 FieldInfo fi = new FieldInfo(); 219 DomUtil.setAttributes(fi, fieldN); 220 ci.addField(fi); 221 } 222 } 223 224 Node firstParamN = DomUtil.getChild(descN, "parameter"); 226 for (Node paramN = firstParamN; paramN != null; paramN = DomUtil.getNext(paramN)) { 227 ParameterInfo pi = new ParameterInfo(); 228 DomUtil.setAttributes(pi, paramN); 229 ci.addParameter(pi); 230 } 231 232 managed.addConstructor(ci); 234 235 } 236 } 237 238 242 private static void processMBeanAttributeNode(final ManagedBean managed, final Node mbeanNode) { 243 Node firstN; 244 firstN = DomUtil.getChild(mbeanNode, "attribute"); 245 for (Node descN = firstN; descN != null; descN = DomUtil.getNext(descN)) { 246 247 AttributeInfo ai = new AttributeInfo(); 249 DomUtil.setAttributes(ai, descN); 250 251 Node descriptorN = DomUtil.getChild(descN, "descriptor"); 253 if (descriptorN != null) { 254 Node firstFieldN = DomUtil.getChild(descriptorN, "field"); 255 for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil.getNext(fieldN)) { 256 FieldInfo fi = new FieldInfo(); 257 DomUtil.setAttributes(fi, fieldN); 258 ai.addField(fi); 259 } 260 } 261 262 managed.addAttribute(ai); 264 265 } 266 } 267 268 272 private static void processMBeanDescriptorNode(final ManagedBean managed, final Node mbeanNode) { 273 Node mbeanDescriptorN = DomUtil.getChild(mbeanNode, "descriptor"); 274 if (mbeanDescriptorN != null) { 275 Node firstFieldN = DomUtil.getChild(mbeanDescriptorN, "field"); 276 for (Node fieldN = firstFieldN; fieldN != null; fieldN = DomUtil.getNext(fieldN)) { 277 FieldInfo fi = new FieldInfo(); 278 DomUtil.setAttributes(fi, fieldN); 279 managed.addField(fi); 280 } 281 } 282 } 283 } 284 | Popular Tags |