1 9 package org.jboss.portal.common.system; 10 11 import java.util.Collections ; 12 import java.util.HashSet ; 13 import java.util.Set ; 14 15 import javax.management.AttributeChangeNotification ; 16 import javax.management.MBeanServer ; 17 import javax.management.MBeanServerNotification ; 18 import javax.management.Notification ; 19 import javax.management.NotificationFilter ; 20 import javax.management.NotificationListener ; 21 import javax.management.ObjectName ; 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 42 public class LifeCycleAdapter 43 { 44 45 46 private static final ObjectName MBEAN_SERVER_DELEGATE = ObjectNameFactory.create("JMImplementation:type=MBeanServerDelegate"); 47 48 49 protected Logger log; 50 51 52 protected ServiceControllerMBean controller; 53 54 55 protected MBeanServer server; 56 57 58 private Listener listener; 59 60 61 private Set watched; 62 63 66 public LifeCycleAdapter(MBeanServer 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 (); 74 this.watched = Collections.synchronizedSet(new HashSet ()); 75 } 76 catch (MBeanProxyCreationException ignored) 77 { 78 } 79 } 80 81 86 public void start() throws IllegalStateException 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 ("The service controller is not here"); 92 } 93 } 94 95 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 name) 107 { 108 try 109 { 110 server.addNotificationListener(name, listener, listener, name); 111 watched.add(name); 112 } 113 catch (Exception e) 114 { 115 log.error("Cannot become a listener of " + name, e); 116 } 117 } 118 119 public void removeStateListener(ObjectName name) 120 { 121 try 122 { 123 server.removeNotificationListener(name, listener); 124 watched.remove(name); 125 } 126 catch (Exception ignored) 127 { 128 } 129 } 130 131 139 protected void created(ObjectName name) 140 { 141 } 142 143 protected void starting(ObjectName name) 144 { 145 } 146 147 protected void started(ObjectName name) 148 { 149 } 150 151 protected void stopping(ObjectName name) 152 { 153 } 154 155 protected void stopped(ObjectName name) 156 { 157 } 158 159 protected void destroyed(ObjectName name) 160 { 161 } 162 163 private class Listener implements NotificationListener , NotificationFilter 164 { 165 public boolean isNotificationEnabled(Notification notification) 166 { 167 return true; 168 } 169 public void handleNotification(Notification notification, Object handback) 170 { 171 String type = notification.getType(); 172 if (type == MBeanServerNotification.REGISTRATION_NOTIFICATION) 173 { 174 MBeanServerNotification msn = (MBeanServerNotification )notification; 175 ObjectName name = msn.getMBeanName(); 176 } 178 else if (type == MBeanServerNotification.UNREGISTRATION_NOTIFICATION) 179 { 180 MBeanServerNotification msn = (MBeanServerNotification )notification; 181 ObjectName name = msn.getMBeanName(); 182 } 184 else if (type == AttributeChangeNotification.ATTRIBUTE_CHANGE) 185 { 186 AttributeChangeNotification acn = (AttributeChangeNotification )notification; 187 ObjectName name = (ObjectName )handback; 188 if ("State".equals(acn.getAttributeName())) 189 { 190 int state = ((Integer )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 name = (ObjectName )notification.getUserData(); 212 if (watched.contains(name)) 213 { 214 created(name); 215 } 216 } 217 else if (type == ServiceMBean.DESTROY_EVENT) 218 { 219 ObjectName name = (ObjectName )notification.getUserData(); 220 if (watched.contains(name)) 221 { 222 destroyed(name); 223 } 224 } 225 } 226 } 227 } 228 | Popular Tags |