1 22 package org.jboss.deployment.spi; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.net.URI ; 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 import java.util.HashMap ; 35 import java.util.HashSet ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Locale ; 39 import java.util.Set ; 40 import java.util.jar.JarEntry ; 41 import java.util.jar.JarInputStream ; 42 import java.util.jar.JarOutputStream ; 43 import java.util.jar.Manifest ; 44 45 import javax.enterprise.deploy.model.DeployableObject ; 46 import javax.enterprise.deploy.shared.ActionType ; 47 import javax.enterprise.deploy.shared.CommandType ; 48 import javax.enterprise.deploy.shared.DConfigBeanVersionType ; 49 import javax.enterprise.deploy.shared.ModuleType ; 50 import javax.enterprise.deploy.shared.StateType ; 51 import javax.enterprise.deploy.spi.DeploymentConfiguration ; 52 import javax.enterprise.deploy.spi.DeploymentManager ; 53 import javax.enterprise.deploy.spi.Target ; 54 import javax.enterprise.deploy.spi.TargetModuleID ; 55 import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException ; 56 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException ; 57 import javax.enterprise.deploy.spi.exceptions.TargetException ; 58 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 59 import javax.enterprise.deploy.spi.status.ProgressObject ; 60 61 import org.dom4j.Document; 62 import org.dom4j.io.SAXReader; 63 import org.jboss.deployment.spi.configurations.WarConfiguration; 64 import org.jboss.deployment.spi.status.DeploymentStatusImpl; 65 import org.jboss.deployment.spi.status.ProgressObjectImpl; 66 import org.jboss.logging.Logger; 67 import org.jboss.util.xml.JBossEntityResolver; 68 69 77 public class DeploymentManagerImpl implements DeploymentManager 78 { 79 private static final Logger log = Logger.getLogger(DeploymentManagerImpl.class); 81 82 83 public static final String DEPLOYER_URI = "http://org.jboss.deployment/jsr88"; 84 85 private Target [] targets; 87 88 private HashMap mapDeploymentPlan; 90 private DeploymentMetaData metaData; 91 92 private List tmpFiles = new ArrayList (); 93 private URI deployURI; 94 private boolean isConnected; 95 96 102 public DeploymentManagerImpl(URI deployURI, boolean isConnected) 103 { 104 this(deployURI, isConnected, null, null); 105 } 106 107 116 public DeploymentManagerImpl(URI deployURI, boolean isConnected, String username, String password) 117 { 118 this.deployURI = deployURI; 119 this.isConnected = isConnected; 120 this.targets = null; 121 } 122 123 132 public Target [] getTargets() 133 { 134 if (isConnected == false) 135 throw new IllegalStateException ("DeploymentManager is not connected"); 136 137 if (targets == null) 138 { 139 if (deployURI.isOpaque()) 140 { 141 log.debug("Opaque URI seen, defaulting to LocalhostTarget"); 142 targets = new Target [] { new LocalhostTarget() }; 143 } 144 else 145 { 146 log.debug("Non-Opaque URI seen, using to JMXTarget"); 147 targets = new Target [] { new JMXTarget(deployURI) }; 148 } 149 } 150 return targets; 151 } 152 153 163 public TargetModuleID [] getRunningModules(ModuleType moduleType, Target [] targets) throws TargetException 164 { 165 if (isConnected == false) 166 throw new IllegalStateException ("DeploymentManager is not connected"); 167 168 log.debug("getRunningModules [type=" + moduleType + ",targets=" + Arrays.asList(targets) + "]"); 169 170 Set moduleSet = new HashSet (); 172 TargetModuleID [] availableModules = getAvailableModules(moduleType, targets); 173 if (availableModules == null) 174 { 175 log.debug("No modules available"); 176 return null; 177 } 178 179 for (int i = 0; i < availableModules.length; i++) 180 { 181 TargetModuleIDImpl moduleID = (TargetModuleIDImpl)availableModules[i]; 182 if (moduleID.isRunning()) 183 { 184 moduleSet.add(moduleID); 185 } 186 } 187 log.debug("Found [" + moduleSet.size() + "] running modules"); 188 189 TargetModuleID [] idarr = new TargetModuleID [moduleSet.size()]; 191 moduleSet.toArray(idarr); 192 return idarr; 193 } 194 195 205 public TargetModuleID [] getNonRunningModules(ModuleType moduleType, Target [] targets) throws TargetException 206 { 207 if (isConnected == false) 208 throw new IllegalStateException ("DeploymentManager is not connected"); 209 210 log.debug("getNonRunningModules [type=" + moduleType + ",targets=" + Arrays.asList(targets) + "]"); 211 212 Set moduleSet = new HashSet (); 214 TargetModuleID [] availableModules = getAvailableModules(moduleType, targets); 215 if (availableModules == null) 216 { 217 log.debug("No modules available"); 218 return null; 219 } 220 221 for (int i = 0; i < availableModules.length; i++) 222 { 223 TargetModuleIDImpl moduleID = (TargetModuleIDImpl)availableModules[i]; 224 if (moduleID.isRunning() == false) 225 { 226 moduleSet.add(moduleID); 227 } 228 } 229 log.debug("Found [" + moduleSet.size() + "] non running modules"); 230 231 TargetModuleID [] idarr = new TargetModuleID [moduleSet.size()]; 233 moduleSet.toArray(idarr); 234 return idarr; 235 } 236 237 247 public TargetModuleID [] getAvailableModules(ModuleType moduleType, Target [] targets) throws TargetException 248 { 249 if (isConnected == false) 250 throw new IllegalStateException ("DeploymentManager is not connected"); 251 252 log.debug("getAvailableModules [type=" + moduleType + ",targets=" + Arrays.asList(targets) + "]"); 253 254 List targetModules = new ArrayList (); 256 for (int i = 0; i < targets.length; i++) 257 { 258 JBossTarget target = (JBossTarget)targets[i]; 259 TargetModuleID [] tmids = target.getAvailableModules(moduleType); 260 targetModules.addAll(Arrays.asList(tmids)); 261 } 262 log.debug("Found [" + targetModules.size() + "] available modules"); 263 264 if (targetModules.size() > 0) 266 { 267 TargetModuleID [] idarr = new TargetModuleID [targetModules.size()]; 268 targetModules.toArray(idarr); 269 return idarr; 270 } 271 272 return null; 274 } 275 276 284 public DeploymentConfiguration createConfiguration(DeployableObject obj) throws InvalidModuleException 285 { 286 if (obj.getType().equals(ModuleType.WAR)) 288 return new WarConfiguration(obj); 289 290 throw new InvalidModuleException ("CreateConfiguration: Module type not yet supported"); 291 } 292 293 303 public ProgressObject distribute(Target [] targets, File moduleArchive, File deploymentPlan) 304 { 305 if (isConnected == false) 306 throw new IllegalStateException ("DeploymentManager is not connected"); 307 308 InputStream isModuleArchive = null; 309 InputStream isDeploymentPlan = null; 310 try 311 { 312 isModuleArchive = new FileInputStream (moduleArchive); 313 isDeploymentPlan = new FileInputStream (deploymentPlan); 314 return distribute(targets, isModuleArchive, isDeploymentPlan); 315 } 316 catch (FileNotFoundException e) 317 { 318 String message = "Cannot find deployment file" + e.getMessage(); 319 log.error(message, e); 320 DeploymentStatus status = new DeploymentStatusImpl(StateType.FAILED, CommandType.DISTRIBUTE, ActionType.EXECUTE, message); 321 return new ProgressObjectImpl(status, null); 322 } 323 } 324 325 335 public ProgressObject distribute(Target [] targets, InputStream moduleArchive, InputStream deploymentPlan) 336 { 337 if (isConnected == false) 338 throw new IllegalStateException ("DeploymentManager is not connected"); 339 340 TargetModuleID [] targetModuleIDs = new TargetModuleID [targets.length]; 341 try 342 { 343 mapDeploymentPlan = unpackDeploymentPlan(deploymentPlan); 345 initDeploymentMetaData(); 346 347 TargetModuleInfo moduleInfo = createDeployment(moduleArchive, metaData.getDeploymentName()); 349 URL deployment = moduleInfo.getModuleID(); 350 351 for (int i = 0; i < targets.length; i++) 353 { 354 JBossTarget target = (JBossTarget)targets[i]; 355 String moduleID = deployment.toExternalForm(); 356 targetModuleIDs[i] = new TargetModuleIDImpl(target, moduleID, null, false, moduleInfo.getModuleType()); 357 } 358 359 for (int i = 0; i < tmpFiles.size(); i++) 361 { 362 File file = (File )tmpFiles.get(i); 363 if (file.equals(deployment) == false) 364 file.delete(); 365 } 366 } 367 catch (IOException e) 368 { 369 String message = "Exception during deployment validation"; 370 log.error(message, e); 371 DeploymentStatus status = new DeploymentStatusImpl(StateType.FAILED, CommandType.DISTRIBUTE, ActionType.EXECUTE, message); 372 return new ProgressObjectImpl(status, targetModuleIDs); 373 } 374 375 DeploymentStatus status = new DeploymentStatusImpl(StateType.RUNNING, CommandType.DISTRIBUTE, ActionType.EXECUTE, null); 377 ProgressObject progress = new ProgressObjectImpl(status, targetModuleIDs); 378 379 DeploymentWorker worker = new DeploymentWorker(progress); 380 worker.start(); 381 382 return progress; 383 } 384 385 388 private void initDeploymentMetaData() throws IOException 389 { 390 File metaTmpFile = (File )mapDeploymentPlan.get(DeploymentMetaData.ENTRY_NAME); 391 if (metaTmpFile == null) 392 throw new IOException ("Deployment plan does not contain an entry: " + DeploymentMetaData.ENTRY_NAME); 393 394 try 395 { 396 SAXReader reader = new SAXReader(); 397 reader.setEntityResolver(new JBossEntityResolver()); 398 399 Document metaDoc = reader.read(metaTmpFile); 400 metaData = new DeploymentMetaData(metaDoc); 401 log.debug(DeploymentMetaData.ENTRY_NAME + "\n" + metaData.toXMLString()); 402 } 403 catch (Exception e) 404 { 405 log.error("Cannot obtain meta data: " + e); 406 } 407 } 408 409 413 private TargetModuleInfo createDeployment(InputStream moduleArchive, String moduleName) throws IOException 414 { 415 File tmpFile = File.createTempFile("jboss_deployment_", ".zip"); 416 log.debug("temporary deployment file: " + tmpFile); 417 418 JarInputStream jis = new JarInputStream (moduleArchive); 419 420 JarOutputStream jos = null; 422 FileOutputStream fos = new FileOutputStream (tmpFile); 423 Manifest manifest = jis.getManifest(); 424 if (manifest != null) 425 jos = new JarOutputStream (fos, manifest); 426 else jos = new JarOutputStream (fos); 427 428 TargetModuleInfo moduleInfo = new TargetModuleInfo(); 430 ModuleType moduleType = null; 431 JarEntry entry = jis.getNextJarEntry(); 432 while (entry != null) 433 { 434 String entryName = entry.getName(); 435 436 if (entryName.endsWith("/") == false) 438 { 439 if (entryName.endsWith("/application.xml")) 440 { 441 moduleType = ModuleType.EAR; 442 } 443 else if (entryName.endsWith("/application-client.xml")) 444 { 445 moduleType = ModuleType.CAR; 446 } 447 else if (entryName.endsWith("/ra.xml")) 448 { 449 moduleType = ModuleType.RAR; 450 } 451 else if (entryName.endsWith("/web.xml")) 452 { 453 moduleType = ModuleType.WAR; 454 } 455 else if (entryName.endsWith("/ejb-jar.xml")) 456 { 457 moduleType = ModuleType.EJB; 458 } 459 460 if (entryName.endsWith(".jar") || entryName.endsWith(".war")) 462 { 463 File tmpSubModule = processSubModule(entryName, jis); 464 FileInputStream fis = new FileInputStream (tmpSubModule); 465 JarUtils.addJarEntry(jos, entryName, fis); 466 fis.close(); 467 } 468 else 469 { 470 if (mapDeploymentPlan.get("!/" + entryName) == null) 471 JarUtils.addJarEntry(jos, entryName, jis); 472 else log.debug("Skip entry found in deployment plan: " + entryName); 473 } 474 } 475 476 entry = jis.getNextJarEntry(); 477 } 478 479 if (moduleType == null) 480 { 481 if (moduleName.endsWith(ModuleType.EAR.getModuleExtension())) 482 moduleType = ModuleType.EAR; 483 else 484 throw new RuntimeException ("cannot obtain module type"); 485 } 486 487 moduleInfo.setModuleType(moduleType); 488 addDeploymentPlanEntry(jos, null); 490 jos.close(); 491 492 String deploymentName = tmpFile.getParent() + File.separator + metaData.getDeploymentName(); 494 File deployment = new File (deploymentName); 495 if (deployment.exists() && deployment.delete() == false) 496 throw new IOException ("Cannot delete existing deployment: " + deployment); 497 498 tmpFile.renameTo(deployment); 499 moduleInfo.setModuleID(deployment.toURL()); 500 return moduleInfo; 501 } 502 503 public ProgressObject redeploy(TargetModuleID [] targetModuleIDs, File file, File file1) throws UnsupportedOperationException , IllegalStateException 504 { 505 if (isConnected == false) 506 throw new IllegalStateException ("DeploymentManager is not connected"); 507 508 throw new UnsupportedOperationException ("redeploy"); 509 } 510 511 public ProgressObject redeploy(TargetModuleID [] targetModuleIDs, InputStream inputStream, InputStream inputStream1) throws UnsupportedOperationException , 512 IllegalStateException 513 { 514 if (isConnected == false) 515 throw new IllegalStateException ("DeploymentManager is not connected"); 516 517 throw new UnsupportedOperationException ("redeploy"); 518 } 519 520 527 public ProgressObject start(TargetModuleID [] targetModuleIDs) 528 { 529 if (isConnected == false) 530 throw new IllegalStateException ("DeploymentManager is not connected"); 531 532 log.debug("start " + Arrays.asList(targetModuleIDs)); 533 534 DeploymentStatus status = new DeploymentStatusImpl(StateType.RUNNING, CommandType.START, ActionType.EXECUTE, null); 536 ProgressObject progress = new ProgressObjectImpl(status, targetModuleIDs); 537 538 DeploymentWorker worker = new DeploymentWorker(progress); 539 worker.start(); 540 541 return progress; 542 } 543 544 551 public ProgressObject stop(TargetModuleID [] targetModuleIDs) 552 { 553 if (isConnected == false) 554 throw new IllegalStateException ("DeploymentManager is not connected"); 555 556 log.debug("stop " + Arrays.asList(targetModuleIDs)); 557 558 DeploymentStatus status = new DeploymentStatusImpl(StateType.RUNNING, CommandType.STOP, ActionType.EXECUTE, null); 559 ProgressObject progress = new ProgressObjectImpl(status, targetModuleIDs); 560 561 DeploymentWorker worker = new DeploymentWorker(progress); 562 worker.start(); 563 564 return progress; 565 } 566 567 574 public ProgressObject undeploy(TargetModuleID [] targetModuleIDs) 575 { 576 if (isConnected == false) 577 throw new IllegalStateException ("DeploymentManager is not connected"); 578 579 log.debug("undeploy " + Arrays.asList(targetModuleIDs)); 580 581 DeploymentStatus status = new DeploymentStatusImpl(StateType.RUNNING, CommandType.UNDEPLOY, ActionType.EXECUTE, null); 583 ProgressObject progress = new ProgressObjectImpl(status, targetModuleIDs); 584 585 DeploymentWorker worker = new DeploymentWorker(progress); 586 worker.start(); 587 588 return progress; 589 } 590 591 596 public boolean isRedeploySupported() 597 { 598 return false; 599 } 600 601 609 public ProgressObject redeploy(TargetModuleID [] moduleIDList) 610 { 611 if (isConnected == false) 612 throw new IllegalStateException ("DeploymentManager is not connected"); 613 614 throw new UnsupportedOperationException ("redeploy"); 615 } 616 617 627 public void release() 628 { 629 isConnected = false; 630 } 631 632 637 public Locale getDefaultLocale() 638 { 639 return Locale.getDefault(); 640 } 641 642 645 public Locale getCurrentLocale() 646 { 647 return Locale.getDefault(); 648 } 649 650 653 public void setLocale(Locale locale) 654 { 655 throw new UnsupportedOperationException ("setLocale"); 656 } 657 658 661 public boolean isLocaleSupported(Locale locale) 662 { 663 return Locale.getDefault().equals(locale); 664 } 665 666 671 public Locale [] getSupportedLocales() 672 { 673 return new Locale [] { Locale.getDefault() }; 674 } 675 676 681 public DConfigBeanVersionType getDConfigBeanVersion() 682 { 683 return DConfigBeanVersionType.V1_4; 684 } 685 686 public void setDConfigBeanVersion(DConfigBeanVersionType dConfigBeanVersionType) throws DConfigBeanVersionUnsupportedException 687 { 688 throw new UnsupportedOperationException ("setDConfigBeanVersion"); 689 } 690 691 public boolean isDConfigBeanVersionSupported(DConfigBeanVersionType dConfigBeanVersionType) 692 { 693 return dConfigBeanVersionType == DConfigBeanVersionType.V1_4; 694 } 695 696 702 public boolean isDConfigBeanVersionTypeSupported(DConfigBeanVersionType version) 703 { 704 return version == DConfigBeanVersionType.V1_4; 705 } 706 707 713 public void setDConfigBeanVersionType(DConfigBeanVersionType version) 714 { 715 throw new UnsupportedOperationException ("setDConfigBeanVersionType"); 716 } 717 718 721 private File processSubModule(String entryName, JarInputStream jis) throws IOException 722 { 723 File tmpModule = getTempFile(entryName); 725 tmpFiles.add(tmpModule); 726 FileOutputStream fos = new FileOutputStream (tmpModule); 727 JarUtils.copyStream(fos, jis); 728 fos.close(); 729 730 JarInputStream jisModule = new JarInputStream (new FileInputStream (tmpModule)); 732 File tmpJBossModule = getTempFile("jboss_" + entryName); 733 tmpFiles.add(tmpJBossModule); 734 JarOutputStream jos = null; 735 fos = new FileOutputStream (tmpJBossModule); 736 Manifest manifest = jisModule.getManifest(); 737 if (manifest != null) 738 jos = new JarOutputStream (fos, manifest); 739 else jos = new JarOutputStream (fos); 740 741 JarEntry entry = jisModule.getNextJarEntry(); 743 while (entry != null) 744 { 745 String subEntryName = entry.getName(); 746 if (mapDeploymentPlan.get(entryName + "!/" + subEntryName) == null) 747 JarUtils.addJarEntry(jos, subEntryName, jisModule); 748 else log.debug("Skip entry found in deployment plan: " + subEntryName); 749 750 entry = jisModule.getNextJarEntry(); 751 } 752 jisModule.close(); 753 754 addDeploymentPlanEntry(jos, entryName); 755 756 jos.close(); 757 758 return tmpJBossModule; 759 } 760 761 765 private void addDeploymentPlanEntry(JarOutputStream jos, String moduleName) throws IOException 766 { 767 if (moduleName == null) 768 moduleName = ""; 769 770 String moduleKey = moduleName + "!/"; 772 773 Iterator it = mapDeploymentPlan.keySet().iterator(); 774 while (it.hasNext()) 775 { 776 String key = (String )it.next(); 777 if (key.startsWith(moduleKey)) 778 { 779 String dpName = key.substring(moduleKey.length()); 780 log.debug("found deployment plan entry: " + dpName); 781 782 File dpFile = (File )mapDeploymentPlan.get(key); 783 FileInputStream dpin = new FileInputStream (dpFile); 784 JarUtils.addJarEntry(jos, dpName, dpin); 785 dpin.close(); 786 } 787 } 788 } 789 790 793 private HashMap unpackDeploymentPlan(InputStream deploymentPlan) throws IOException 794 { 795 HashMap dpMap = new HashMap (); 796 797 if (deploymentPlan == null) 798 return dpMap; 799 800 try 802 { 803 JarInputStream jarDeploymentPlan = new JarInputStream (deploymentPlan); 804 JarEntry entry = jarDeploymentPlan.getNextJarEntry(); 805 while (entry != null) 806 { 807 String entryName = entry.getName(); 808 log.debug("unpack deployment plan entry: " + entryName); 809 810 File tempFile = getTempFile(entryName); 811 dpMap.put(entryName, tempFile); 812 813 FileOutputStream out = new FileOutputStream (tempFile); 814 JarUtils.copyStream(out, jarDeploymentPlan); 815 out.close(); 816 817 entry = jarDeploymentPlan.getNextJarEntry(); 818 } 819 } 820 finally 821 { 822 deploymentPlan.close(); 823 } 824 825 return dpMap; 826 } 827 828 831 private File getTempFile(String entryName) throws IOException 832 { 833 entryName = entryName.replace('/', '_'); 834 int index = entryName.lastIndexOf("."); 835 String prefix = entryName.substring(0, index); 836 String suffix = entryName.substring(index); 837 838 File tempFile = File.createTempFile(prefix, suffix); 839 tmpFiles.add(tempFile); 840 return tempFile; 841 } 842 843 static private class TargetModuleInfo 844 { 845 URL moduleID = null; 846 ModuleType moduleType = null; 847 848 public URL getModuleID() 849 { 850 return moduleID; 851 } 852 853 public void setModuleID(URL url) 854 { 855 moduleID = url; 856 } 857 858 public ModuleType getModuleType() 859 { 860 return moduleType; 861 } 862 863 public void setModuleType(ModuleType type) 864 { 865 moduleType = type; 866 } 867 } 868 } 869 | Popular Tags |