KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > system > LifeCycleAdapter


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.system;
10
11 import java.util.Collections JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import javax.management.AttributeChangeNotification JavaDoc;
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MBeanServerNotification JavaDoc;
18 import javax.management.Notification JavaDoc;
19 import javax.management.NotificationFilter JavaDoc;
20 import javax.management.NotificationListener JavaDoc;
21 import javax.management.ObjectName JavaDoc;
22
23 import org.apache.log4j.Logger;
24 import org.jboss.mx.util.MBeanProxy;
25 import org.jboss.mx.util.MBeanProxyCreationException;
26 import org.jboss.mx.util.ObjectNameFactory;
27 import org.jboss.portal.common.util.JMX;
28 import org.jboss.system.ServiceController;
29 import org.jboss.system.ServiceControllerMBean;
30 import org.jboss.system.ServiceMBean;
31
32 /**
33  * Adapts JBoss lifecycle event handling and multiplex event from
34  * different sources into one source.
35  *
36  * It requires an MBeanServer and a ServiceController plugged
37  * into the MBeanServer.
38  *
39  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
40  * @version $Revision: 1.2 $
41  */

42 public class LifeCycleAdapter
43 {
44
45    /** The name of the server delegate. */
46    private static final ObjectName JavaDoc MBEAN_SERVER_DELEGATE = ObjectNameFactory.create("JMImplementation:type=MBeanServerDelegate");
47
48    /** The logger. */
49    protected Logger log;
50
51    /** The service controller name. */
52    protected ServiceControllerMBean controller;
53
54    /** The server. */
55    protected MBeanServer JavaDoc server;
56
57    /** The delegated listener. */
58    private Listener JavaDoc listener;
59
60    /** The watched mbeans. */
61    private Set JavaDoc watched;
62
63    /**
64     * Create the rule for the given server.
65     */

66    public LifeCycleAdapter(MBeanServer JavaDoc server)
67    {
68       try
69       {
70          this.log = Logger.getLogger(getClass());
71          this.controller = (ServiceControllerMBean)MBeanProxy.get(ServiceControllerMBean.class, ServiceController.OBJECT_NAME, server);
72          this.server = server;
73          this.listener = new Listener JavaDoc();
74          this.watched = Collections.synchronizedSet(new HashSet JavaDoc());
75       }
76       catch (MBeanProxyCreationException ignored)
77       {
78       }
79    }
80
81    /**
82     * Start the adapter.
83     *
84     * @exception IllegalStateException if the service controller is missing
85     */

86    public void start() throws IllegalStateException JavaDoc
87    {
88       JMX.addNotificationListener(server, MBEAN_SERVER_DELEGATE, listener, listener, null);
89       if (!JMX.addNotificationListener(server, ServiceControllerMBean.OBJECT_NAME, listener, listener, null))
90       {
91          throw new IllegalStateException JavaDoc("The service controller is not here");
92       }
93    }
94
95    /**
96     * Stop the adapter
97     */

98    public void stop()
99    {
100       JMX.removeNotificationListener(server, ServiceControllerMBean.OBJECT_NAME, listener);
101       JMX.removeNotificationListener(server, MBEAN_SERVER_DELEGATE, listener);
102       server = null;
103       watched.clear();
104    }
105
106    public void addStateListener(ObjectName JavaDoc name)
107    {
108       try
109       {
110          server.addNotificationListener(name, listener, listener, name);
111          watched.add(name);
112       }
113       catch (Exception JavaDoc e)
114       {
115          log.error("Cannot become a listener of " + name, e);
116       }
117    }
118
119    public void removeStateListener(ObjectName JavaDoc name)
120    {
121       try
122       {
123          server.removeNotificationListener(name, listener);
124          watched.remove(name);
125       }
126       catch (Exception JavaDoc ignored)
127       {
128       }
129    }
130
131 // protected void registered(ObjectName name)
132
// {
133
// }
134
//
135
// protected void unregistered(ObjectName name)
136
// {
137
// }
138

139    protected void created(ObjectName JavaDoc name)
140    {
141    }
142
143    protected void starting(ObjectName JavaDoc name)
144    {
145    }
146
147    protected void started(ObjectName JavaDoc name)
148    {
149    }
150
151    protected void stopping(ObjectName JavaDoc name)
152    {
153    }
154
155    protected void stopped(ObjectName JavaDoc name)
156    {
157    }
158
159    protected void destroyed(ObjectName JavaDoc name)
160    {
161    }
162
163    private class Listener implements NotificationListener JavaDoc, NotificationFilter JavaDoc
164    {
165       public boolean isNotificationEnabled(Notification JavaDoc notification)
166       {
167          return true;
168       }
169       public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
170       {
171          String JavaDoc type = notification.getType();
172          if (type == MBeanServerNotification.REGISTRATION_NOTIFICATION)
173          {
174             MBeanServerNotification JavaDoc msn = (MBeanServerNotification JavaDoc)notification;
175             ObjectName JavaDoc name = msn.getMBeanName();
176             // registered(name);
177
}
178          else if (type == MBeanServerNotification.UNREGISTRATION_NOTIFICATION)
179          {
180             MBeanServerNotification JavaDoc msn = (MBeanServerNotification JavaDoc)notification;
181             ObjectName JavaDoc name = msn.getMBeanName();
182             // unregistered(name);
183
}
184          else if (type == AttributeChangeNotification.ATTRIBUTE_CHANGE)
185          {
186             AttributeChangeNotification JavaDoc acn = (AttributeChangeNotification JavaDoc)notification;
187             ObjectName JavaDoc name = (ObjectName JavaDoc)handback;
188             if ("State".equals(acn.getAttributeName()))
189             {
190                int state = ((Integer JavaDoc)acn.getNewValue()).intValue();
191                switch(state)
192                {
193                case ServiceMBean.STARTING:
194                   starting(name);
195                   break;
196                case ServiceMBean.STARTED:
197                   started(name);
198                   break;
199                case ServiceMBean.STOPPING:
200                   stopping(name);
201                   break;
202                case ServiceMBean.STOPPED:
203                   stopped(name);
204                   break;
205                default:
206                }
207             }
208          }
209          else if (type == ServiceMBean.CREATE_EVENT)
210          {
211             ObjectName JavaDoc name = (ObjectName JavaDoc)notification.getUserData();
212             if (watched.contains(name))
213             {
214                created(name);
215             }
216          }
217          else if (type == ServiceMBean.DESTROY_EVENT)
218          {
219             ObjectName JavaDoc name = (ObjectName JavaDoc)notification.getUserData();
220             if (watched.contains(name))
221             {
222                destroyed(name);
223             }
224          }
225       }
226    }
227 }
228
Popular Tags