1 22 package org.jboss.system; 23 24 import javax.management.Notification ; 25 import javax.management.ObjectName ; 26 27 55 public class BarrierController extends ListenerServiceMBeanSupport 56 implements BarrierControllerMBean 57 { 58 60 61 private ObjectName barrierName; 62 63 64 private Boolean enableOnStartup; 65 66 67 private String startHandback; 68 69 70 private String stopHandback; 71 72 73 private Boolean dynamicSubscriptions; 74 75 77 78 protected Barrier barrier; 79 80 82 85 public BarrierController() 86 { 87 } 89 90 92 97 public String getBarrierStateString() 98 { 99 return (barrier != null) ? barrier.getStateString() : null; 100 } 101 102 107 public void setBarrierObjectName(ObjectName barrierName) 108 { 109 if (this.barrierName == null) 111 { 112 this.barrierName = barrierName; 113 } 114 } 115 116 121 public ObjectName getBarrierObjectName() 122 { 123 return barrierName; 124 } 125 126 127 135 public void setBarrierEnabledOnStartup(Boolean enableOnStartup) 136 { 137 if (this.enableOnStartup == null) 139 { 140 this.enableOnStartup = enableOnStartup; 141 } 142 } 143 144 153 public Boolean getBarrierEnabledOnStartup() 154 { 155 if (enableOnStartup == null) 156 { 157 enableOnStartup = enableOnStartup(); 160 } 161 return enableOnStartup; 162 } 163 164 169 public void setStartBarrierHandback(String startHandback) 170 { 171 if (this.startHandback == null) 173 { 174 this.startHandback = startHandback; 175 } 176 } 177 178 183 public String getStartBarrierHandback() 184 { 185 return startHandback; 186 } 187 188 193 public void setStopBarrierHandback(String stopHandback) 194 { 195 if (this.stopHandback == null) 197 { 198 this.stopHandback = stopHandback; 199 } 200 } 201 202 207 public String getStopBarrierHandback() 208 { 209 return stopHandback; 210 } 211 212 217 public void setDynamicSubscriptions(Boolean dynamicSubscriptions) 218 { 219 if (this.dynamicSubscriptions == null) 221 { 222 this.dynamicSubscriptions = dynamicSubscriptions; 223 } 224 } 225 226 231 public Boolean getDynamicSubscriptions() 232 { 233 if (dynamicSubscriptions == null) 234 { 235 dynamicSubscriptions = Boolean.TRUE; 236 } 237 return dynamicSubscriptions; 238 } 239 240 242 251 protected Boolean enableOnStartup() 252 { 253 return Boolean.FALSE; 254 } 255 256 258 protected void createService() throws Exception 259 { 260 barrier = new Barrier(getServiceName()); 262 263 initBarrier(); 264 } 265 266 273 private void initBarrier() throws Exception 274 { 275 if (barrier != null && getServer() != null) 276 { 277 getServer().registerMBean(barrier, barrierName); 279 280 barrier.create(); 282 283 if (getBarrierEnabledOnStartup().booleanValue()) 285 { 286 startBarrier(); 287 } 288 subscribe(getDynamicSubscriptions().booleanValue()); 290 } 291 } 292 293 protected void destroyService() 294 { 295 unsubscribe(); 297 298 try 299 { 300 barrier.destroy(); 302 303 getServer().unregisterMBean(barrierName); 305 } 306 catch (Throwable e) 307 { 308 log.debug("Unexpected error during destroy", e); 309 } 310 311 barrier = null; 313 } 314 315 317 321 public void handleNotification2(Notification n, Object handback) 322 { 323 log.debug("Got notification: " + n); 324 325 if (startHandback != null && startHandback.equals(handback)) 326 { 327 log.debug("Saw '" + handback + "' handback, starting barrier"); 328 startBarrier(); 329 } 330 else if (stopHandback != null && stopHandback.equals(handback)) 331 { 332 log.debug("Saw '" + handback + "' handback, stopping barrier"); 333 stopBarrier(); 334 } 335 } 336 337 339 344 public void startBarrier() 345 { 346 try 347 { 348 barrier.start(); 350 } 351 catch (Throwable e) 352 { 353 log.warn("Failed to start barrier: " + barrierName, e); 354 } 355 } 356 357 362 public void stopBarrier() 363 { 364 try 365 { 366 barrier.stop(); 368 } 369 catch (Throwable e) 370 { 371 log.warn("Failed to stop barrier: " + barrierName, e); 372 } 373 } 374 375 377 @Override 378 public void postRegister(Boolean registrationDone) 379 { 380 super.postRegister(registrationDone); 381 382 if (Boolean.TRUE.equals(registrationDone)) 383 { 384 try 385 { 386 initBarrier(); 387 } 388 catch (Exception e) 389 { 390 throw new RuntimeException (e); 391 } 392 } 393 } 394 395 397 400 public static interface BarrierMBean 401 { 402 403 ObjectName getBarrierController(); 404 String getStateString(); 405 int getState(); 406 407 408 void jbossInternalLifecycle(String method) throws Exception ; 409 } 410 411 414 public static class Barrier extends ServiceMBeanSupport implements BarrierMBean 415 { 416 417 private ObjectName barrierController; 418 419 420 public Barrier(ObjectName barrierController) 421 { 422 this.barrierController = barrierController; 423 } 424 425 426 public ObjectName getBarrierController() 427 { 428 return barrierController; 429 } 430 } 431 } 432 | Popular Tags |