1 25 package org.objectweb.jonas.mejb; 26 27 import java.rmi.RemoteException ; 28 29 import javax.management.InstanceNotFoundException ; 30 import javax.management.ListenerNotFoundException ; 31 import javax.management.NotificationFilter ; 32 import javax.management.NotificationListener ; 33 import javax.management.ObjectName ; 34 import javax.management.j2ee.ListenerRegistration ; 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.naming.NamingException ; 38 import javax.rmi.PortableRemoteObject ; 39 40 import org.objectweb.jonas.common.Log; 41 import org.objectweb.jonas.jmx.JmxService; 42 import org.objectweb.jonas.jmx.RMIConnector; 43 import org.objectweb.jonas.service.ServiceManager; 44 import org.objectweb.util.monolog.api.BasicLevel; 45 import org.objectweb.util.monolog.api.Logger; 46 47 50 public class ListenerRegistrationImpl implements ListenerRegistration { 51 transient Logger logger = null; 52 53 ObjectName listenerMBean_on = null; 55 56 boolean initialized = false; 58 59 String proxyName = null; 60 String rmiConnectorName = null; 62 transient Context context = null; 64 transient ListenerProxy proxy = null; 66 67 70 public ListenerRegistrationImpl(ObjectName listenerMBean_on, String proxyName) { 71 JmxService jmxService = null; 72 try { 73 jmxService = (JmxService) ServiceManager.getInstance().getJmxService(); 74 } catch (Exception e) { 75 } 77 rmiConnectorName = jmxService.getRmiConnectorName(); 78 this.listenerMBean_on = listenerMBean_on; 79 this.proxyName = proxyName; 80 logger = Log.getLogger("org.objectweb.jonas.management.j2eemanagement.event"); 81 logger.log(BasicLevel.INFO, "Create ListenerRegistration instance"); 82 if (logger.isLoggable(BasicLevel.DEBUG)) { 83 logger.log(BasicLevel.DEBUG, "MBeanServer's RMI Connector: " + rmiConnectorName); 84 logger.log(BasicLevel.DEBUG, "ManagementListener MBean object name: " + listenerMBean_on.toString()); 85 logger.log(BasicLevel.DEBUG, "ListenerProxy JNDI name: " + proxyName); 86 } 87 } 88 89 92 public void addNotificationListener(ObjectName name, 93 NotificationListener listener, 94 NotificationFilter filter, 95 Object handback) 96 throws InstanceNotFoundException , 97 RemoteException { 98 99 if (context == null) { 101 try { 102 context = new InitialContext (); 103 } catch (NamingException e) { 104 throw new RemoteException ("Could not register a listener to managed object " + name.toString()); 106 } 107 } 108 109 if (proxy == null) { 111 proxy = new ListenerProxyImpl(); 112 try { 113 context.rebind(proxyName, proxy); 114 System.out.println("ListenerRegistration.addNotificationListener: ListnerProxy created and binded in JNDI"); 115 } catch (NamingException e) { 116 throw new RemoteException ("Could not bind a listener proxy in JNDI:" + e.toString()); 117 } 118 } 119 120 RMIConnector rmic = null; 121 try { 122 rmic = (RMIConnector)PortableRemoteObject.narrow(context.lookup(rmiConnectorName), RMIConnector.class); 123 if (rmic != null) { 124 rmic.addNotificationListener(name, listenerMBean_on, null, handback); 126 System.out.println("ListenerRegistration.addNotificationListener: ManagementListener MBean added as listener to managed object: " + name.toString()); 127 } else { 128 throw new RemoteException ("Could not add the ManagementListener MBean as listener to managed object: " + name.toString()); 129 } 130 } catch (NamingException ne) { 131 throw new RemoteException ("Could not add the ManagementListener MBean as listener to managed object: " + name.toString() + " : " + ne.toString()); 132 } 133 134 proxy.addNotificationListener(listener, filter); 135 } 136 137 140 public void removeNotificationListener(ObjectName name, 141 NotificationListener listener) 142 throws InstanceNotFoundException , 143 ListenerNotFoundException , 144 RemoteException { 145 146 proxy.removeNotificationListener(listener); 147 148 if (context == null) { 149 try { 150 context = new InitialContext (); 151 } catch (NamingException e) { 152 throw new RemoteException ("Could not access registry to unbind the ListenerProxy"); 154 } 155 } 156 157 try { 158 context.unbind(proxyName); 159 System.out.println("ListenerRegistration.removeNotificationListener ListnerProxy un-bound from JNDI"); 160 } catch (NamingException e) { 161 throw new RemoteException ("Could not unbound ListenerProxy"); 163 } 164 } 165 } 166 | Popular Tags |