KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > mejb > ListenerRegistrationImpl


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ListenerRegistrationImpl.java,v 1.6 2004/05/06 14:38:12 danesa Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.mejb;
26
27 import java.rmi.RemoteException JavaDoc;
28
29 import javax.management.InstanceNotFoundException JavaDoc;
30 import javax.management.ListenerNotFoundException JavaDoc;
31 import javax.management.NotificationFilter JavaDoc;
32 import javax.management.NotificationListener JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.j2ee.ListenerRegistration JavaDoc;
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38 import javax.rmi.PortableRemoteObject JavaDoc;
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 /**
48  * ListenerRegistration implementation
49  */

50 public class ListenerRegistrationImpl implements ListenerRegistration JavaDoc {
51     transient Logger logger = null;
52
53     // The ManagementListener MBean's object name
54
ObjectName JavaDoc listenerMBean_on = null;
55
56     // The initialization of transient attributes is completed on the client side
57
boolean initialized = false;
58
59     String JavaDoc proxyName = null;
60     // The current MBean server's RMI connector name
61
String JavaDoc rmiConnectorName = null;
62     // Naming context used to bind the proxy object
63
transient Context JavaDoc context = null;
64     // Remote object called by the ManagementListenerMBean
65
transient ListenerProxy proxy = null;
66
67     /**
68      * The constructor is executed on the server side.
69      */

70     public ListenerRegistrationImpl(ObjectName JavaDoc listenerMBean_on, String JavaDoc proxyName) {
71         JmxService jmxService = null;
72         try {
73             jmxService = (JmxService) ServiceManager.getInstance().getJmxService();
74         } catch (Exception JavaDoc e) {
75             // should never occurs
76
}
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     /**
90      * Add a listener to a registered managed object.
91      */

92      public void addNotificationListener(ObjectName JavaDoc name,
93                                         NotificationListener JavaDoc listener,
94                                         NotificationFilter JavaDoc filter,
95                                         Object JavaDoc handback)
96         throws InstanceNotFoundException JavaDoc,
97                RemoteException JavaDoc {
98
99         // Create a naming context
100
if (context == null) {
101             try {
102                 context = new InitialContext JavaDoc();
103             } catch (NamingException JavaDoc e) {
104                 // Could not create naming context
105
throw new RemoteException JavaDoc("Could not register a listener to managed object " + name.toString());
106             }
107         }
108
109         // Set actual listener and filter in the proxy
110
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 JavaDoc e) {
116                 throw new RemoteException JavaDoc("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                 // Add a the ManagementListener MBean as listener to the 'name' MBean
125
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 JavaDoc("Could not add the ManagementListener MBean as listener to managed object: " + name.toString());
129             }
130         } catch (NamingException JavaDoc ne) {
131             throw new RemoteException JavaDoc("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     /**
138      * Enables to remove a listener from a registered managed object.
139      */

140     public void removeNotificationListener(ObjectName JavaDoc name,
141                                         NotificationListener JavaDoc listener)
142         throws InstanceNotFoundException JavaDoc,
143                ListenerNotFoundException JavaDoc,
144                RemoteException JavaDoc {
145
146         proxy.removeNotificationListener(listener);
147
148         if (context == null) {
149             try {
150                 context = new InitialContext JavaDoc();
151             } catch (NamingException JavaDoc e) {
152                 // Could not create naming context
153
throw new RemoteException JavaDoc("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 JavaDoc e) {
161             // Could not create naming context
162
throw new RemoteException JavaDoc("Could not unbound ListenerProxy");
163         }
164     }
165 }
166
Popular Tags