001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.jems.as.system;
023:
024: import org.apache.log4j.Logger;
025: import org.jboss.mx.util.MBeanProxy;
026: import org.jboss.mx.util.MBeanProxyCreationException;
027: import org.jboss.mx.util.ObjectNameFactory;
028: import org.jboss.portal.jems.as.JMX;
029: import org.jboss.system.ServiceController;
030: import org.jboss.system.ServiceControllerMBean;
031: import org.jboss.system.ServiceMBean;
032:
033: import javax.management.AttributeChangeNotification;
034: import javax.management.MBeanServer;
035: import javax.management.MBeanServerNotification;
036: import javax.management.Notification;
037: import javax.management.NotificationFilter;
038: import javax.management.NotificationListener;
039: import javax.management.ObjectName;
040: import java.util.Collections;
041: import java.util.HashSet;
042: import java.util.Set;
043:
044: /**
045: * Adapts JBoss lifecycle event handling and multiplex event from different sources into one source.
046: * <p/>
047: * It requires an MBeanServer and a ServiceController plugged into the MBeanServer.
048: *
049: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
050: * @version $Revision: 8784 $
051: */
052: public class LifeCycleAdapter {
053:
054: /** The name of the server delegate. */
055: private static final ObjectName MBEAN_SERVER_DELEGATE = ObjectNameFactory
056: .create("JMImplementation:type=MBeanServerDelegate");
057:
058: /** The logger. */
059: protected Logger log;
060:
061: /** The service controller name. */
062: protected ServiceControllerMBean controller;
063:
064: /** The server. */
065: protected MBeanServer server;
066:
067: /** The delegated listener. */
068: private Listener listener;
069:
070: /** The watched mbeans. */
071: private Set watched;
072:
073: /** Create the rule for the given server. */
074: public LifeCycleAdapter(MBeanServer server) {
075: try {
076: this .log = Logger.getLogger(getClass());
077: this .controller = (ServiceControllerMBean) MBeanProxy.get(
078: ServiceControllerMBean.class,
079: ServiceController.OBJECT_NAME, server);
080: this .server = server;
081: this .listener = new Listener();
082: this .watched = Collections.synchronizedSet(new HashSet());
083: } catch (MBeanProxyCreationException ignored) {
084: }
085: }
086:
087: /**
088: * Start the adapter.
089: *
090: * @throws IllegalStateException if the service controller is missing
091: */
092: public void start() throws IllegalStateException {
093: JMX.addNotificationListener(server, MBEAN_SERVER_DELEGATE,
094: listener, listener, null);
095: if (!JMX.addNotificationListener(server,
096: ServiceControllerMBean.OBJECT_NAME, listener, listener,
097: null)) {
098: throw new IllegalStateException(
099: "The service controller is not here");
100: }
101: }
102:
103: /** Stop the adapter */
104: public void stop() {
105: JMX.removeNotificationListener(server,
106: ServiceControllerMBean.OBJECT_NAME, listener);
107: JMX.removeNotificationListener(server, MBEAN_SERVER_DELEGATE,
108: listener);
109: server = null;
110: watched.clear();
111: }
112:
113: public void addStateListener(ObjectName name) {
114: try {
115: server.addNotificationListener(name, listener, listener,
116: name);
117: watched.add(name);
118: } catch (Exception e) {
119: log.error("Cannot become a listener of " + name, e);
120: }
121: }
122:
123: public void removeStateListener(ObjectName name) {
124: try {
125: server.removeNotificationListener(name, listener);
126: watched.remove(name);
127: } catch (Exception ignored) {
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 name) {
140: }
141:
142: protected void starting(ObjectName name) {
143: }
144:
145: protected void started(ObjectName name) {
146: }
147:
148: protected void stopping(ObjectName name) {
149: }
150:
151: protected void stopped(ObjectName name) {
152: }
153:
154: protected void destroyed(ObjectName name) {
155: }
156:
157: private class Listener implements NotificationListener,
158: NotificationFilter {
159: /** The serialVersionUID */
160: private static final long serialVersionUID = 4049015398272819164L;
161:
162: public boolean isNotificationEnabled(Notification notification) {
163: return true;
164: }
165:
166: public void handleNotification(Notification notification,
167: Object handback) {
168: String type = notification.getType();
169: if (type == MBeanServerNotification.REGISTRATION_NOTIFICATION) {
170: MBeanServerNotification msn = (MBeanServerNotification) notification;
171: ObjectName name = msn.getMBeanName();
172: // registered(name);
173: } else if (type == MBeanServerNotification.UNREGISTRATION_NOTIFICATION) {
174: MBeanServerNotification msn = (MBeanServerNotification) notification;
175: ObjectName name = msn.getMBeanName();
176: // unregistered(name);
177: } else if (type == AttributeChangeNotification.ATTRIBUTE_CHANGE) {
178: AttributeChangeNotification acn = (AttributeChangeNotification) notification;
179: ObjectName name = (ObjectName) handback;
180: if ("State".equals(acn.getAttributeName())) {
181: int state = ((Integer) acn.getNewValue())
182: .intValue();
183: switch (state) {
184: case ServiceMBean.STARTING:
185: starting(name);
186: break;
187: case ServiceMBean.STARTED:
188: started(name);
189: break;
190: case ServiceMBean.STOPPING:
191: stopping(name);
192: break;
193: case ServiceMBean.STOPPED:
194: stopped(name);
195: break;
196: default:
197: }
198: }
199: } else if (type == ServiceMBean.CREATE_EVENT) {
200: ObjectName name = (ObjectName) notification
201: .getUserData();
202: if (watched.contains(name)) {
203: created(name);
204: }
205: } else if (type == ServiceMBean.DESTROY_EVENT) {
206: ObjectName name = (ObjectName) notification
207: .getUserData();
208: if (watched.contains(name)) {
209: destroyed(name);
210: }
211: }
212: }
213: }
214: }
|