| 1 | /** |
| 2 | * Class <code>NamedModelMBean</code>. |
| 3 | */ |
| 4 | package org.jsesoft.mmbi; |
| 5 | |
| 6 | import javax.management.*; |
| 7 | import javax.management.modelmbean.*; |
| 8 | |
| 9 | /** |
| 10 | * Couples the Model MBean with its <code>ObjectInstance</code> |
| 11 | * information (eg <code>ObjectName</code>). |
| 12 | * |
| 13 | * <p> |
| 14 | * This convenience class provides a ModelMBean with attached |
| 15 | * <code>ObjectInstance</code> information. Especially, it provides |
| 16 | * the <code>ObjectName</code>. |
| 17 | * </p> |
| 18 | * <p> |
| 19 | * If the managed resource emits JMX notification (i.e. implements |
| 20 | * the <code>NotificationEmitter</code> interface, adding and removing |
| 21 | * Notification listeners is forwarded to the managed resource (which |
| 22 | * should be done by the <code>RequiredModelMBean</code>). |
| 23 | * </p> |
| 24 | * <p> |
| 25 | * <b>Example:</b> |
| 26 | * <code> |
| 27 | * <pre> |
| 28 | * NamedModelMBean mbean = new NamedModelMBean(); |
| 29 | * mbean.setModelMBeanInfo( ... ); |
| 30 | * mbean.setManagedResource( ..., "ObjectReference"); |
| 31 | * ObjectName objectName = new ObjectName( ... ); |
| 32 | * MBeanServer server = |
| 33 | * java.lang.management.ManagementFactory.getPlatformMBeanServer(); |
| 34 | * mbean.setInstance( server.registerMBean( mbean, objectName ) ); |
| 35 | * ... |
| 36 | * server.unregisterMBean( mbean.getName() ); |
| 37 | * </pre> |
| 38 | * </code> |
| 39 | * </p> |
| 40 | |
| 41 | * @author JSESoft |
| 42 | * @version 1.0 |
| 43 | */ |
| 44 | public class NamedModelMBean |
| 45 | extends RequiredModelMBean |
| 46 | { |
| 47 | |
| 48 | /** |
| 49 | * The attached object instance. |
| 50 | */ |
| 51 | private ObjectInstance instance; |
| 52 | |
| 53 | private NotificationEmitter resource; |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * Creates an <code>NamedModelMBean</code> instance from the |
| 58 | * <code>RequiredModelMBean</code>. |
| 59 | * |
| 60 | * @throws Exception if <code>RequiredModelMBean</code> construction fails |
| 61 | */ |
| 62 | public NamedModelMBean() |
| 63 | throws Exception |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Sets the instance attribute. |
| 69 | * |
| 70 | * @see #getInstance |
| 71 | * @param instance ObjectInstance |
| 72 | */ |
| 73 | public void setInstance( ObjectInstance instance ) |
| 74 | { |
| 75 | this.instance = instance; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Gets the instance attribute. |
| 80 | * |
| 81 | * @return the instance value (maybe null) |
| 82 | * @see #setInstance |
| 83 | */ |
| 84 | public ObjectInstance getInstance() |
| 85 | { |
| 86 | return instance; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Gets the object name. |
| 91 | * |
| 92 | * @return the MBean's object name from the instance. |
| 93 | */ |
| 94 | public ObjectName getName() |
| 95 | { |
| 96 | return getInstance().getObjectName(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Sets the manages resource (and remembers ist). |
| 101 | */ |
| 102 | @Override |
| 103 | public void setManagedResource( Object managed, String type ) |
| 104 | throws MBeanException, RuntimeOperationsException, |
| 105 | InstanceNotFoundException, InvalidTargetObjectTypeException |
| 106 | { |
| 107 | if( NotificationEmitter.class.isInstance( managed ) ) { |
| 108 | this.resource = ( NotificationEmitter ) managed; |
| 109 | } |
| 110 | super.setManagedResource( resource, type ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Adds a notification listener (to the managed resource as well). |
| 115 | * |
| 116 | * @see #removeNotificationListener |
| 117 | */ |
| 118 | @Override |
| 119 | public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) |
| 120 | { |
| 121 | if( resource != null ) { |
| 122 | resource.addNotificationListener( listener, filter, handback ); |
| 123 | } |
| 124 | super.addNotificationListener( listener, filter, handback ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Removes a notification listener (from the managed resource as well). |
| 129 | * |
| 130 | * @see #addNotificationListener |
| 131 | */ |
| 132 | @Override |
| 133 | public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) |
| 134 | throws ListenerNotFoundException |
| 135 | { |
| 136 | if( resource != null ) { |
| 137 | resource.removeNotificationListener( listener, filter, handback ); |
| 138 | } |
| 139 | super.removeNotificationListener( listener, filter, handback ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Removes a notification listener (from the managed resource as well). |
| 144 | * |
| 145 | * @see #addNotificationListener |
| 146 | */ |
| 147 | @Override |
| 148 | public void removeNotificationListener(NotificationListener listener) |
| 149 | throws ListenerNotFoundException |
| 150 | { |
| 151 | if( resource != null ) { |
| 152 | resource.removeNotificationListener( listener ); |
| 153 | } |
| 154 | super.removeNotificationListener( listener ); |
| 155 | } |
| 156 | } |