1 22 package org.jboss.mx.modelmbean; 23 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import javax.management.Descriptor ; 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.ListenerNotFoundException ; 33 import javax.management.MBeanException ; 34 import javax.management.MBeanInfo ; 35 import javax.management.MBeanNotificationInfo ; 36 import javax.management.MBeanServer ; 37 import javax.management.NotCompliantMBeanException ; 38 import javax.management.NotificationBroadcaster ; 39 import javax.management.Notification ; 40 import javax.management.NotificationFilter ; 41 import javax.management.NotificationListener ; 42 import javax.management.ObjectName ; 43 import javax.management.RuntimeOperationsException ; 44 import javax.management.StandardMBean ; 45 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 46 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 47 import javax.management.modelmbean.ModelMBeanConstructorInfo ; 48 import javax.management.modelmbean.ModelMBeanInfo ; 49 import javax.management.modelmbean.ModelMBeanInfoSupport ; 50 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 51 import javax.management.modelmbean.ModelMBeanOperationInfo ; 52 import javax.xml.parsers.DocumentBuilder ; 53 import javax.xml.parsers.DocumentBuilderFactory ; 54 import javax.xml.parsers.ParserConfigurationException ; 55 56 import org.jboss.mx.interceptor.StandardMBeanInfoInterceptor; 57 import org.jboss.mx.metadata.MBeanInfoConversion; 58 import org.jboss.mx.metadata.MetaDataBuilder; 59 import org.jboss.mx.metadata.StandardMetaData; 60 import org.jboss.mx.metadata.XMLMetaData; 61 62 70 public class XMBean 71 extends ModelMBeanInvoker 72 implements XMBeanConstants, NotificationListener 73 { 74 75 77 81 public XMBean() throws MBeanException 82 { 83 try 84 { 85 setManagedResource(new Object (), OBJECT_REF); 86 setModelMBeanInfo(new ModelMBeanInfoSupport ("XMBean", "Uninitialized XMBean", new ModelMBeanAttributeInfo [0], 87 new ModelMBeanConstructorInfo [0], new ModelMBeanOperationInfo [0], new ModelMBeanNotificationInfo [0])); 88 } 89 catch (RuntimeException e) 90 { 91 throw new RuntimeOperationsException (e); 92 } 93 catch (Exception e) 94 { 95 throw new MBeanException (e); 96 } 97 } 98 99 105 public XMBean(ModelMBeanInfo info) throws MBeanException 106 { 107 super(info); 108 } 109 110 158 public XMBean(Object resource, String resourceType) throws MBeanException , NotCompliantMBeanException 159 { 160 162 ModelMBeanInfo minfo = null; 163 try 164 { 165 HashMap properties = new HashMap (); 166 167 if (resourceType.equals(DESCRIPTOR)) 168 { 169 Descriptor d = (Descriptor )resource; 170 171 resourceType = (String )d.getFieldValue(RESOURCE_TYPE); 173 174 resource = d.getFieldValue(RESOURCE_REFERENCE); 176 177 String [] fields = d.getFieldNames(); 179 180 for (int i = 0; i < fields.length; ++i) 181 { 182 if (fields[i].startsWith(METADATA_DESCRIPTOR_PREFIX)) 185 properties.put(fields[i], d.getFieldValue(fields[i])); 186 } 187 } 188 189 if (resourceType.equals(STANDARD_MBEAN) && resource instanceof StandardMBean ) 190 setManagedResource(((StandardMBean )resource).getImplementation(), resourceType); 191 else 192 setManagedResource(resource, resourceType); 193 194 final boolean CREATE_ATTRIBUTE_OPERATION_MAPPING = true; 197 198 if (resourceType.equals(STANDARD_MBEAN) && 200 resource instanceof StandardMBean ) 201 { 202 StandardMBean standardMBean = (StandardMBean ) resource; 203 minfo = MBeanInfoConversion.toModelMBeanInfo(standardMBean.getMBeanInfo(), 204 CREATE_ATTRIBUTE_OPERATION_MAPPING); 205 } 206 207 else if ((resourceType.equals(STANDARD_INTERFACE)) || 209 (resourceType.equals(STANDARD_MBEAN))) 210 { 211 dynamicResource = false; 212 213 MetaDataBuilder builder = new StandardMetaData(resource); 215 216 for (Iterator it = properties.keySet().iterator(); it.hasNext();) 218 { 219 String key = (String )it.next(); 220 builder.setProperty(key, properties.get(key)); 221 } 222 223 MBeanInfo standardInfo = builder.build(); 225 226 minfo = MBeanInfoConversion.toModelMBeanInfo(standardInfo, CREATE_ATTRIBUTE_OPERATION_MAPPING); 230 } 231 232 else if (resourceType.endsWith(".xml")) 235 { 236 240 MetaDataBuilder builder = new XMLMetaData( 241 this.getClass().getName(), resource.getClass().getName(), resourceType 244 ); 245 246 for (Iterator it = properties.keySet().iterator(); it.hasNext();) 248 { 249 String key = (String )it.next(); 250 builder.setProperty(key, properties.get(key)); 251 } 252 253 minfo = (ModelMBeanInfo ) builder.build(); 254 } 255 this.setModelMBeanInfo(minfo); 257 258 load(); 261 } 262 catch (InstanceNotFoundException e) 263 { 264 throw new MBeanException (e); 265 } 266 catch (InvalidTargetObjectTypeException e) 267 { 268 if (resourceType.endsWith(".xml")) 269 throw new MBeanException (e, "Malformed URL: " + resourceType); 270 271 throw new MBeanException (e, "Unsupported resource type: " + resourceType); 272 } 273 catch (MalformedURLException e) 274 { 275 throw new MBeanException (e, "Malformed URL: " + resourceType); 276 } 277 } 278 279 280 public XMBean(Object resource, URL interfaceURL) throws MBeanException , NotCompliantMBeanException 281 { 282 this(resource, interfaceURL.toString()); 283 } 284 285 286 public XMBean(Descriptor descriptor) throws MBeanException , NotCompliantMBeanException 287 { 288 this(descriptor, DESCRIPTOR); 289 } 290 291 public XMBean(Object resource, org.w3c.dom.Element element, String version) throws MBeanException , NotCompliantMBeanException 292 { 293 try 294 { 295 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 296 org.w3c.dom.Document doc = builder.newDocument(); 297 doc.appendChild(doc.importNode(element, true)); 298 299 org.dom4j.io.DOMReader domReader = new org.dom4j.io.DOMReader(); 300 org.dom4j.Document dom4jDoc = domReader.read(doc); 301 org.dom4j.Element dom4jElem = dom4jDoc.getRootElement(); 302 dom4jElem.detach(); 303 createXMBean(resource, dom4jElem, version); 304 } 305 catch (ParserConfigurationException e) 306 { 307 throw new MBeanException (e, "Could not convert w3c Element to dom4j Element."); 308 } 309 310 } 311 312 public XMBean(Object resource, org.dom4j.Element element, String version) throws MBeanException , NotCompliantMBeanException 313 { 314 createXMBean(resource, element, version); 316 317 } 318 319 private void createXMBean(Object resource, org.dom4j.Element element, String version) 320 throws MBeanException , NotCompliantMBeanException 321 { 322 try 323 { 324 setManagedResource(resource, OBJECT_REF); 325 MetaDataBuilder builder = new XMLMetaData( 326 this.getClass().getName(), resource.getClass().getName(), element, 329 version 330 ); 331 332 ModelMBeanInfo minfo = (ModelMBeanInfo ) builder.build(); 333 this.setModelMBeanInfo(minfo); 334 } 335 catch (InstanceNotFoundException e) 336 { 337 throw new MBeanException (e); 338 } 339 catch (InvalidTargetObjectTypeException e) 340 { 341 throw new MBeanException (e, "Unsupported resource type: " + resourceType); 342 } 343 344 } 345 346 347 349 public boolean isSupportedResourceType(Object resource, String resourceType) 350 { 351 if (resourceType == null) 352 return false; 353 354 if (resourceType.equalsIgnoreCase(OBJECT_REF)) 355 return true; 356 if (resourceType.equalsIgnoreCase(STANDARD_INTERFACE)) 357 return true; 358 if (resourceType.equalsIgnoreCase(STANDARD_MBEAN)) 359 return true; 360 if (resourceType.equalsIgnoreCase(DESCRIPTOR)) 361 { 362 if (resource == null || !(resource instanceof Descriptor )) 363 return false; 364 365 Descriptor d = (Descriptor )resource; 366 367 if (d.getFieldValue(RESOURCE_REFERENCE) == null) 368 return false; 369 370 if (d.getFieldValue(RESOURCE_TYPE) == null) 371 return false; 372 373 return true; 374 } 375 if (resourceType.endsWith(".xml")) 376 { 377 try 378 { 379 new URL (resourceType); 380 return true; 381 } 382 catch (MalformedURLException e) 383 { 384 return false; 385 } 386 } 387 388 return false; 389 } 390 391 392 394 protected void configureInterceptorStack(ModelMBeanInfo info, 395 MBeanServer server, ObjectName name) 396 throws Exception 397 { 398 400 super.configureInterceptorStack(info, server, name); 401 402 if (resourceType.equals(STANDARD_MBEAN)) 403 { 404 List interceptors = getMBeanInfoCtx.getInterceptors(); 405 interceptors.add(0, new StandardMBeanInfoInterceptor()); 406 getMBeanInfoCtx.setInterceptors(interceptors); 407 } 408 } 409 410 412 414 public void addNotificationListener(NotificationListener listener, 415 NotificationFilter filter, Object handback) 416 { 417 if (resourceType.equals(STANDARD_MBEAN)) 419 { 420 addNotificationListenerToResource(listener, filter, handback); 421 } 422 else 423 { 424 super.addNotificationListener(listener, filter, handback); 427 428 if (getResource() instanceof NotificationBroadcaster ) 430 addNotificationListenerToResource(listener, filter, handback); 431 } 432 } 433 434 public void removeNotificationListener(NotificationListener listener) 435 throws ListenerNotFoundException 436 { 437 if (resourceType.equals(STANDARD_MBEAN)) 439 { 440 removeNotificationListenerFromResource(listener); 441 } 442 else 443 { 444 super.removeNotificationListener(listener); 447 448 if (getResource() instanceof NotificationBroadcaster ) 450 removeNotificationListenerFromResource(listener); 451 } 452 } 453 454 public void removeNotificationListener(NotificationListener listener, 455 NotificationFilter filter, 456 Object handback) 457 throws ListenerNotFoundException 458 { 459 if (resourceType.equals(STANDARD_MBEAN)) 461 { 462 removeNotificationListenerFromResource(listener, filter, handback); 463 } 464 else 465 { 466 super.removeNotificationListener(listener, filter, handback); 469 470 if (getResource() instanceof NotificationBroadcaster ) 472 removeNotificationListenerFromResource(listener, filter, handback); 473 } 474 } 475 476 public MBeanNotificationInfo [] getNotificationInfo() 477 { 478 if (resourceType.equals(STANDARD_MBEAN)) 479 return getNotificationInfoFromResource(); 480 else 481 return super.getNotificationInfo(); 482 } 483 484 486 495 public void handleNotification(Notification notification, Object handback) 496 { 497 Object resource = getResource(); 498 499 if (resource instanceof NotificationListener ) 500 ((NotificationListener )resource).handleNotification(notification, handback); 501 } 502 503 } 504 | Popular Tags |