1 22 package org.jboss.system; 23 24 import javax.management.AttributeChangeNotification ; 25 import javax.management.JMException ; 26 import javax.management.MBeanInfo ; 27 import javax.management.MBeanOperationInfo ; 28 import javax.management.MBeanRegistration ; 29 import javax.management.MBeanServer ; 30 import javax.management.MalformedObjectNameException ; 31 import javax.management.ObjectName ; 32 33 import org.jboss.deployment.DeploymentInfo; 34 import org.jboss.deployment.SARDeployerMBean; 35 import org.jboss.logging.Logger; 36 import org.jboss.mx.util.JBossNotificationBroadcasterSupport; 37 38 52 public class ServiceMBeanSupport 53 extends JBossNotificationBroadcasterSupport 54 implements ServiceMBean, MBeanRegistration 55 { 56 57 public static final String [] SERVICE_CONTROLLER_SIG = new String [] { ObjectName .class.getName() }; 58 59 64 protected Logger log; 65 66 67 protected MBeanServer server; 68 69 70 protected ObjectName serviceName; 71 72 73 private int state = UNREGISTERED; 74 75 76 private boolean isJBossInternalLifecycleExposed = false; 77 78 83 public ServiceMBeanSupport() 84 { 85 this.log = Logger.getLogger(getClass().getName()); 87 log.trace("Constructing"); 88 } 89 90 97 public ServiceMBeanSupport(final Class type) 98 { 99 this(type.getName()); 100 } 101 102 109 public ServiceMBeanSupport(final String category) 110 { 111 this(Logger.getLogger(category)); 112 } 113 114 119 public ServiceMBeanSupport(final Logger log) 120 { 121 this.log = log; 122 log.trace("Constructing"); 123 } 124 125 128 public String getName() 129 { 130 return org.jboss.util.Classes.stripPackageName(log.getName()); 133 } 134 135 public ObjectName getServiceName() 136 { 137 return serviceName; 138 } 139 140 148 public DeploymentInfo getDeploymentInfo() 149 throws JMException 150 { 151 Object [] args = {serviceName}; 152 String [] sig = {serviceName.getClass().getName()}; 153 DeploymentInfo sdi = (DeploymentInfo) server.invoke(SARDeployerMBean.OBJECT_NAME, 154 "getService", args, sig); 155 return sdi; 156 } 157 158 public MBeanServer getServer() 159 { 160 return server; 161 } 162 163 public int getState() 164 { 165 return state; 166 } 167 168 public String getStateString() 169 { 170 return states[state]; 171 } 172 173 public Logger getLog() 174 { 175 return log; 176 } 177 178 179 183 public void create() throws Exception 184 { 185 if (serviceName != null && isJBossInternalLifecycleExposed) 186 server.invoke(ServiceController.OBJECT_NAME, "create", new Object [] { serviceName }, SERVICE_CONTROLLER_SIG); 187 else 188 jbossInternalCreate(); 189 } 190 191 public void start() throws Exception 192 { 193 if (serviceName != null && isJBossInternalLifecycleExposed) 194 server.invoke(ServiceController.OBJECT_NAME, "start", new Object [] { serviceName }, SERVICE_CONTROLLER_SIG); 195 else 196 jbossInternalStart(); 197 } 198 199 public void stop() 200 { 201 try 202 { 203 if (serviceName != null && isJBossInternalLifecycleExposed) 204 server.invoke(ServiceController.OBJECT_NAME, "stop", new Object [] { serviceName }, SERVICE_CONTROLLER_SIG); 205 else 206 jbossInternalStop(); 207 } 208 catch (Throwable t) 209 { 210 log.warn("Error in stop " + jbossInternalDescription(), t); 211 } 212 } 213 214 public void destroy() 215 { 216 try 217 { 218 if (serviceName != null && isJBossInternalLifecycleExposed) 219 server.invoke(ServiceController.OBJECT_NAME, "destroy", new Object [] { serviceName }, SERVICE_CONTROLLER_SIG); 220 else 221 jbossInternalDestroy(); 222 } 223 catch (Throwable t) 224 { 225 log.warn("Error in destroy " + jbossInternalDescription(), t); 226 } 227 } 228 229 protected String jbossInternalDescription() 230 { 231 if (serviceName != null) 232 return serviceName.toString(); 233 else 234 return getName(); 235 } 236 237 public void jbossInternalLifecycle(String method) throws Exception 238 { 239 if (method == null) 240 throw new IllegalArgumentException ("Null method name"); 241 242 if (method.equals("create")) 243 jbossInternalCreate(); 244 else if (method.equals("start")) 245 jbossInternalStart(); 246 else if (method.equals("stop")) 247 jbossInternalStop(); 248 else if (method.equals("destroy")) 249 jbossInternalDestroy(); 250 else 251 throw new IllegalArgumentException ("Unknown lifecyle method " + method); 252 } 253 254 protected void jbossInternalCreate() throws Exception 255 { 256 log.debug("Creating " + jbossInternalDescription()); 257 258 try 259 { 260 createService(); 261 state = CREATED; 262 } 263 catch (Exception e) 264 { 265 log.debug("Initialization failed " + jbossInternalDescription(), e); 266 throw e; 267 } 268 269 log.debug("Created " + jbossInternalDescription()); 270 } 271 272 protected void jbossInternalStart() throws Exception 273 { 274 if (state == STARTING || state == STARTED || state == STOPPING) 275 return; 276 277 if (state != CREATED && state != STOPPED && state != FAILED) 278 { 279 log.debug("Start requested before create, calling create now"); 280 create(); 281 } 282 283 state = STARTING; 284 sendStateChangeNotification(STOPPED, STARTING, getName() + " starting", null); 285 log.debug("Starting " + jbossInternalDescription()); 286 287 try 288 { 289 startService(); 290 } 291 catch (Exception e) 292 { 293 state = FAILED; 294 sendStateChangeNotification(STARTING, FAILED, getName() + " failed", e); 295 log.debug("Starting failed " + jbossInternalDescription(), e); 296 throw e; 297 } 298 299 state = STARTED; 300 sendStateChangeNotification(STARTING, STARTED, getName() + " started", null); 301 log.debug("Started " + jbossInternalDescription()); 302 } 303 304 protected void jbossInternalStop() 305 { 306 if (state != STARTED) 307 return; 308 309 state = STOPPING; 310 sendStateChangeNotification(STARTED, STOPPING, getName() + " stopping", null); 311 log.debug("Stopping " + jbossInternalDescription()); 312 313 try 314 { 315 stopService(); 316 } 317 catch (Throwable e) 318 { 319 state = FAILED; 320 sendStateChangeNotification(STOPPING, FAILED, getName() + " failed", e); 321 log.warn("Stopping failed " + jbossInternalDescription(), e); 322 return; 323 } 324 325 state = STOPPED; 326 sendStateChangeNotification(STOPPING, STOPPED, getName() + " stopped", null); 327 log.debug("Stopped " + jbossInternalDescription()); 328 } 329 330 protected void jbossInternalDestroy() 331 { 332 if (state == DESTROYED) 333 return; 334 335 if (state == STARTED) 336 { 337 log.debug("Destroy requested before stop, calling stop now"); 338 stop(); 339 } 340 341 log.debug("Destroying " + jbossInternalDescription()); 342 343 try 344 { 345 destroyService(); 346 } 347 catch (Throwable t) 348 { 349 log.warn("Destroying failed " + jbossInternalDescription(), t); 350 } 351 state = DESTROYED; 352 log.debug("Destroyed " + jbossInternalDescription()); 353 } 354 355 356 360 373 public ObjectName preRegister(MBeanServer server, ObjectName name) 374 throws Exception 375 { 376 this.server = server; 377 378 serviceName = getObjectName(server, name); 379 380 return serviceName; 381 } 382 383 public void postRegister(Boolean registrationDone) 384 { 385 if (!registrationDone.booleanValue()) 386 { 387 log.info( "Registration is not done -> stop" ); 388 stop(); 389 } 390 else 391 { 392 state = REGISTERED; 393 try 395 { 396 MBeanInfo info = server.getMBeanInfo(serviceName); 397 MBeanOperationInfo [] ops = info.getOperations(); 398 for (int i = 0; i < ops.length; ++i) 399 { 400 if (ops[i] != null && ServiceController.JBOSS_INTERNAL_LIFECYCLE.equals(ops[i].getName())) 401 { 402 isJBossInternalLifecycleExposed = true; 403 break; 404 } 405 } 406 } 407 catch (Throwable t) 408 { 409 log.warn("Unexcepted error accessing MBeanInfo for " + serviceName, t); 410 } 411 } 412 } 413 414 public void preDeregister() throws Exception 415 { 416 } 417 418 public void postDeregister() 419 { 420 server = null; 421 serviceName = null; 422 state = UNREGISTERED; 423 } 424 425 431 protected long getNextNotificationSequenceNumber() 432 { 433 return nextNotificationSequenceNumber(); 434 } 435 436 437 441 445 protected ObjectName getObjectName(MBeanServer server, ObjectName name) 446 throws MalformedObjectNameException 447 { 448 return name; 449 } 450 451 459 protected void createService() throws Exception {} 460 461 469 protected void startService() throws Exception {} 470 471 479 protected void stopService() throws Exception {} 480 481 489 protected void destroyService() throws Exception {} 490 491 493 496 private void sendStateChangeNotification(int oldState, int newState, String msg, Throwable t) 497 { 498 long now = System.currentTimeMillis(); 499 500 AttributeChangeNotification stateChangeNotification = new AttributeChangeNotification ( 501 this, 502 getNextNotificationSequenceNumber(), now, msg, 503 "State", "java.lang.Integer", 504 new Integer (oldState), new Integer (newState) 505 ); 506 stateChangeNotification.setUserData(t); 507 508 sendNotification(stateChangeNotification); 509 } 510 } 511 | Popular Tags |