1 17 package org.apache.servicemix.jbi.framework; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.StringReader ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import javax.jbi.JBIException; 27 import javax.jbi.component.ServiceUnitManager; 28 import javax.jbi.management.DeploymentException; 29 import javax.jbi.management.DeploymentServiceMBean; 30 import javax.management.JMException ; 31 import javax.management.MBeanAttributeInfo ; 32 import javax.management.MBeanOperationInfo ; 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.apache.servicemix.jbi.container.EnvironmentContext; 40 import org.apache.servicemix.jbi.container.JBIContainer; 41 import org.apache.servicemix.jbi.container.ServiceAssemblyEnvironment; 42 import org.apache.servicemix.jbi.deployment.Descriptor; 43 import org.apache.servicemix.jbi.deployment.DescriptorFactory; 44 import org.apache.servicemix.jbi.deployment.ServiceAssembly; 45 import org.apache.servicemix.jbi.deployment.ServiceUnit; 46 import org.apache.servicemix.jbi.management.AttributeInfoHelper; 47 import org.apache.servicemix.jbi.management.BaseSystemService; 48 import org.apache.servicemix.jbi.management.OperationInfoHelper; 49 import org.apache.servicemix.jbi.management.ParameterHelper; 50 import org.apache.servicemix.jbi.util.DOMUtil; 51 import org.apache.servicemix.jbi.util.FileUtil; 52 import org.w3c.dom.Document ; 53 import org.w3c.dom.Element ; 54 import org.w3c.dom.NodeList ; 55 import org.xml.sax.InputSource ; 56 import org.xml.sax.SAXException ; 57 58 63 public class DeploymentService extends BaseSystemService implements DeploymentServiceMBean { 64 65 private static final Log log = LogFactory.getLog(DeploymentService.class); 66 private EnvironmentContext environmentContext; 67 private Registry registry; 68 69 73 80 public void init(JBIContainer container) throws JBIException { 81 this.environmentContext = container.getEnvironmentContext(); 82 this.registry = container.getRegistry(); 83 super.init(container); 84 buildState(); 85 } 86 87 protected Class getServiceMBean() { 88 return DeploymentServiceMBean.class; 89 } 90 91 public void start() throws javax.jbi.JBIException { 92 super.start(); 93 String [] sas = registry.getDeployedServiceAssemblies(); 94 for (int i = 0; i < sas.length; i++) { 95 try { 96 ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(sas[i]); 97 sa.restore(); 98 } catch (Exception e) { 99 log.error("Unable to restore state for service assembly " + sas[i], e); 100 } 101 } 102 } 103 104 110 public MBeanAttributeInfo [] getAttributeInfos() throws JMException { 111 AttributeInfoHelper helper = new AttributeInfoHelper(); 112 helper.addAttribute(getObjectToManage(), "deployedServiceAssemblies", "list of deployed SAs"); 113 return AttributeInfoHelper.join(super.getAttributeInfos(), helper.getAttributeInfos()); 114 } 115 116 122 public MBeanOperationInfo [] getOperationInfos() throws JMException { 123 OperationInfoHelper helper = new OperationInfoHelper(); 124 ParameterHelper ph = helper.addOperation(getObjectToManage(), "deploy", 1, "deploy An SA"); 125 ph.setDescription(0, "saZipURL", "location of SA zip file"); 126 ph = helper.addOperation(getObjectToManage(), "undeploy", 1, "undeploy An SA"); 127 ph.setDescription(0, "saName", "SA name"); 128 ph = helper.addOperation(getObjectToManage(), "getDeployedServiceUnitList", 1, 129 "list of SU's currently deployed"); 130 ph.setDescription(0, "componentName", "Component name"); 131 ph = helper.addOperation(getObjectToManage(), "getServiceAssemblyDescriptor", 1, "Get descriptor for a SA"); 132 ph.setDescription(0, "saName", "SA name"); 133 ph = helper.addOperation(getObjectToManage(), "getDeployedServiceAssembliesForComponent", 1, 134 "list of SA's for a Component"); 135 ph.setDescription(0, "componentName", "Component name"); 136 ph = helper.addOperation(getObjectToManage(), "getComponentsForDeployedServiceAssembly", 1, 137 "list of Components for a SA"); 138 ph.setDescription(0, "saName", "SA name"); 139 ph = helper.addOperation(getObjectToManage(), "isDeployedServiceUnit", 2, "is SU deployed at a Component ?"); 140 ph.setDescription(0, "componentName", "Component name"); 141 ph.setDescription(1, "suName", "SU name"); 142 ph = helper 143 .addOperation(getObjectToManage(), "canDeployToComponent", 1, "Can a SU be deployed to a Component?"); 144 ph.setDescription(0, "componentName", "Component name"); 145 ph = helper.addOperation(getObjectToManage(), "start", 1, "start an SA"); 146 ph.setDescription(0, "saName", "SA name"); 147 ph = helper.addOperation(getObjectToManage(), "stop", 1, "stop an SA"); 148 ph.setDescription(0, "saName", "SA name"); 149 ph = helper.addOperation(getObjectToManage(), "shutDown", 1, "shutDown an SA"); 150 ph.setDescription(0, "saName", "SA name"); 151 ph = helper.addOperation(getObjectToManage(), "getState", 1, "Running state of an SA"); 152 ph.setDescription(0, "saName", "SA name"); 153 return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos()); 154 } 155 160 public String getDescription() { 161 return "Allows admin tools to manage service deployments"; 162 } 163 164 168 175 public String deploy(String saZipURL) throws Exception { 176 try { 177 if (saZipURL == null) { 178 throw ManagementSupport.failure("deploy", "saZipURL must not be null"); 179 } 180 File tmpDir = null; 181 try { 182 tmpDir = AutoDeploymentService.unpackLocation(environmentContext.getTmpDir(), saZipURL); 183 } catch (Exception e) { 184 throw ManagementSupport.failure("deploy", "Unable to unpack archive: " + saZipURL, e); 185 } 186 if (tmpDir == null) { 188 throw ManagementSupport.failure("deploy", "Unable to find jbi descriptor: " + saZipURL); 189 } 190 Descriptor root = null; 191 try { 192 root = DescriptorFactory.buildDescriptor(tmpDir); 193 } catch (Exception e) { 194 throw ManagementSupport.failure("deploy", "Unable to build jbi descriptor: " + saZipURL, e); 195 } 196 if (root == null) { 197 throw ManagementSupport.failure("deploy", "Unable to find jbi descriptor: " + saZipURL); 198 } 199 ServiceAssembly sa = root.getServiceAssembly(); 200 if (sa == null) { 201 throw ManagementSupport.failure("deploy", "JBI descriptor is not an assembly descriptor: " + saZipURL); 202 } 203 return deployServiceAssembly(tmpDir, sa); 204 } catch (Exception e) { 205 log.error("Error deploying service assembly", e); 206 throw e; 207 } 208 } 209 210 217 public String undeploy(String saName) throws Exception { 218 if (saName == null) { 219 throw ManagementSupport.failure("undeploy", "SA name must not be null"); 220 } 221 ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(saName); 222 if (sa == null) { 223 throw ManagementSupport.failure("undeploy", "SA has not been deployed: " + saName); 224 } 225 String state = sa.getCurrentState(); 226 if (!DeploymentServiceMBean.SHUTDOWN.equals(state)) { 227 throw ManagementSupport.failure("undeploy", "SA must be shut down: " + saName); 228 } 229 try { 230 try { 233 sa.shutDown(); 234 } catch (Exception e) { 235 } 236 237 String result = null; 238 String assemblyName = sa.getName(); 239 registry.unregisterServiceAssembly(assemblyName); 240 ServiceUnitLifeCycle[] sus = sa.getDeployedSUs(); 241 if (sus != null) { 242 for (int i = 0;i < sus.length; i++) { 243 undeployServiceUnit(sus[i]); 244 } 245 } 246 FileUtil.deleteFile(sa.getEnvironment().getRootDir()); 247 248 return result; 249 } catch (Exception e) { 250 log.info("Unable to undeploy assembly", e); 251 throw e; 252 } 253 } 254 255 261 public String [] getDeployedServiceUnitList(String componentName) throws Exception { 262 try { 263 ServiceUnitLifeCycle[] sus = registry.getDeployedServiceUnits(componentName); 264 String [] names = new String [sus.length]; 265 for (int i = 0; i < names.length; i++) { 266 names[i] = sus[i].getName(); 267 } 268 return names; 269 } catch (Exception e) { 270 log.info("Unable to get deployed service unit list", e); 271 throw e; 272 } 273 } 274 275 280 public String [] getDeployedServiceAssemblies() throws Exception { 281 try { 282 return registry.getDeployedServiceAssemblies(); 283 } catch (Exception e) { 284 log.info("Unable to get deployed service assemblies", e); 285 throw e; 286 } 287 } 288 289 295 public String getServiceAssemblyDescriptor(String saName) throws Exception { 296 ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(saName); 297 if (sa != null) { 298 return sa.getDescriptor(); 299 } else { 300 return null; 301 } 302 } 303 304 311 public String [] getDeployedServiceAssembliesForComponent(String componentName) throws Exception { 312 try { 313 return registry.getDeployedServiceAssembliesForComponent(componentName); 314 } catch (Exception e) { 315 log.info("Error in getDeployedServiceAssembliesForComponent", e); 316 throw e; 317 } 318 } 319 320 327 public String [] getComponentsForDeployedServiceAssembly(String saName) throws Exception { 328 try { 329 return registry.getComponentsForDeployedServiceAssembly(saName); 330 } catch (Exception e) { 331 log.info("Error in getComponentsForDeployedServiceAssembly", e); 332 throw e; 333 } 334 } 335 336 344 public boolean isDeployedServiceUnit(String componentName, String suName) throws Exception { 345 try { 346 return registry.isSADeployedServiceUnit(componentName, suName); 347 } catch (Exception e) { 348 log.info("Error in isSADeployedServiceUnit", e); 349 throw e; 350 } 351 } 352 353 359 public boolean canDeployToComponent(String componentName) { 360 ComponentMBeanImpl lcc = container.getComponent(componentName); 361 return lcc != null && lcc.isStarted() && lcc.getServiceUnitManager() != null; 362 } 363 364 371 public String start(String serviceAssemblyName) throws Exception { 372 try { 373 ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(serviceAssemblyName); 374 return sa.start(true); 375 } catch (Exception e) { 376 log.info("Error in start", e); 377 throw e; 378 } 379 } 380 381 382 383 390 public String stop(String serviceAssemblyName) throws Exception { 391 try { 392 ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(serviceAssemblyName); 393 return sa.stop(true, false); 394 } catch (Exception e) { 395 log.info("Error in stop", e); 396 throw e; 397 } 398 } 399 400 407 public String shutDown(String serviceAssemblyName) throws Exception { 408 try { 409 ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(serviceAssemblyName); 410 return sa.shutDown(true); 411 } catch (Exception e) { 412 log.info("Error in shutDown", e); 413 throw e; 414 } 415 } 416 417 424 public String getState(String serviceAssemblyName) throws Exception { 425 try { 426 ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(serviceAssemblyName); 427 return sa.getCurrentState(); 428 } catch (Exception e) { 429 log.info("Error in getState", e); 430 throw e; 431 } 432 } 433 434 440 protected boolean isSaDeployed(String serviceAssemblyName) { 441 return registry.getServiceAssembly(serviceAssemblyName) != null; 442 } 443 444 452 protected String deployServiceAssembly(File tmpDir, ServiceAssembly sa) throws Exception { 453 String assemblyName = sa.getIdentification().getName(); 454 ServiceAssemblyEnvironment env = environmentContext.getNewServiceAssemblyEnvironment(assemblyName); 455 File saDirectory = env.getInstallDir(); 456 457 if (log.isDebugEnabled()) { 459 log.debug("Moving " + tmpDir.getAbsolutePath() + " to " + saDirectory.getAbsolutePath()); 460 } 461 saDirectory.getParentFile().mkdirs(); 462 if (!tmpDir.renameTo(saDirectory)) { 463 throw ManagementSupport.failure("deploy", "Failed to rename " + tmpDir + " to " + saDirectory); 464 } 465 ServiceUnit[] sus = sa.getServiceUnits(); 467 if (sus != null) { 468 for (int i = 0; i < sus.length; i++) { 469 String suName = sus[i].getIdentification().getName(); 470 String artifact = sus[i].getTarget().getArtifactsZip(); 471 String componentName = sus[i].getTarget().getComponentName(); 472 File artifactFile = new File (saDirectory, artifact); 473 if (!artifactFile.exists()) { 474 throw ManagementSupport.failure("deploy", "Artifact " + artifact + " not found for service unit " + suName); 475 } 476 ComponentMBeanImpl lcc = container.getComponent(componentName); 477 if (lcc == null) { 478 throw ManagementSupport.failure("deploy", "Target component " + componentName + " for service unit " + suName + " is not installed"); 479 } 480 if (!lcc.isStarted()) { 481 throw ManagementSupport.failure("deploy", "Target component " + componentName + " for service unit " + suName + " is not started"); 482 } 483 if (lcc.getServiceUnitManager() == null) { 484 throw ManagementSupport.failure("deploy", "Target component " + componentName + " for service unit " + suName + " does not accept deployments"); 485 } 486 if (isDeployedServiceUnit(componentName, suName)) { 488 throw ManagementSupport.failure("deploy", "Service unit " + suName + " is already deployed on component " + componentName); 489 } 490 } 491 } 492 int nbSuccess = 0; 494 int nbFailures = 0; 495 List componentResults = new ArrayList (); 496 List suKeys = new ArrayList (); 497 if (sus != null) { 498 for (int i = 0; i < sus.length; i++) { 499 File targetDir = null; 500 String suName = sus[i].getIdentification().getName(); 501 String artifact = sus[i].getTarget().getArtifactsZip(); 502 String componentName = sus[i].getTarget().getComponentName(); 503 try { 506 File artifactFile = new File (saDirectory, artifact); 507 targetDir = env.getServiceUnitDirectory(componentName, suName); 508 if (log.isDebugEnabled()) { 509 log.debug("Unpack service unit archive " + artifactFile + " to " + targetDir); 510 } 511 FileUtil.unpackArchive(artifactFile, targetDir); 512 } catch (IOException e) { 513 nbFailures++; 514 componentResults.add(ManagementSupport.createComponentFailure( 515 "deploy", componentName, 516 "Error unpacking service unit", e)); 517 continue; 518 } 519 boolean success = false; 521 try { 522 ComponentMBeanImpl lcc = container.getComponent(componentName); 523 ServiceUnitManager sum = lcc.getServiceUnitManager(); 524 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 525 try { 526 Thread.currentThread().setContextClassLoader(lcc.getComponent().getClass().getClassLoader()); 527 String resultMsg = sum.deploy(suName, targetDir.getAbsolutePath()); 528 success = getComponentTaskResult(resultMsg, componentName, componentResults, true); 529 } finally { 530 Thread.currentThread().setContextClassLoader(cl); 531 } 532 } catch (Exception e) { 534 getComponentTaskError(e, componentName, componentResults); 535 } 536 if (success) { 537 nbSuccess++; 538 suKeys.add(registry.registerServiceUnit(sus[i], assemblyName, targetDir)); 539 } else { 540 nbFailures++; 541 } 542 } 543 } 544 if (nbFailures > 0) { 551 for (Iterator iter = suKeys.iterator(); iter.hasNext();) { 553 try { 554 String suName = (String ) iter.next(); 555 ServiceUnitLifeCycle su = registry.getServiceUnit(suName); 556 undeployServiceUnit(su); 557 } catch (Exception e) { 558 log.warn("Error undeploying SU", e); 559 } 560 } 561 FileUtil.deleteFile(saDirectory); 563 throw ManagementSupport.failure("deploy", componentResults); 564 } 565 else { 567 String [] deployedSUs = (String []) suKeys.toArray(new String [suKeys.size()]); 569 ServiceAssemblyLifeCycle salc = registry.registerServiceAssembly(sa, deployedSUs, env); 570 salc.writeRunningState(); 571 if (nbFailures > 0) { 573 return ManagementSupport.createWarningMessage("deploy", "Failed to deploy some service units", componentResults); 574 } else { 575 return ManagementSupport.createSuccessMessage("deploy", componentResults); 576 } 577 } 578 } 579 580 protected void getComponentTaskError(Exception exception, String component, List results) { 581 Element result = null; 582 try { 583 Document doc = parse(exception.getMessage()); 584 result = getElement(doc, "component-task-result"); 585 } catch (Exception e) { 586 result = ManagementSupport.createComponentFailure( 587 "deploy", component, 588 "Unable to parse result string", exception); 589 } 590 if (result != null) { 591 results.add(result); 592 } 593 } 594 595 protected boolean getComponentTaskResult(String resultMsg, String component, List results, boolean success) { 596 Element result = null; 597 try { 598 Document doc = parse(resultMsg); 599 result = getElement(doc, "component-task-result"); 600 Element e = getChildElement(result, "component-task-result-details"); 601 e = getChildElement(e, "task-result-details"); 602 e = getChildElement(e, "task-result"); 603 String r = DOMUtil.getElementText(e); 604 if (!"SUCCESS".equals(r)) { 605 success = false; 606 } 607 } catch (Exception e) { 608 try { 612 if (success) { 613 result = ManagementSupport.createComponentWarning( 614 "deploy", component, 615 "Unable to parse result string", e); 616 } else { 617 result = ManagementSupport.createComponentFailure( 618 "deploy", component, 619 "Unable to parse result string", e); 620 } 621 } catch (Exception e2) { 622 log.error(e2); 623 result = null; 624 } 625 } 626 if (result != null) { 627 results.add(result); 628 } 629 return success; 630 } 631 632 protected Document parse(String result) throws ParserConfigurationException , SAXException , IOException { 633 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 634 factory.setNamespaceAware(true); 635 factory.setIgnoringElementContentWhitespace(true); 636 factory.setIgnoringComments(true); 637 DocumentBuilder builder = factory.newDocumentBuilder(); 638 return builder.parse(new InputSource (new StringReader (result))); 639 } 640 641 protected Element getElement(Document doc, String name) { 642 NodeList l = doc.getElementsByTagNameNS("http://java.sun.com/xml/ns/jbi/management-message", name); 643 Element e = (Element ) l.item(0); 644 return e; 645 } 646 647 protected Element getChildElement(Element element, String name) { 648 NodeList l = element.getElementsByTagNameNS("http://java.sun.com/xml/ns/jbi/management-message", name); 649 Element e = (Element ) l.item(0); 650 return e; 651 } 652 653 protected void undeployServiceUnit(ServiceUnitLifeCycle su) throws DeploymentException { 654 String name = su.getName(); 655 String componentName = su.getComponentName(); 656 File targetDir = su.getServiceUnitRootPath(); 657 registry.unregisterServiceUnit(su.getKey()); 658 ComponentMBeanImpl component = container.getComponent(componentName); 661 if (component != null) { 662 ServiceUnitManager sum = component.getServiceUnitManager(); 663 if (sum != null) { 664 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 665 try { 666 Thread.currentThread().setContextClassLoader(component.getComponent().getClass().getClassLoader()); 667 sum.undeploy(name, targetDir.getAbsolutePath()); 668 } finally { 669 Thread.currentThread().setContextClassLoader(cl); 670 } 671 FileUtil.deleteFile(targetDir); 672 } 673 } 674 else { 675 FileUtil.deleteFile(targetDir); 676 } 677 log.info("UnDeployed ServiceUnit " + name + " from Component: " + componentName); 678 } 679 680 683 protected void buildState() { 684 log.info("Restoring service assemblies"); 685 File top = environmentContext.getServiceAssembliesDir(); 687 if (top != null && top.exists() && top.isDirectory()) { 688 File [] files = top.listFiles(); 689 if (files != null) { 690 for (int i = 0; i < files.length; i++) { 692 if (files[i].isDirectory()) { 693 String assemblyName = files[i].getName(); 694 try { 695 ServiceAssemblyEnvironment env = environmentContext.getServiceAssemblyEnvironment(assemblyName); 696 Descriptor root = DescriptorFactory.buildDescriptor(env.getInstallDir()); 697 if (root != null) { 698 ServiceAssembly sa = root.getServiceAssembly(); 699 if (sa != null && sa.getIdentification() != null) { 700 registry.registerServiceAssembly(sa, env); 701 } 702 } 703 } catch(Exception e) { 704 log.error("Failed to initialized service assembly: " + assemblyName,e); 705 } 706 } 707 } 708 } 709 } 710 } 711 712 } 713 | Popular Tags |