ListenerRegistrationImpl.java

00001 
00025 package org.objectweb.jonas.mejb;
00026 
00027 import java.rmi.RemoteException;
00028 
00029 import javax.management.InstanceNotFoundException;
00030 import javax.management.ListenerNotFoundException;
00031 import javax.management.NotificationFilter;
00032 import javax.management.NotificationListener;
00033 import javax.management.ObjectName;
00034 import javax.management.j2ee.ListenerRegistration;
00035 import javax.naming.Context;
00036 import javax.naming.InitialContext;
00037 import javax.naming.NamingException;
00038 import javax.rmi.PortableRemoteObject;
00039 
00040 import org.objectweb.jonas.common.Log;
00041 import org.objectweb.jonas.jmx.JmxService;
00042 import org.objectweb.jonas.jmx.RMIConnector;
00043 import org.objectweb.jonas.service.ServiceManager;
00044 import org.objectweb.util.monolog.api.BasicLevel;
00045 import org.objectweb.util.monolog.api.Logger;
00046 
00050 public class ListenerRegistrationImpl implements ListenerRegistration {
00051     transient Logger logger = null;
00052 
00053     // The ManagementListener MBean's object name
00054     ObjectName listenerMBean_on = null;
00055 
00056     // The initialization of transient attributes is completed on the client side
00057     boolean initialized = false;
00058 
00059     String proxyName = null;
00060     // The current MBean server's RMI connector name
00061     String rmiConnectorName = null;
00062     // Naming context used to bind the proxy object
00063     transient Context context = null;
00064     // Remote object called by the ManagementListenerMBean
00065     transient ListenerProxy proxy = null;
00066 
00070     public ListenerRegistrationImpl(ObjectName listenerMBean_on, String proxyName) {
00071         JmxService jmxService = null;
00072         try {
00073             jmxService = (JmxService) ServiceManager.getInstance().getJmxService();
00074         } catch (Exception e) {
00075             // should never occurs
00076         }
00077         rmiConnectorName = jmxService.getRmiConnectorName();
00078         this.listenerMBean_on = listenerMBean_on;
00079         this.proxyName = proxyName;
00080         logger = Log.getLogger("org.objectweb.jonas.management.j2eemanagement.event");
00081         logger.log(BasicLevel.INFO, "Create ListenerRegistration instance");
00082         if (logger.isLoggable(BasicLevel.DEBUG)) {
00083             logger.log(BasicLevel.DEBUG, "MBeanServer's RMI Connector: " + rmiConnectorName);
00084             logger.log(BasicLevel.DEBUG, "ManagementListener MBean object name: " +  listenerMBean_on.toString());
00085             logger.log(BasicLevel.DEBUG, "ListenerProxy JNDI name: " + proxyName);
00086         }
00087     }
00088 
00092      public void addNotificationListener(ObjectName name,
00093                                         NotificationListener listener,
00094                                         NotificationFilter filter,
00095                                         Object handback)
00096         throws InstanceNotFoundException,
00097                RemoteException {
00098 
00099         // Create a naming context
00100         if (context == null) {
00101             try {
00102                 context = new InitialContext();
00103             } catch (NamingException e) {
00104                 // Could not create naming context
00105                 throw new RemoteException("Could not register a listener to managed object " + name.toString());
00106             }
00107         }
00108 
00109         // Set actual listener and filter in the proxy
00110         if (proxy == null) {
00111             proxy = new ListenerProxyImpl();
00112             try {
00113                 context.rebind(proxyName, proxy);
00114                 System.out.println("ListenerRegistration.addNotificationListener: ListnerProxy created and binded in JNDI");
00115             } catch (NamingException e) {
00116                 throw new RemoteException("Could not bind a listener proxy in JNDI:" + e.toString());
00117             }
00118         }
00119 
00120         RMIConnector rmic = null;
00121         try {
00122             rmic = (RMIConnector)PortableRemoteObject.narrow(context.lookup(rmiConnectorName), RMIConnector.class);
00123             if (rmic != null) {
00124                 // Add a the ManagementListener MBean as listener to the 'name' MBean
00125                 rmic.addNotificationListener(name, listenerMBean_on, null, handback);
00126                 System.out.println("ListenerRegistration.addNotificationListener: ManagementListener MBean added as listener to managed object: " + name.toString());
00127             } else {
00128                 throw new RemoteException("Could not add the ManagementListener MBean as listener to managed object: " + name.toString());
00129             }
00130         } catch (NamingException ne) {
00131             throw new RemoteException("Could not add the ManagementListener MBean as listener to managed object: " + name.toString() + " : " + ne.toString());
00132         }
00133 
00134         proxy.addNotificationListener(listener, filter);
00135     }
00136 
00140     public void removeNotificationListener(ObjectName name,
00141                                         NotificationListener listener)
00142         throws InstanceNotFoundException,
00143                ListenerNotFoundException,
00144                RemoteException {
00145 
00146         proxy.removeNotificationListener(listener);
00147 
00148         if (context == null) {
00149             try {
00150                 context = new InitialContext();
00151             } catch (NamingException e) {
00152                 // Could not create naming context
00153                 throw new RemoteException("Could not access registry to unbind the ListenerProxy");
00154             }
00155         }
00156 
00157         try {
00158             context.unbind(proxyName);
00159             System.out.println("ListenerRegistration.removeNotificationListener ListnerProxy un-bound from JNDI");
00160         } catch (NamingException e) {
00161             // Could not create naming context
00162             throw new RemoteException("Could not unbound ListenerProxy");
00163         }
00164     }
00165 }

Generated on Tue Feb 15 15:05:19 2005 for JOnAS by  doxygen 1.3.9.1