1 25 26 package org.objectweb.petals.jbi.component.lifecycle; 27 28 import javax.jbi.JBIException; 29 import javax.management.AttributeChangeNotification ; 30 import javax.management.ListenerNotFoundException ; 31 import javax.management.MBeanNotificationInfo ; 32 import javax.management.Notification ; 33 import javax.management.NotificationBroadcaster ; 34 import javax.management.NotificationBroadcasterSupport ; 35 import javax.management.NotificationFilter ; 36 import javax.management.NotificationListener ; 37 import javax.management.ObjectName ; 38 39 import org.objectweb.petals.jbi.component.event.StateChangeFailedEvent; 40 import org.objectweb.petals.jbi.component.event.StateChangedEvent; 41 import org.objectweb.petals.util.LoggingUtil; 42 import org.objectweb.petals.util.PetalsRuntimeException; 43 44 50 public abstract class LifeCycleAbstract implements LifeCycleMBean, 51 NotificationBroadcaster { 52 53 protected Object activitySynchronizer; 54 55 protected boolean initialized; 56 57 protected LoggingUtil log; 58 59 protected ObjectName mBeanName; 60 61 protected long sequenceNumber; 62 63 protected String state; 64 65 NotificationBroadcasterSupport notifSupport = new NotificationBroadcasterSupport (); 66 67 public LifeCycleAbstract(ObjectName mBeanName, LoggingUtil log) { 68 this.log = log; 69 70 log.call(); 71 72 activitySynchronizer = new Object (); 73 74 state = SHUTDOWN; 75 76 initialized = false; 77 78 this.mBeanName = mBeanName; 79 80 } 81 82 public void addNotificationListener(NotificationListener arg0, 83 NotificationFilter arg1, Object arg2) 84 throws IllegalArgumentException { 85 notifSupport.addNotificationListener(arg0, arg1, arg2); 86 } 87 88 public abstract void doInit() throws JBIException; 89 90 public abstract void doShutdown() throws JBIException; 91 92 96 public abstract void doStart() throws JBIException; 97 98 public abstract void doStop() throws JBIException; 99 100 103 public String getCurrentState() { 104 if (!(LifeCycleMBean.SHUTDOWN.equals(state) 105 || LifeCycleMBean.STARTED.equals(state) 106 || LifeCycleMBean.STOPPED.equals(state) || LifeCycleMBean.UNKNOWN 107 .equals(state))) { 108 throw new PetalsRuntimeException( 109 "State '" + state + "' isn't defined by JBI specification !"); 110 } 111 return state; 112 } 113 114 118 public ObjectName getMBeanName() { 119 return mBeanName; 120 } 121 122 public MBeanNotificationInfo [] getNotificationInfo() { 123 String [] types = new String [] {AttributeChangeNotification.ATTRIBUTE_CHANGE}; 124 125 String name = AttributeChangeNotification .class.getName(); 126 127 String description = "Notification about a change of the lifeCycle state or a state change failure."; 128 129 MBeanNotificationInfo info = new MBeanNotificationInfo (types, name, 130 description); 131 132 return new MBeanNotificationInfo [] {info}; 133 } 134 135 public void removeNotificationListener(NotificationListener arg0) 136 throws ListenerNotFoundException { 137 notifSupport.removeNotificationListener(arg0); 138 } 139 140 public void setMBeanName(ObjectName beanName) { 141 mBeanName = beanName; 142 } 143 144 148 154 public synchronized void setState(String state) throws JBIException { 155 stateChanged(new StateChangedEvent(this, this.state, state)); 156 157 this.state = state; 158 } 159 160 168 public void shutDown() throws JBIException { 169 log.start(); 170 171 if (STOPPED.equals(state)) { 172 synchronized (activitySynchronizer) { 173 try { 174 setState(SHUTDOWNING); 175 176 doShutdown(); 177 178 initialized = false; 179 180 setState(SHUTDOWN); 181 } catch (Throwable re) { 182 setState(UNKNOWN); 183 184 String msg = "An exception occured during the shutdown."; 185 186 JBIException e = new JBIException(msg, re); 187 188 log.error(msg, e); 189 190 stateChangeFailed(new StateChangeFailedEvent(this, e)); 191 192 throw e; 193 } 194 } 195 } else { 196 String msg = "The Object can not be shutdowned in this state."; 197 198 JBIException e = new JBIException(msg); 199 200 log.error(msg, e); 201 202 stateChangeFailed(new StateChangeFailedEvent(this, e)); 203 204 throw e; 205 } 206 log.end(); 207 } 208 209 217 public void start() throws JBIException { 218 log.start(); 219 220 if (SHUTDOWN.equals(state) || STOPPED.equals(state)) { 221 synchronized (activitySynchronizer) { 222 try { 223 if (!initialized) { 224 setState(INITIALIZING); 225 226 doInit(); 227 228 initialized = true; 229 } 230 setState(STARTING); 231 232 doStart(); 233 234 setState(STARTED); 235 } catch (Throwable re) { 236 setState(UNKNOWN); 237 238 String msg = "An exception occured during the start."; 239 240 JBIException e = new JBIException(msg, re); 241 242 log.error(msg, e); 243 244 stateChangeFailed(new StateChangeFailedEvent(this, e)); 245 246 throw e; 247 } 248 } 249 } else { 250 String msg = "The Object can not be started in this state."; 251 252 JBIException e = new JBIException(msg); 253 254 log.error(msg, e); 255 256 stateChangeFailed(new StateChangeFailedEvent(this, e)); 257 258 throw e; 259 } 260 log.end(); 261 } 262 263 269 public synchronized void stateChanged(StateChangedEvent event) { 270 log.call(); 271 if (mBeanName != null) { 272 Notification n = new AttributeChangeNotification (mBeanName, 273 sequenceNumber++, System.currentTimeMillis(), "State of '" 274 + mBeanName.toString() + "' changed : " 275 + event.getOldState() + " -> " 276 + event.getNewState(), "state", "String", event 277 .getOldState(), event.getNewState()); 278 279 notifSupport.sendNotification(n); 280 } 281 } 282 283 290 public synchronized void stateChangeFailed(StateChangeFailedEvent event) { 291 log.call(); 292 if (mBeanName != null) { 293 Notification n = new AttributeChangeNotification (mBeanName, 294 sequenceNumber++, System.currentTimeMillis(), 295 "State change of '" + mBeanName.toString() + "' failed : " 296 + event.getException().getMessage(), "exception", 297 "String", null, event.getException().toString()); 298 299 notifSupport.sendNotification(n); 300 } 301 } 302 303 311 public void stop() throws JBIException { 312 log.start(); 313 314 if (STARTED.equals(state)) { 315 synchronized (activitySynchronizer) { 316 try { 317 318 setState(STOPPING); 319 320 doStop(); 321 322 setState(STOPPED); 323 324 } catch (Throwable re) { 325 setState(UNKNOWN); 326 327 String msg = "An exception occured during the stop."; 328 329 JBIException e = new JBIException(msg, re); 330 331 log.error(msg, e); 332 333 stateChangeFailed(new StateChangeFailedEvent(this, e)); 334 335 throw e; 336 } 337 } 338 } else { 339 String msg = "The Object can not be stoped in this state." + state; 340 341 JBIException e = new JBIException(msg); 342 343 log.error(msg, e); 344 345 stateChangeFailed(new StateChangeFailedEvent(this, e)); 346 347 throw e; 348 } 349 log.end(); 350 } 351 352 } 353 | Popular Tags |