1 22 package org.objectweb.petals.jbi.management.deployment; 23 24 import java.net.URI ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import javax.jbi.management.AdminServiceMBean; 34 import javax.jbi.management.LifeCycleMBean; 35 36 import org.objectweb.fractal.fraclet.annotation.FractalComponent; 37 import org.objectweb.fractal.fraclet.annotation.Interface; 38 import org.objectweb.fractal.fraclet.annotation.LifeCycle; 39 import org.objectweb.fractal.fraclet.annotation.LifeCycleType; 40 import org.objectweb.fractal.fraclet.annotation.Monolog; 41 import org.objectweb.fractal.fraclet.annotation.Provides; 42 import org.objectweb.fractal.fraclet.annotation.Requires; 43 import org.objectweb.petals.PetalsException; 44 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle; 45 import org.objectweb.petals.jbi.component.lifecycle.LifeCycleAbstract; 46 import org.objectweb.petals.jbi.component.lifecycle.ServiceAssemblyLifeCycle; 47 import org.objectweb.petals.jbi.management.service.EndpointService; 48 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService; 49 import org.objectweb.petals.jbi.management.service.PackageHandler; 50 import org.objectweb.petals.jbi.management.service.util.XMLResult; 51 import org.objectweb.petals.jbi.management.service.util.XMLResult.CauseFramework; 52 import org.objectweb.petals.jbi.management.service.util.XMLResult.MessageType; 53 import org.objectweb.petals.jbi.management.service.util.XMLResult.TaskResult; 54 import org.objectweb.petals.jbi.management.systemstate.SystemState; 55 import org.objectweb.petals.processor.TaskProcessor; 56 import org.objectweb.petals.repository.RepositoryService; 57 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptorBuilder; 58 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceUnit; 59 import org.objectweb.petals.util.LoggingUtil; 60 import org.objectweb.petals.util.ParameterCheckHelper; 61 import org.objectweb.util.monolog.api.Logger; 62 63 70 @FractalComponent 71 @Provides(interfaces = @Interface(name = "service", signature = org.objectweb.petals.jbi.management.deployment.DeploymentService.class)) 72 public class DeploymentServiceImpl implements DeploymentService { 73 74 77 @Requires(name = "admin", signature = javax.jbi.management.AdminServiceMBean.class) 78 protected AdminServiceMBean adminSrv; 79 80 83 protected Map <String , ServiceAssemblyDataHandler> deployedSACache; 84 85 @Requires(name = "endpoint-service", signature = org.objectweb.petals.jbi.management.service.EndpointService.class) 86 protected EndpointService endpointService; 87 88 91 protected LoggingUtil log; 92 93 96 @Monolog(name = "logger") 97 protected Logger logger; 98 99 102 @Requires(name = "lifecyclemanager", signature = org.objectweb.petals.jbi.management.service.LifeCycleManagerService.class) 103 protected LifeCycleManagerService managerService; 104 105 108 protected PackageHandler packageHandler; 109 110 113 @Requires(name = "systemstate", signature = org.objectweb.petals.jbi.management.systemstate.SystemState.class) 114 protected SystemState recoverySrv; 115 116 119 @Requires(name = "repository", signature = org.objectweb.petals.repository.RepositoryService.class) 120 protected RepositoryService repositorySrv; 121 122 protected DeploymentTaskFactory deploymentTaskFactory; 123 124 127 public DeploymentServiceImpl() { 128 } 129 130 135 public boolean canDeployToComponent(String componentName) { 136 ParameterCheckHelper.isNullParameterWithLog(componentName, 137 "componentName", log); 138 ParameterCheckHelper.isEmptyParameterWithLog(componentName, 139 "componentName", log); 140 boolean result = false; 141 ComponentLifeCycle clc = managerService 142 .getComponentByName(componentName); 143 result = clc != null 144 && LifeCycleAbstract.STARTED.equals(clc.getCurrentState()) 145 && clc.getComponent().getServiceUnitManager() != null; 146 return result; 147 } 148 149 154 public String deploy(String serviceAssemblyZipUrl) throws Exception { 155 log.start(); 156 String result = null; 157 158 161 ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyZipUrl, 162 "serviceAssemblyZipUrl", log); 163 ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyZipUrl, 164 "serviceAssemblyZipUrl", log); 165 166 try { 167 168 171 HashMap <String , Object > deploymentContext = createDeploymentContext(serviceAssemblyZipUrl); 172 173 176 TaskProcessor processor = createDeploymentProcessor(deploymentContext); 177 178 181 if (!processor.process()) { 182 String msg = "An error occured during the deployment " 183 + "of the following service assembly: " 184 + serviceAssemblyZipUrl; 185 throw new Exception (msg); 186 } 187 188 191 result = ((XMLResult) deploymentContext 192 .get(DeploymentContextConstants.XML_RESULT)).getString(); 193 194 } catch (Throwable e) { 195 log.error(e.getMessage(), e); 198 throw new Exception (e.getMessage()); 199 } 200 201 log.end(); 203 return result; 204 } 205 206 214 protected TaskProcessor createDeploymentProcessor( 215 HashMap <String , Object > deploymentContext) { 216 TaskProcessor processor = new TaskProcessor(deploymentContext, log); 217 218 processor.addTask(deploymentTaskFactory 219 .getDescriptorAndURIRetrievalTask()); 220 221 processor.addTask(deploymentTaskFactory.getPackageCheckingTask()); 222 223 processor.addTask(deploymentTaskFactory.getResultCreationTask()); 224 processor.addTask(deploymentTaskFactory.getExtractionTask()); 225 processor.addTask(deploymentTaskFactory 226 .getAllConnectionRegistrationTask()); 227 228 processor.addTask(deploymentTaskFactory.getAllSUDeploymentTask()); 229 processor.addTask(deploymentTaskFactory 230 .getSaLifeCycleRegistrationTask()); 231 232 processor.addTask(deploymentTaskFactory.getStateHolderCreationTask()); 233 processor.addTask(deploymentTaskFactory 234 .getCopyArchiveToInstalledDirTask()); 235 236 processor.addTask(deploymentTaskFactory.getDeploymentSuccessTask()); 237 238 return processor; 239 } 240 241 249 protected HashMap <String , Object > createDeploymentContext( 250 String serviceAssemblyZipUrl) { 251 HashMap <String , Object > deploymentContext = new HashMap <String , Object >(); 252 deploymentContext.put(DeploymentContextConstants.ARCHIVE_URL, 253 serviceAssemblyZipUrl); 254 return deploymentContext; 255 } 256 257 262 public String [] getComponentsForDeployedServiceAssembly( 263 String serviceAssemblyName) throws Exception { 264 ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName, 265 "serviceAssemblyName", log); 266 ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName, 267 "serviceAssemblyName", log); 268 List <String > result = new ArrayList <String >(); 269 ServiceAssemblyLifeCycle saLifeCycle = managerService 270 .getServiceAssemblyByName(serviceAssemblyName); 271 if (saLifeCycle != null) { 272 List <ServiceUnit> sus = saLifeCycle.getServiceAssembly() 273 .getServiceUnits(); 274 for (ServiceUnit unit : sus) { 275 result.add(unit.getTargetComponentName()); 276 } 277 } else { 278 String msg = "Cannot retrieve service assembly with the specified name : " 279 + serviceAssemblyName; 280 log.error(msg); 281 throw new Exception (msg); 282 } 283 return result.toArray(new String [0]); 284 } 285 286 public Map <String , ServiceAssemblyDataHandler> getDeployedSACache() { 287 return deployedSACache; 288 } 289 290 295 public String [] getDeployedServiceAssemblies() throws Exception { 296 Set <String > sas = managerService.getServiceAssemblyLifeCycles() 297 .keySet(); 298 return sas.toArray(new String [0]); 299 } 300 301 306 public String [] getDeployedServiceAssembliesForComponent( 307 String componentName) throws Exception { 308 ParameterCheckHelper.isNullParameterWithLog(componentName, 309 "componentName", log); 310 ParameterCheckHelper.isEmptyParameterWithLog(componentName, 311 "componentName", log); 312 Set <String > result = new HashSet <String >(); 313 Map <String , ServiceAssemblyLifeCycle> saLifeCycles = managerService 314 .getServiceAssemblyLifeCycles(); 315 for (ServiceAssemblyLifeCycle salc : saLifeCycles.values()) { 316 List <ServiceUnit> sus = salc.getServiceAssembly().getServiceUnits(); 317 for (ServiceUnit unit : sus) { 318 if (componentName.equals(unit.getTargetComponentName())) { 319 result.add(salc.getServiceAssembly().getIdentification() 320 .getName()); 321 } 322 } 323 } 324 return result.toArray(new String [0]); 325 } 326 327 332 public String [] getDeployedServiceUnitList(String componentName) 333 throws Exception { 334 ParameterCheckHelper.isNullParameterWithLog(componentName, 335 "componentName", log); 336 ParameterCheckHelper.isEmptyParameterWithLog(componentName, 337 "componentName", log); 338 Set <String > result = new HashSet <String >(); 339 Map <String , ServiceAssemblyLifeCycle> saLifeCycles = managerService 340 .getServiceAssemblyLifeCycles(); 341 for (ServiceAssemblyLifeCycle salc : saLifeCycles.values()) { 342 List <ServiceUnit> sus = salc.getServiceAssembly().getServiceUnits(); 343 for (ServiceUnit unit : sus) { 344 if (componentName.equals(unit.getTargetComponentName())) { 345 result.add(unit.getIdentification().getName()); 346 } 347 } 348 } 349 return result.toArray(new String [0]); 350 } 351 352 public String getServiceAssemblyDescriptor(String serviceAssemblyName) 353 throws Exception { 354 ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName, 355 "serviceAssemblyName", log); 356 ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName, 357 "serviceAssemblyName", log); 358 String result = "Service Assembly doesn't exist :" 359 + serviceAssemblyName; 360 ServiceAssemblyLifeCycle saLifeCycle = managerService 361 .getServiceAssemblyByName(serviceAssemblyName); 362 if (saLifeCycle != null) { 363 URI installationRoot = saLifeCycle.getInstallationRoot(); 364 URI jbiXmlUri = installationRoot 366 .resolve(JBIDescriptorBuilder.JBI_DESCRIPTOR_RESOURCE 367 .replace('\\', '/')); 368 result = getJbiDescriptorAsString(jbiXmlUri); 369 } 370 return result; 371 } 372 373 378 public String getState(String serviceAssemblyName) throws Exception { 379 ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName, 380 "serviceAssemblyName", log); 381 ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName, 382 "serviceAssemblyName", log); 383 String result = null; 384 String msg; 385 ServiceAssemblyLifeCycle saLifeCycle = managerService 386 .getServiceAssemblyByName(serviceAssemblyName); 387 if (saLifeCycle != null) { 388 result = saLifeCycle.getCurrentState(); 389 } else { 390 msg = "Cannot retrieve service " 391 + "assembly with the specified name : " 392 + serviceAssemblyName; 393 log.error(msg); 394 throw new Exception (msg); 395 } 396 if (!(LifeCycleMBean.SHUTDOWN.equals(result) 397 || LifeCycleMBean.STARTED.equals(result) || LifeCycleMBean.STOPPED 398 .equals(result))) { 399 msg = "Service Assembly state isn't correctly defined"; 400 log.error(msg); 401 throw new Exception (msg); 402 } 403 return result; 404 } 405 406 412 public boolean isDeployedServiceUnit(String componentName, 413 String serviceUnitName) throws Exception { 414 ParameterCheckHelper.isNullParameterWithLog(serviceUnitName, 415 "serviceUnitName", log); 416 ParameterCheckHelper.isEmptyParameterWithLog(serviceUnitName, 417 "serviceUnitName", log); 418 ParameterCheckHelper.isNullParameterWithLog(componentName, 419 "componentName", log); 420 ParameterCheckHelper.isEmptyParameterWithLog(componentName, 421 "componentName", log); 422 boolean result = false; 423 Map <String , ServiceAssemblyLifeCycle> saLifeCycles = managerService 424 .getServiceAssemblyLifeCycles(); 425 for (Iterator iter = saLifeCycles.values().iterator(); iter.hasNext() 426 && !result;) { 427 ServiceAssemblyLifeCycle salc = (ServiceAssemblyLifeCycle) iter 428 .next(); 429 List <ServiceUnit> sus = salc.getServiceAssembly().getServiceUnits(); 430 for (Iterator iter2 = sus.iterator(); iter2.hasNext() && !result;) { 431 ServiceUnit unit = (ServiceUnit) iter2.next(); 432 if (componentName.equals(unit.getTargetComponentName()) 433 && serviceUnitName.equals(unit.getIdentification() 434 .getName())) { 435 result = true; 436 } 437 } 438 } 439 return result; 440 } 441 442 447 public String shutDown(String serviceAssemblyName) throws Exception { 448 ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName, 449 "serviceAssemblyName", log); 450 ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName, 451 "serviceAssemblyName", log); 452 log.start(); 453 454 String msg = null; 455 XMLResult xmlResult = new XMLResult("shutdown"); 456 457 ServiceAssemblyLifeCycle saLifeCycle = managerService 458 .getServiceAssemblyByName(serviceAssemblyName); 459 if (saLifeCycle != null) { 460 try { 461 saLifeCycle.shutDown(); 462 } catch (Throwable e) { 463 msg = e.getMessage(); 464 log.error(msg, e); 465 throw new Exception (msg); 466 } 467 } else { 468 msg = "The service assembly \"" + serviceAssemblyName 469 + "\" is not deployed"; 470 throw new Exception (msg); 471 } 472 xmlResult.addFrameworkTaskResult(TaskResult.SUCCESS, MessageType.INFO, 473 "Successfully shutdown service assembly : {1}", 474 new String [] { serviceAssemblyName }, CauseFramework.YES); 475 String output = xmlResult.getString(); 476 try { 477 XMLResult.validateManagementMessage(output, this.getClass() 478 .getClassLoader().getResourceAsStream( 479 "schema/management-message.xsd")); 480 } catch (Exception e1) { 481 log.error("Problem while validating management message", e1); 482 } 483 log.info(serviceAssemblyName+" successfully shutdown"); 484 log.end(); 485 return output; 486 } 487 488 493 public String start(String serviceAssemblyName) throws Exception { 494 ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName, 495 "serviceAssemblyName", log); 496 ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName, 497 "serviceAssemblyName", log); 498 log.start(); 499 500 String msg = null; 501 XMLResult xmlResult = new XMLResult("start"); 502 503 ServiceAssemblyLifeCycle saLifeCycle = managerService 504 .getServiceAssemblyByName(serviceAssemblyName); 505 if (saLifeCycle != null) { 506 try { 507 saLifeCycle.start(); 508 } catch (Exception e) { 509 e.printStackTrace(); 510 msg = e.getMessage(); 511 throw new Exception (msg); 512 } 513 } else { 514 msg = "The service assembly \"" + serviceAssemblyName 515 + "\" is not deployed"; 516 throw new Exception (msg); 517 } 518 xmlResult.addFrameworkTaskResult(TaskResult.SUCCESS, MessageType.INFO, 519 "Successfully started service assembly : {1}", 520 new String [] { serviceAssemblyName }, CauseFramework.YES); 521 String output = xmlResult.getString(); 522 try { 523 XMLResult.validateManagementMessage(output, this.getClass() 524 .getClassLoader().getResourceAsStream( 525 "schema/management-message.xsd")); 526 } catch (Exception e1) { 527 log.error("Problem while validating management message", e1); 528 } 529 530 log.info(serviceAssemblyName+" successfully started"); 531 log.end(); 532 return output; 533 } 534 535 540 public String stop(String serviceAssemblyName) throws Exception { 541 ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName, 542 "serviceAssemblyName", log); 543 ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName, 544 "serviceAssemblyName", log); 545 log.start(); 546 547 String msg = null; 548 XMLResult xmlResult = new XMLResult("stop"); 549 550 ServiceAssemblyLifeCycle saLifeCycle = managerService 551 .getServiceAssemblyByName(serviceAssemblyName); 552 if (saLifeCycle != null) { 553 try { 554 saLifeCycle.stop(); 555 } catch (Throwable e) { 556 msg = e.getMessage(); 557 log.error(msg, e); 558 throw new Exception (msg); 559 } 560 } else { 561 msg = "The service assembly \"" + serviceAssemblyName 562 + "\" is not deployed"; 563 throw new Exception (msg); 564 } 565 xmlResult.addFrameworkTaskResult(TaskResult.SUCCESS, MessageType.INFO, 566 "Successfully stopped service assembly : {1}", 567 new String [] { serviceAssemblyName }, CauseFramework.YES); 568 String output = xmlResult.getString(); 569 try { 570 XMLResult.validateManagementMessage(output, this.getClass() 571 .getClassLoader().getResourceAsStream( 572 "schema/management-message.xsd")); 573 } catch (Exception e1) { 574 log.error("Problem while validating management message", e1); 575 } 576 577 log.info(serviceAssemblyName+" successfully stoped"); 578 log.end(); 579 return output; 580 } 581 582 587 public String undeploy(String serviceAssemblyName) throws Exception { 588 log.start(); 589 590 String output = ""; 591 592 ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName, 593 "serviceAssemblyZipUrl", log); 594 ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName, 595 "serviceAssemblyZipUrl", log); 596 try { 597 598 601 HashMap <String , Object > undeploymentContext = createUndeploymentContext(serviceAssemblyName); 602 603 606 TaskProcessor processor = createUndeploymentProcessor(undeploymentContext); 607 608 611 if (!processor.process()) { 612 String msg = "An error occured during the undeployment " 613 + "of the following service assembly: " 614 + serviceAssemblyName; 615 throw new Exception (msg); 616 } 617 618 621 output = ((XMLResult) undeploymentContext 622 .get(DeploymentContextConstants.XML_RESULT)).getString(); 623 624 } catch (Throwable e) { 625 log.error(e.getMessage(), e); 628 throw new Exception (e.getMessage()); 629 } 630 631 log.info(serviceAssemblyName+" successfully undeployed"); 632 log.end(); 633 return output; 634 } 635 636 644 protected TaskProcessor createUndeploymentProcessor( 645 HashMap <String , Object > undeploymentContext) { 646 TaskProcessor processor = new TaskProcessor(undeploymentContext, log); 647 648 processor.addTask(deploymentTaskFactory 649 .getXmlUndeploymentResultCreationTask()); 650 processor.addTask(deploymentTaskFactory.getSaLifeCycleRetrievalTask()); 651 processor.addTask(deploymentTaskFactory.getAllSUUndeploymentTask()); 652 processor.addTask(deploymentTaskFactory.getAllConnectionsRemovalTask()); 653 processor.addTask(deploymentTaskFactory 654 .getSaLifeCycleUnregistrationTask()); 655 processor.addTask(deploymentTaskFactory.getSaStateHolderRemovalTask()); 656 processor.addTask(deploymentTaskFactory.getSaCacheRemovalTask()); 657 processor.addTask(deploymentTaskFactory.getSaFolderRemovalTask()); 658 processor.addTask(deploymentTaskFactory.getDeleteArchiveFromInstalledDirTask()); 659 660 return processor; 661 } 662 663 672 protected HashMap <String , Object > createUndeploymentContext( 673 String serviceAssemblyName) throws PetalsException { 674 HashMap <String , Object > undeploymentContext = new HashMap <String , Object >(); 675 676 ServiceAssemblyDataHandler handler = deployedSACache 677 .get(serviceAssemblyName); 678 679 undeploymentContext.put(DeploymentContextConstants.SA_DATA_HANDLER, 680 handler); 681 return undeploymentContext; 682 } 683 684 692 protected String getJbiDescriptorAsString(URI jbiXmlUri) { 693 JBIDescriptorBuilder builder = new JBIDescriptorBuilder(jbiXmlUri, 694 (java.util.logging.Logger ) logger); 695 return builder.getJbiDescriptorAsString(); 696 } 697 698 @LifeCycle(on = LifeCycleType.START) 699 protected void start() { 700 log = new LoggingUtil(logger); 701 log.start(); 702 703 706 deployedSACache = new HashMap <String , ServiceAssemblyDataHandler>(); 707 708 711 packageHandler = new PackageHandler(logger, repositorySrv); 712 713 716 deploymentTaskFactory = new DeploymentTaskFactory(packageHandler, 717 endpointService, this, recoverySrv, repositorySrv, log, 718 managerService); 719 720 log.end(); 721 } 722 723 @LifeCycle(on = LifeCycleType.STOP) 724 public void stop() { 725 log.start(); 726 727 logger = null; 728 729 deployedSACache = null; 730 731 log.end(); 732 } 733 734 737 public void shutdown() throws Exception { 738 Map <String , ServiceAssemblyDataHandler> removeList = new HashMap <String , ServiceAssemblyDataHandler>( 740 deployedSACache); 741 for (String saName : removeList.keySet()) { 742 undeploy(saName); 743 } 744 } 745 746 public DeploymentTaskFactory getDeploymentTaskFactory() { 747 return deploymentTaskFactory; 748 } 749 750 } 751 | Popular Tags |