1 25 26 package org.objectweb.petals.jbi.management.service; 27 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import java.util.Map.Entry; 35 36 import javax.management.MBeanServer ; 37 import javax.management.MalformedObjectNameException ; 38 import javax.management.ObjectName ; 39 40 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 41 import org.objectweb.fractal.fraclet.annotation.FractalComponent; 42 import org.objectweb.fractal.fraclet.annotation.Interface; 43 import org.objectweb.fractal.fraclet.annotation.LifeCycle; 44 import org.objectweb.fractal.fraclet.annotation.LifeCycleType; 45 import org.objectweb.fractal.fraclet.annotation.Monolog; 46 import org.objectweb.fractal.fraclet.annotation.Provides; 47 import org.objectweb.fractal.fraclet.annotation.Requires; 48 import org.objectweb.fractal.jmx.agent.AdminAttributes; 49 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle; 50 import org.objectweb.petals.jbi.component.lifecycle.Installer; 51 import org.objectweb.petals.jbi.component.lifecycle.LifeCycleAbstract; 52 import org.objectweb.petals.jbi.component.lifecycle.ServiceAssemblyLifeCycle; 53 import org.objectweb.petals.util.LoggingUtil; 54 import org.objectweb.util.monolog.api.Logger; 55 56 80 @FractalComponent 81 @Provides(interfaces=@Interface(name="service",signature=org.objectweb.petals.jbi.management.service.LifeCycleManagerService.class)) 82 public class LifeCycleManagerImpl implements LifeCycleManagerService { 83 84 private static final String DEPLOYMENT_NAME = "deployment"; 85 86 private static final String INSTALLATION_NAME = "installation"; 87 88 private final static String PETALS_DOMAIN = "Petals"; 89 90 private final static String TYPE = "type"; 91 92 private final static String NAME = "name"; 93 94 private final static String SERVICE_TYPE = "service"; 95 96 private final static String ADMIN_SERV_NAME = "Admin"; 97 98 private final static String INSTALLATION_SERV_NAME = "Installation"; 99 100 private final static String DEPLOYMENT_SERV_NAME = "Deployment"; 101 102 protected ObjectName addressName; 103 104 protected ObjectName adminName; 105 106 protected Map <ObjectName , ComponentLifeCycle> bindingCompoLifeCycles; 107 108 protected String containerName; 109 110 protected ObjectName deploymentName; 111 112 protected ObjectName directoryName; 113 114 protected Map <ObjectName , ComponentLifeCycle> engineCompoLifeCycles; 115 116 protected ObjectName installationName; 117 118 protected Map <ObjectName , Installer> installers; 119 120 protected ObjectName itineraryName; 121 122 125 @Monolog(name="logger") 126 protected Logger logger; 127 128 protected MBeanNamesImpl mBeanNames; 129 130 @Requires(name="adminAtt",signature=org.objectweb.fractal.jmx.agent.AdminAttributes.class) 131 protected AdminAttributes adminAttributes; 132 133 protected Map <String , ServiceAssemblyLifeCycle> serviceAssemblyLifeCycles; 134 135 protected Map <ObjectName , LifeCycleAbstract> systemServices; 136 137 protected ObjectName transporterName; 138 139 142 private LoggingUtil log; 143 144 149 public LifeCycleManagerImpl() { 150 151 mBeanNames = new MBeanNamesImpl("org.objectweb.petals"); 152 153 bindingCompoLifeCycles = Collections 154 .synchronizedMap(new HashMap <ObjectName , ComponentLifeCycle>()); 155 156 engineCompoLifeCycles = Collections 157 .synchronizedMap(new HashMap <ObjectName , ComponentLifeCycle>()); 158 159 systemServices = Collections 160 .synchronizedMap(new HashMap <ObjectName , LifeCycleAbstract>()); 161 162 serviceAssemblyLifeCycles = Collections 163 .synchronizedMap(new HashMap <String , ServiceAssemblyLifeCycle>()); 164 165 installers = Collections 166 .synchronizedMap(new HashMap <ObjectName , Installer>()); 167 168 deploymentName = mBeanNames 169 .createSystemComponentMBeanName(DEPLOYMENT_NAME); 170 171 installationName = mBeanNames 172 .createSystemComponentMBeanName(INSTALLATION_NAME); 173 } 174 175 181 public ObjectName getAdminServiceMBeanName() throws ManagementException { 182 ObjectName result = null; 183 Set objNames; 184 try { 185 Hashtable <String , String > attributes = new Hashtable <String , String >(); 186 attributes.put(NAME, ADMIN_SERV_NAME); 187 attributes.put(TYPE, SERVICE_TYPE); 188 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 189 objNames = adminAttributes.getRawMBeanServer().queryNames(objName, 190 null); 191 } catch (MalformedObjectNameException e) { 192 String msg = "Error during AdminService ObjName retrieval"; 193 log.error(msg, e); 194 throw new ManagementException(msg, e); 195 } catch (NullPointerException e) { 196 String msg = "Error during AdminService ObjName retrieval"; 197 log.error(msg, e); 198 throw new ManagementException(msg, e); 199 } 200 if (objNames != null && objNames.size() == 1) { 201 result = (ObjectName ) objNames.iterator().next(); 202 } else { 203 throw new ManagementException("AdminService Mbean can't be " 204 + "correctly retrieved"); 205 } 206 return result; 207 } 208 209 212 public ComponentLifeCycle getComponentByName(String name) { 213 log.call(); 214 215 ComponentLifeCycle on = null; 216 217 219 on = getBindingComponentByName(name); 220 221 if (on == null) { 222 224 on = getEngineComponentByName(name); 225 } 226 return on; 227 } 228 229 237 public ObjectName getDeploymentServiceMBeanName() 238 throws ManagementException { 239 ObjectName result = null; 240 Set objNames; 241 try { 242 Hashtable <String , String > attributes = new Hashtable <String , String >(); 243 attributes.put(NAME, DEPLOYMENT_SERV_NAME); 244 attributes.put(TYPE, SERVICE_TYPE); 245 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 246 objNames = adminAttributes.getRawMBeanServer().queryNames(objName, 247 null); 248 } catch (MalformedObjectNameException e) { 249 String msg = "Error during DeploymentService ObjName retrieval"; 250 log.error(msg, e); 251 throw new ManagementException(msg, e); 252 } catch (NullPointerException e) { 253 String msg = "Error during DeploymentService ObjName retrieval"; 254 log.error(msg, e); 255 throw new ManagementException(msg, e); 256 } 257 if (objNames != null && objNames.size() == 1) { 258 result = (ObjectName ) objNames.iterator().next(); 259 } else { 260 throw new ManagementException("DeploymentService Mbean can't be " 261 + "correctly retrieved"); 262 } 263 return result; 264 } 265 266 269 public ObjectName getInstallationServiceMBeanName() 270 throws ManagementException { 271 ObjectName result = null; 272 Set objNames; 273 try { 274 Hashtable <String , String > attributes = new Hashtable <String , String >(); 275 attributes.put(NAME, INSTALLATION_SERV_NAME); 276 attributes.put(TYPE, SERVICE_TYPE); 277 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 278 objNames = adminAttributes.getRawMBeanServer().queryNames(objName, 279 null); 280 } catch (MalformedObjectNameException e) { 281 String msg = "Error during InstallationService ObjName retrieval"; 282 log.error(msg, e); 283 throw new ManagementException(msg, e); 284 } catch (NullPointerException e) { 285 String msg = "Error during InstallationService ObjName retrieval"; 286 log.error(msg, e); 287 throw new ManagementException(msg, e); 288 } 289 if (objNames != null && objNames.size() == 1) { 290 result = (ObjectName ) objNames.iterator().next(); 291 } else { 292 throw new ManagementException("InstallationService Mbean can't be " 293 + "correctly retrieved"); 294 } 295 return result; 296 } 297 298 302 309 @SuppressWarnings ("unchecked") 310 public ObjectName getInstallerByName(String name) { 311 log.call(); 312 313 ObjectName result = null; 314 315 Map <ObjectName , Installer> installers = getInstallers(); 316 317 synchronized (installers) { 318 for (Iterator iter = installers.entrySet().iterator(); iter 319 .hasNext() 320 && result == null;) { 321 Entry<ObjectName , Installer> entry = (Entry<ObjectName , Installer>) iter 322 .next(); 323 ObjectName objName = entry.getKey(); 324 Installer installer = entry.getValue(); 325 326 if (installer.getComponentName().equals(name)) { 327 result = objName; 328 } 329 } 330 } 331 return result; 332 } 333 334 340 public MBeanNamesImpl getMBeanNames() { 341 return mBeanNames; 342 } 343 344 349 public MBeanServer getMBeanServer() { 350 return adminAttributes.getRawMBeanServer(); 351 } 352 353 359 public ServiceAssemblyLifeCycle getServiceAssemblyByName(String saName) { 360 log.call(); 361 362 ServiceAssemblyLifeCycle result = null; 363 364 Map <String , ServiceAssemblyLifeCycle> sas = this 365 .getServiceAssemblyLifeCycles(); 366 367 synchronized (sas) { 368 result = sas.get(saName); 369 } 370 return result; 371 } 372 373 378 public Map <ObjectName , ComponentLifeCycle> getBindingCompoLifeCycles() { 379 return bindingCompoLifeCycles; 380 } 381 382 387 public Map <ObjectName , ComponentLifeCycle> getEngineCompoLifeCycles() { 388 return engineCompoLifeCycles; 389 } 390 391 396 public Map <ObjectName , Installer> getInstallers() { 397 return installers; 398 } 399 400 405 public Map <String , ServiceAssemblyLifeCycle> getServiceAssemblyLifeCycles() { 406 return serviceAssemblyLifeCycles; 407 } 408 409 414 public Map <ObjectName , LifeCycleAbstract> getSystemServices() { 415 return systemServices; 416 } 417 418 425 public ObjectName registerBindingComponent(ComponentLifeCycle lifeCycle) 426 throws ManagementException { 427 428 ObjectName name = lifeCycle.getMBeanName(); 429 430 Map <ObjectName , ComponentLifeCycle> bindings = getBindingCompoLifeCycles(); 431 432 synchronized (bindings) { 433 try { 434 adminAttributes.getRawMBeanServer().registerMBean(lifeCycle, 435 name); 436 437 bindings.put(name, lifeCycle); 438 } catch (Exception e) { 439 throw new ManagementException(e); 440 } 441 } 442 return name; 443 } 444 445 452 public ObjectName registerEngineComponent(ComponentLifeCycle lifeCycle) 453 throws ManagementException { 454 ObjectName name = lifeCycle.getMBeanName(); 455 456 Map <ObjectName , ComponentLifeCycle> engines = getEngineCompoLifeCycles(); 457 458 synchronized (engines) { 459 try { 460 adminAttributes.getRawMBeanServer().registerMBean(lifeCycle, 461 name); 462 463 engines.put(name, lifeCycle); 464 } catch (Exception e) { 465 throw new ManagementException(e); 466 } 467 } 468 return name; 469 } 470 471 481 public ObjectName registerInstaller(Installer installerMBean) 482 throws ManagementException { 483 484 ObjectName installerMBeanName = installerMBean.getMBeanName(); 485 486 Map <ObjectName , Installer> installers = getInstallers(); 487 488 synchronized (installers) { 489 try { 490 adminAttributes.getRawMBeanServer().registerMBean( 491 installerMBean, installerMBeanName); 492 493 installers.put(installerMBeanName, installerMBean); 494 } catch (Exception e) { 495 throw new ManagementException(e); 496 } 497 } 498 return installerMBeanName; 499 } 500 501 510 public void registerServiceAssembly(String saName, 511 ServiceAssemblyLifeCycle serviceAssemblyLifeCycle) { 512 Map <String , ServiceAssemblyLifeCycle> sas = getServiceAssemblyLifeCycles(); 513 synchronized (sas) { 514 sas.put(saName, serviceAssemblyLifeCycle); 515 } 516 } 517 518 521 @LifeCycle(on=LifeCycleType.START) 522 public void start() throws IllegalLifeCycleException { 523 log = new LoggingUtil(logger); 524 } 525 526 530 535 public void unregisterBindingComponent(ObjectName name) 536 throws ManagementException { 537 Map bindings = getBindingCompoLifeCycles(); 538 539 synchronized (bindings) { 540 try { 541 adminAttributes.getRawMBeanServer().unregisterMBean(name); 542 543 bindings.remove(name); 544 } catch (Exception e) { 545 throw new ManagementException(e); 546 } 547 } 548 } 549 550 555 public void unregisterEngineComponent(ObjectName name) 556 throws ManagementException { 557 Map engines = getEngineCompoLifeCycles(); 558 559 synchronized (engines) { 560 try { 561 adminAttributes.getRawMBeanServer().unregisterMBean(name); 562 563 engines.remove(name); 564 } catch (Exception e) { 565 throw new ManagementException(e); 566 } 567 } 568 } 569 570 574 579 public void unregisterInstaller(ObjectName name) throws ManagementException { 580 Map <ObjectName , Installer> installers = getInstallers(); 581 582 synchronized (installers) { 583 try { 584 adminAttributes.getRawMBeanServer().unregisterMBean(name); 585 586 installers.remove(name); 587 } catch (Exception e) { 588 throw new ManagementException(e); 589 } 590 } 591 } 592 593 598 public void unregisterServiceAssembly(String saName) 599 throws ManagementException { 600 Map sas = getServiceAssemblyLifeCycles(); 601 602 synchronized (sas) { 603 try { 604 sas.remove(saName); 605 } catch (Exception e) { 606 throw new ManagementException(e); 607 } 608 } 609 } 610 611 617 private ComponentLifeCycle getBindingComponentByName(String name) { 618 log.call(); 619 620 ComponentLifeCycle result = null; 621 622 Map bindings = this.getBindingCompoLifeCycles(); 623 624 synchronized (bindings) { 625 for (Iterator iter = bindings.values().iterator(); iter.hasNext() 626 && result == null;) { 627 ComponentLifeCycle lifeCycle = (ComponentLifeCycle) iter.next(); 628 629 if (lifeCycle.getName().equals(name)) { 630 result = lifeCycle; 631 } 632 } 633 } 634 return result; 635 } 636 637 643 private ComponentLifeCycle getEngineComponentByName(String name) { 644 log.call(); 645 646 ComponentLifeCycle result = null; 647 648 Map engines = this.getEngineCompoLifeCycles(); 649 650 synchronized (engines) { 651 for (Iterator iter = engines.values().iterator(); iter.hasNext() 652 && result == null;) { 653 ComponentLifeCycle lifeCycle = (ComponentLifeCycle) iter.next(); 654 655 if (lifeCycle.getName().equals(name)) { 656 result = lifeCycle; 657 } 658 } 659 } 660 return result; 661 } 662 } 663 | Popular Tags |