1 22 package org.jboss.deployment; 23 24 import java.io.BufferedInputStream ; 25 import java.io.BufferedOutputStream ; 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.net.MalformedURLException ; 32 import java.net.URL ; 33 import java.net.URLClassLoader ; 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import java.util.Collections ; 37 import java.util.Comparator ; 38 import java.util.HashMap ; 39 import java.util.HashSet ; 40 import java.util.Iterator ; 41 import java.util.LinkedList ; 42 import java.util.List ; 43 import java.util.ListIterator ; 44 import java.util.Map ; 45 import java.util.Set ; 46 import java.util.StringTokenizer ; 47 import java.util.jar.Attributes ; 48 import java.util.jar.Manifest ; 49 50 import javax.management.JMException ; 51 import javax.management.MBeanServer ; 52 import javax.management.MalformedObjectNameException ; 53 import javax.management.Notification ; 54 import javax.management.ObjectName ; 55 56 import org.jboss.deployers.plugins.structure.AbstractDeploymentContext; 57 import org.jboss.deployers.spi.structure.DeploymentContext; 58 import org.jboss.mx.util.JMXExceptionDecoder; 59 import org.jboss.system.ServiceContext; 60 import org.jboss.system.ServiceMBeanSupport; 61 import org.jboss.system.server.ServerConfig; 62 import org.jboss.system.server.ServerConfigLocator; 63 import org.jboss.util.file.Files; 64 import org.jboss.util.file.JarUtils; 65 import org.jboss.util.stream.Streams; 66 import org.jboss.virtual.VFS; 67 import org.jboss.virtual.VirtualFile; 68 69 81 public class MainDeployer extends ServiceMBeanSupport 82 implements Deployer, MainDeployerMBean 83 { 84 private org.jboss.deployers.spi.deployment.MainDeployer delegate; 85 private Map <URL , String > contextMap = Collections.synchronizedMap(new HashMap <URL , String >()); 86 87 92 private ObjectName serviceController; 93 94 95 private final LinkedList deployers = new LinkedList (); 96 97 98 private final Map deploymentMap = Collections.synchronizedMap(new HashMap ()); 99 100 101 private final List deploymentList = new ArrayList (); 102 103 104 private final List waitingDeployments = new ArrayList (); 105 106 107 private final DeploymentSorter sorter = new DeploymentSorter(); 108 109 110 private final Comparator infoSorter = new DeploymentInfoComparator(sorter); 111 112 113 private final SuffixOrderHelper suffixOrderHelper = new SuffixOrderHelper(sorter); 114 115 116 private boolean copyFiles = true; 117 118 119 private File tempDir; 120 121 122 private String tempDirString; 123 124 127 public MainDeployer() 128 { 129 String localCopy = System.getProperty("jboss.deploy.localcopy"); 131 if (localCopy != null && ( 132 localCopy.equalsIgnoreCase("false") || 133 localCopy.equalsIgnoreCase("no") || 134 localCopy.equalsIgnoreCase("off"))) 135 { 136 log.debug("Disabling local copies of file: urls"); 137 copyFiles = false; 138 } 139 } 140 141 public org.jboss.deployers.spi.deployment.MainDeployer getKernelMainDeployer() 142 { 143 return delegate; 144 } 145 public void setKernelMainDeployer(org.jboss.deployers.spi.deployment.MainDeployer delegate) 146 { 147 this.delegate = delegate; 148 } 149 150 155 public boolean getCopyFiles() 156 { 157 return copyFiles; 158 } 159 166 public void setCopyFiles(boolean copyFiles) 167 { 168 this.copyFiles = copyFiles; 169 } 170 171 176 public File getTempDir() 177 { 178 return tempDir; 179 } 180 185 public void setTempDir(File tempDir) 186 { 187 this.tempDir = tempDir; 188 } 189 190 195 public String getTempDirString() 196 { 197 return tempDirString; 198 } 199 200 205 public String [] getSuffixOrder() 206 { 207 return suffixOrderHelper.getSuffixOrder(); 208 } 209 210 215 public String [] getEnhancedSuffixOrder() 216 { 217 return suffixOrderHelper.getEnhancedSuffixes(); 218 } 219 220 225 public void setEnhancedSuffixOrder(String [] enhancedSuffixOrder) 226 { 227 suffixOrderHelper.setEnhancedSuffixes(enhancedSuffixOrder); 228 } 229 230 236 public void setServiceController(final ObjectName serviceController) 237 { 238 this.serviceController = serviceController; 239 } 240 241 248 public Collection listDeployed() 249 { 250 synchronized (deploymentList) 251 { 252 log.debug("deployment list string: " + deploymentList); 253 return new ArrayList (deploymentList); 254 } 255 } 256 257 264 public Collection listDeployedModules() 265 { 266 log.debug("listDeployedModules"); 267 268 HashMap map = new HashMap (); 269 synchronized (deploymentList) 270 { 271 Collection col = new ArrayList (deploymentList); 272 273 for (Iterator it = col.iterator(); it.hasNext();) 275 { 276 DeploymentInfo info = (DeploymentInfo) it.next(); 277 map.put(info.url, new SerializableDeploymentInfo(info)); 278 fillParentAndChildrenSDI(info, map); 280 } 281 } 282 283 return new ArrayList (map.values()); 285 } 286 287 293 public String listDeployedAsString() 294 { 295 return "<pre>" + listDeployed() + "</pre>"; 296 } 297 298 306 public Collection listIncompletelyDeployed() 307 { 308 List id = new ArrayList (); 309 List copy; 310 synchronized (deploymentList) 311 { 312 copy = new ArrayList (deploymentList); 313 } 314 for (Iterator i = copy.iterator(); i.hasNext();) 315 { 316 DeploymentInfo di = (DeploymentInfo)i.next(); 317 if (!"Deployed".equals(di.status) && !"Starting".equals(di.status)) 318 { 319 id.add(di); 320 } 322 } return id; 324 } 325 326 333 public Collection listWaitingForDeployer() 334 { 335 synchronized (waitingDeployments) 336 { 337 return new ArrayList (waitingDeployments); 338 } 339 } 340 341 348 public void addDeployer(final SubDeployer deployer) 349 { 350 log.debug("Adding deployer: " + deployer); 351 ObjectName deployerName = deployer.getServiceName(); 352 353 synchronized(deployers) 354 { 355 deployers.addFirst(deployer); 356 try 357 { 358 String [] suffixes = (String []) server.getAttribute(deployerName, "EnhancedSuffixes"); 359 suffixOrderHelper.addEnhancedSuffixes(suffixes); 360 } 361 catch(Exception e) 362 { 363 log.debug(deployerName + " does not support EnhancedSuffixes"); 364 suffixOrderHelper.addSuffixes(deployer.getSuffixes(), deployer.getRelativeOrder()); 365 } 366 } 367 368 Notification msg = new Notification (ADD_DEPLOYER, this, getNextNotificationSequenceNumber()); 370 msg.setUserData(deployerName); 371 sendNotification(msg); 372 373 synchronized (waitingDeployments) 374 { 375 List copy = new ArrayList (waitingDeployments); 376 waitingDeployments.clear(); 377 for (Iterator i = copy.iterator(); i.hasNext();) 378 { 379 DeploymentInfo di = (DeploymentInfo)i.next(); 380 log.debug("trying to deploy with new deployer: " + di.shortName); 381 try 382 { 383 di.setServer(server); 384 deploy(di); 385 } 386 catch (DeploymentException e) 387 { 388 log.error("DeploymentException while trying to deploy a package with a new deployer", e); 389 } } } 392 } 393 394 401 public void removeDeployer(final SubDeployer deployer) 402 { 403 log.debug("Removing deployer: " + deployer); 404 ObjectName deployerName = deployer.getServiceName(); 405 boolean removed = false; 406 407 synchronized(deployers) 408 { 409 removed = deployers.remove(deployer); 410 try 411 { 412 String [] suffixes = (String []) server.getAttribute(deployerName, "EnhancedSuffixes"); 413 suffixOrderHelper.removeEnhancedSuffixes(suffixes); 414 } 415 catch(Exception e) 416 { 417 log.debug(deployerName + " does not support EnhancedSuffixes"); 418 suffixOrderHelper.removeSuffixes(deployer.getSuffixes(), deployer.getRelativeOrder()); 419 } 420 } 421 422 if (removed) 424 { 425 Notification msg = new Notification (REMOVE_DEPLOYER, this, getNextNotificationSequenceNumber()); 426 msg.setUserData(deployerName); 427 sendNotification(msg); 428 } 429 430 List copy; 431 synchronized (deploymentList) 432 { 433 copy = new ArrayList (deploymentList); 434 } 435 for (Iterator i = copy.iterator(); i.hasNext(); ) 436 { 437 DeploymentInfo di = (DeploymentInfo)i.next(); 438 if (di.deployer == deployer) 439 { 440 undeploy(di); 441 di.deployer = null; 442 synchronized (waitingDeployments) 443 { 444 waitingDeployments.add(di); 445 } 446 } 447 } 448 } 449 450 457 public Collection listDeployers() 458 { 459 ArrayList deployerNames = new ArrayList (); 460 synchronized(deployers) 461 { 462 for(int n = 0; n < deployers.size(); n ++) 463 { 464 SubDeployer deployer = (SubDeployer) deployers.get(n); 465 ObjectName name = deployer.getServiceName(); 466 deployerNames.add(name); 467 } 468 } 469 return deployerNames; 470 } 471 472 474 protected ObjectName getObjectName(MBeanServer server, ObjectName name) 475 throws MalformedObjectNameException 476 { 477 return name == null ? OBJECT_NAME : name; 478 } 479 480 485 protected void createService() throws Exception 486 { 487 ServerConfig config = ServerConfigLocator.locate(); 488 File basedir = config.getServerTempDir(); 490 tempDir = new File (basedir, "deploy"); 492 Files.delete(tempDir); 494 tempDir.mkdirs(); 496 497 tempDirString = tempDir.toURL().toString(); 499 500 suffixOrderHelper.initialize(); 502 } 503 504 510 public void shutdown() 511 { 512 int deployCounter = 0; 515 516 List copy; 518 synchronized (deploymentList) 519 { 520 copy = new ArrayList (deploymentList); 521 } 522 for (ListIterator i = copy.listIterator(copy.size()); i.hasPrevious(); ) 523 { 524 try 525 { 526 undeploy((DeploymentInfo)i.previous(), true); 527 deployCounter++; 528 } 529 catch (Exception e) 530 { 531 log.info("exception trying to undeploy during shutdown", e); 532 } 533 534 } 535 this.deployers.clear(); 537 this.deploymentMap.clear(); 538 this.deploymentList.clear(); 539 this.waitingDeployments.clear(); 540 this.tempDir = null; 541 542 log.debug("Undeployed " + deployCounter + " deployed packages"); 543 } 544 545 546 554 public void redeploy(String urlspec) 555 throws DeploymentException, MalformedURLException 556 { 557 redeploy(new URL (urlspec)); 558 } 559 560 567 public void redeploy(URL url) throws DeploymentException 568 { 569 undeploy(url); 570 deploy(url); 571 } 572 573 580 public void redeploy(DeploymentInfo sdi) throws DeploymentException 581 { 582 try 583 { 584 undeploy(sdi); 585 } 586 catch (Throwable t) 587 { 588 log.info("Throwable from undeployment attempt: ", t); 589 } sdi.setServer(server); 591 deploy(sdi); 592 } 593 594 601 public void undeploy(String urlspec) 602 throws DeploymentException, MalformedURLException 603 { 604 undeploy(new URL (urlspec)); 605 } 606 607 613 public void undeploy(URL url) throws DeploymentException 614 { 615 String deploymentName = contextMap.remove(url); 616 if (deploymentName != null) 617 { 618 try 619 { 620 delegate.removeDeploymentContext(deploymentName); 621 delegate.process(); 622 } 623 catch(Exception e) 624 { 625 DeploymentException ex = new DeploymentException("Error during undeploy of: "+url, e); 626 throw ex; 627 } 628 } 629 else 630 { 631 log.warn("undeploy '" + url + "' : package not deployed"); 632 } 633 } 634 635 642 public void undeploy(DeploymentInfo di) 643 { 644 undeploy(di, false); 645 } 646 protected void undeploy(DeploymentInfo di, boolean isShutdown) 647 { 648 log.debug("Undeploying "+di.url); 649 stop(di); 650 destroy(di); 651 } 652 653 658 private void stop(DeploymentInfo di) 659 { 660 ArrayList reverseSortedSubs = new ArrayList (di.subDeployments); 662 Collections.sort(reverseSortedSubs, infoSorter); 663 Collections.reverse(reverseSortedSubs); 664 for (Iterator subs = reverseSortedSubs.iterator(); subs.hasNext();) 665 { 666 DeploymentInfo sub = (DeploymentInfo) subs.next(); 667 log.debug("Stopping sub deployment: "+sub.url); 668 stop(sub); 669 } 670 try 672 { 673 if (di.deployer != null) 675 { 676 di.deployer.stop(di); 677 di.status="Stopped"; 678 di.state = DeploymentState.STOPPED; 679 } 680 } 681 catch (Throwable t) 682 { 683 log.error("Deployer stop failed for: " + di.url, t); 684 } 685 686 } 687 688 693 private void destroy(DeploymentInfo di) 694 { 695 ArrayList reverseSortedSubs = new ArrayList (di.subDeployments); 697 Collections.sort(reverseSortedSubs, infoSorter); 698 Collections.reverse(reverseSortedSubs); 699 for (Iterator subs = reverseSortedSubs.iterator(); subs.hasNext();) 700 { 701 DeploymentInfo sub = (DeploymentInfo) subs.next(); 702 log.debug("Destroying sub deployment: "+sub.url); 703 destroy(sub); 704 } 705 try 707 { 708 if (di.deployer != null) 710 { 711 di.deployer.destroy(di); 712 di.status="Destroyed"; 713 di.state = DeploymentState.DESTROYED; 714 } 715 } 716 catch (Throwable t) 717 { 718 log.error("Deployer destroy failed for: " + di.url, t); 719 di.state = DeploymentState.FAILED; 720 } 721 722 try 723 { 724 synchronized (deploymentList) 726 { 727 deploymentMap.remove(di.url); 728 if (deploymentList.lastIndexOf(di) != -1) 729 { 730 deploymentList.remove(deploymentList.lastIndexOf(di)); 731 } 732 } 733 synchronized (waitingDeployments) 734 { 735 waitingDeployments.remove(di); 736 } 737 di.cleanup(); 739 740 log.debug("Undeployed "+di.url); 741 } 742 catch (Throwable t) 743 { 744 log.error("Undeployment cleanup failed: " + di.url, t); 745 } 746 } 747 748 756 public void deploy(String urlspec) 757 throws DeploymentException, MalformedURLException 758 { 759 if( server == null ) 760 throw new DeploymentException("The MainDeployer has been unregistered"); 761 762 URL url; 763 try 764 { 765 url = new URL (urlspec); 766 } 767 catch (MalformedURLException e) 768 { 769 File file = new File (urlspec); 770 url = file.toURL(); 771 } 772 773 deploy(url); 774 } 775 776 782 public void deploy(URL url) throws DeploymentException 783 { 784 log.info("deploy, url="+url); 785 String deploymentName = contextMap.get(url); 786 if (deploymentName == null) 788 { 789 try 790 { 791 VirtualFile file = VFS.getRoot(url); 792 DeploymentContext deployment = new AbstractDeploymentContext(file); 793 delegate.addDeploymentContext(deployment); 794 deploymentName = deployment.getName(); 795 delegate.process(); 796 contextMap.put(url, deploymentName); 797 } 798 catch(Exception e) 799 { 800 log.warn("Failed to deploy: "+url, e); 801 DeploymentException ex = new DeploymentException("Failed to deploy: "+url, e); 802 throw ex; 803 } 804 } 805 } 806 807 814 public void deploy(DeploymentInfo deployment) 815 throws DeploymentException 816 { 817 if (isDeployed(deployment.url)) 819 { 820 log.info("Package: " + deployment.url + " is already deployed"); 821 return; 822 } 823 log.debug("Starting deployment of package: " + deployment.url); 824 825 boolean inited = false; 826 try 827 { 828 inited = init(deployment); 829 } 830 catch (Throwable t) 831 { 832 log.error("Could not initialise deployment: " + deployment.url, t); 833 DeploymentException.rethrowAsDeploymentException("Could not initialise deployment: " + deployment.url, t); 834 } 835 if ( inited ) 836 { 837 create(deployment); 838 start(deployment); 839 log.debug("Deployed package: " + deployment.url); 840 } else 842 { 843 log.debug("Deployment of package: " + deployment.url + " is waiting for an appropriate deployer."); 844 } } 846 847 848 856 private boolean init(DeploymentInfo deployment) throws DeploymentException 857 { 858 if (isDeployed(deployment.url)) 860 { 861 log.info("Package: " + deployment.url + " is already deployed"); 862 return false; 863 } 864 log.debug("Starting deployment (init step) of package at: " + deployment.url); 865 try 866 { 867 if (deployment.localUrl == null) 869 { 870 makeLocalCopy(deployment); 871 URL [] localCl = new URL []{deployment.localUrl}; 872 deployment.localCl = new URLClassLoader (localCl); 873 } 874 875 findDeployer(deployment); 877 878 if(deployment.deployer == null) 879 { 880 deployment.state = DeploymentState.INIT_WAITING_DEPLOYER; 881 log.debug("deployment waiting for deployer: " + deployment.url); 882 synchronized (waitingDeployments) 883 { 884 if (waitingDeployments.contains(deployment) == false) 885 waitingDeployments.add(deployment); 886 } 887 return false; 888 } 889 deployment.state = DeploymentState.INIT_DEPLOYER; 890 deployment.deployer.init(deployment); 892 deployment.createClassLoaders(); 894 deployment.state = DeploymentState.INITIALIZED; 895 896 synchronized (deploymentList) 898 { 899 deploymentMap.put(deployment.url, deployment); 900 } 901 902 parseManifestLibraries(deployment); 904 905 log.debug("found " + deployment.subDeployments.size() + " subpackages of " + deployment.url); 906 ArrayList sortedSubs = new ArrayList (deployment.subDeployments); 908 Collections.sort(sortedSubs, infoSorter); 909 for (Iterator lt = sortedSubs.listIterator(); lt.hasNext();) 910 { 911 init((DeploymentInfo) lt.next()); 912 } 913 } 914 catch (Exception e) 915 { 916 deployment.state = DeploymentState.FAILED; 917 DeploymentException.rethrowAsDeploymentException("exception in init of " + deployment.url, e); 918 } 919 finally 920 { 921 try 923 { 924 URL url = deployment.localUrl == null ? deployment.url : deployment.localUrl; 925 926 long lastModified = -1; 927 928 if (url.getProtocol().equals("file")) 929 lastModified = new File (url.getFile()).lastModified(); 930 else 931 lastModified = url.openConnection().getLastModified(); 932 933 deployment.lastModified=lastModified; 934 deployment.lastDeployed=System.currentTimeMillis(); 935 } 936 catch (IOException ignore) 937 { 938 deployment.lastModified=System.currentTimeMillis(); 939 deployment.lastDeployed=System.currentTimeMillis(); 940 } 941 942 synchronized (deploymentList) 943 { 944 if (!inLocalCopyDir(deployment.url) && deploymentList.contains(deployment) == false) 946 { 947 deploymentList.add(deployment); 948 log.debug("Watching new file: " + deployment.url); 949 } 950 } 951 } 952 return true; 953 } 954 955 964 private void create(DeploymentInfo deployment) throws DeploymentException 965 { 966 log.debug("create step for deployment " + deployment.url); 967 try 968 { 969 ArrayList sortedSubs = new ArrayList (deployment.subDeployments); 970 Collections.sort(sortedSubs, infoSorter); 971 for (Iterator lt = sortedSubs.listIterator(); lt.hasNext();) 972 { 973 create((DeploymentInfo) lt.next()); 974 } 975 deployment.state = DeploymentState.CREATE_SUBDEPLOYMENTS; 976 977 if (deployment.deployer != null) 979 { 980 try 981 { 982 deployment.state = DeploymentState.CREATE_DEPLOYER; 983 deployment.deployer.create(deployment); 984 deployment.state = DeploymentState.CREATED; 986 deployment.status="Created"; 987 log.debug("Done with create step of deploying " + deployment.shortName); 988 } 989 catch (Throwable t) 990 { 991 log.error("Could not create deployment: " + deployment.url, t); 992 throw t; 993 } 994 } 995 else 996 { 997 log.debug("Still no deployer for package in create step: " + deployment.shortName); 998 } } 1000 catch (Throwable t) 1001 { 1002 log.trace("could not create deployment: " + deployment.url, t); 1003 deployment.status = "Deployment FAILED reason: " + t.getMessage(); 1004 deployment.state = DeploymentState.FAILED; 1005 DeploymentException.rethrowAsDeploymentException("Could not create deployment: " + deployment.url, t); 1006 } 1007 } 1008 1009 1017 private void start(DeploymentInfo deployment) throws DeploymentException 1018 { 1019 deployment.status = "Starting"; 1020 log.debug("Begin deployment start " + deployment.url); 1021 try 1022 { 1023 ArrayList sortedSubs = new ArrayList (deployment.subDeployments); 1024 Collections.sort(sortedSubs, infoSorter); 1025 for (Iterator lt = sortedSubs.listIterator(); lt.hasNext();) 1026 { 1027 start((DeploymentInfo) lt.next()); 1028 } 1029 deployment.state = DeploymentState.START_SUBDEPLOYMENTS; 1030 1031 if (deployment.deployer != null) 1033 { 1034 try 1035 { 1036 deployment.state = DeploymentState.START_DEPLOYER; 1037 deployment.deployer.start(deployment); 1038 Object [] args = {deployment, DeploymentState.STARTED}; 1040 String [] sig = {"org.jboss.deployment.DeploymentInfo", 1041 "org.jboss.deployment.DeploymentState"}; 1042 server.invoke(serviceController, "validateDeploymentState",args, sig); 1043 deployment.status = "Deployed"; 1044 log.debug("End deployment start on package: "+ deployment.shortName); 1045 } 1046 catch (Throwable t) 1047 { 1048 log.error("Could not start deployment: " + deployment.url, t); 1049 throw t; 1050 } 1051 } 1052 else 1053 { 1054 log.debug("Still no deployer for package in start step: " + deployment.shortName); 1055 } } 1057 catch (Throwable t) 1058 { 1059 log.trace("could not start deployment: " + deployment.url, t); 1060 deployment.state = DeploymentState.FAILED; 1061 deployment.status = "Deployment FAILED reason: " + t.getMessage(); 1062 DeploymentException.rethrowAsDeploymentException("Could not create deployment: " + deployment.url, t); 1063 } 1064 } 1065 1066 1072 private void findDeployer(DeploymentInfo sdi) 1073 { 1074 if( sdi.deployer != null ) 1076 { 1077 log.debug("using existing deployer "+sdi.deployer); 1078 return; 1079 } 1080 1081 synchronized(deployers) 1086 { 1087 for (Iterator iterator = deployers.iterator(); iterator.hasNext(); ) 1088 { 1089 SubDeployer deployer = (SubDeployer) iterator.next(); 1090 if (deployer.accepts(sdi)) 1091 { 1092 sdi.deployer = deployer; 1093 log.debug("using deployer "+deployer); 1094 return; 1095 } 1096 } 1097 } 1098 log.debug("No deployer found for url: " + sdi.url); 1099 } 1100 1101 1107 private void parseManifestLibraries(DeploymentInfo sdi) 1108 { 1109 String classPath = null; 1110 1111 Manifest mf = sdi.getManifest(); 1112 1113 if( mf != null ) 1114 { 1115 Attributes mainAttributes = mf.getMainAttributes(); 1116 classPath = mainAttributes.getValue(Attributes.Name.CLASS_PATH); 1117 } 1118 1119 if (classPath != null) 1120 { 1121 StringTokenizer st = new StringTokenizer (classPath); 1122 log.debug("resolveLibraries: "+classPath); 1123 1124 while (st.hasMoreTokens()) 1125 { 1126 URL lib = null; 1127 String tk = st.nextToken(); 1128 log.debug("new manifest entry for sdi at "+sdi.shortName+" entry is "+tk); 1129 1130 try 1131 { 1132 if (sdi.isDirectory) 1133 { 1134 File parentDir = new File (sdi.url.getPath()).getParentFile(); 1135 lib = new File (parentDir, tk).toURL(); 1136 } 1137 else 1138 { 1139 lib = new URL (sdi.url, tk); 1140 } 1141 1142 if ( deploymentMap.containsKey(lib) == false ) 1144 { 1145 1149 DeploymentInfo mfRef = new DeploymentInfo(lib, null, getServer()); 1150 makeLocalCopy(mfRef); 1151 URL [] localURL = {mfRef.localUrl}; 1152 mfRef.localCl = new java.net.URLClassLoader (localURL); 1153 findDeployer(mfRef); 1154 SubDeployer deployer = mfRef.deployer; 1155 if(deployer != null && (deployer instanceof JARDeployer) == false) 1156 { 1157 log.warn("Found non-jar deployer for " + tk + ": " + deployer); 1159 } 1160 1161 sdi.addLibraryJar(lib); 1163 } 1164 } 1165 catch (Exception ignore) 1166 { 1167 log.debug("The manifest entry in "+sdi.url+" references URL "+lib+ 1168 " which could not be opened, entry ignored", ignore); 1169 } 1170 } 1171 } 1172 } 1173 1174 1178 private void makeLocalCopy(DeploymentInfo sdi) 1179 { 1180 try 1181 { 1182 if (sdi.url.getProtocol().equals("file") && (!copyFiles || sdi.isDirectory)) 1183 { 1184 sdi.localUrl = sdi.url; 1186 return; 1187 } 1188 else if (inLocalCopyDir(sdi.url)) 1190 { 1191 sdi.localUrl = sdi.url; 1192 return; 1193 } 1194 else 1195 { 1196 String shortName = sdi.shortName; 1197 File localFile = File.createTempFile("tmp", shortName, tempDir); 1198 sdi.localUrl = localFile.toURL(); 1199 copy(sdi.url, localFile); 1200 } 1201 } 1202 catch (Exception e) 1203 { 1204 log.error("Could not make local copy for " + sdi.url, e); 1205 } 1206 } 1207 1208 private boolean inLocalCopyDir(URL url) 1209 { 1210 int i = 0; 1211 String urlTest = url.toString(); 1212 if( urlTest.startsWith("jar:") ) 1213 i = 4; 1214 1215 return urlTest.startsWith(tempDirString, i); 1216 } 1217 1218 protected void copy(URL src, File dest) throws IOException 1219 { 1220 log.debug("Copying " + src + " -> " + dest); 1221 1222 File dir = dest.getParentFile(); 1224 if (!dir.exists()) 1225 { 1226 boolean created = dir.mkdirs(); 1227 if( created == false ) 1228 throw new IOException ("mkdirs failed for: "+dir.getAbsolutePath()); 1229 } 1230 1231 if( dest.exists() == true ) 1233 { 1234 boolean deleted = Files.delete(dest); 1235 if( deleted == false ) 1236 throw new IOException ("delete of previous content failed for: "+dest.getAbsolutePath()); 1237 } 1238 1239 if (src.getProtocol().equals("file")) 1240 { 1241 File srcFile = new File (src.getFile()); 1242 if (srcFile.isDirectory()) 1243 { 1244 log.debug("Making zip copy of: " + srcFile); 1245 OutputStream out = new BufferedOutputStream (new FileOutputStream (dest)); 1247 JarUtils.jar(out, srcFile.listFiles()); 1248 out.close(); 1249 return; 1250 } 1251 } 1252 1253 InputStream in = new BufferedInputStream (src.openStream()); 1254 OutputStream out = new BufferedOutputStream (new FileOutputStream (dest)); 1255 Streams.copy(in, out); 1256 out.flush(); 1257 out.close(); 1258 in.close(); 1259 } 1260 1261 1267 public void start(String urlspec) 1268 throws DeploymentException, MalformedURLException 1269 { 1270 throw new DeploymentException("Not supported"); 1271 } 1272 1273 1279 public void stop(String urlspec) 1280 throws DeploymentException, MalformedURLException 1281 { 1282 throw new DeploymentException("Not supported"); 1283 } 1284 1285 1294 public boolean isDeployed(String url) 1295 throws MalformedURLException 1296 { 1297 return isDeployed(new URL (url)); 1298 } 1299 1300 1307 public boolean isDeployed(URL url) 1308 { 1309 DeploymentContext di = delegate.getDeploymentContext(url.toString()); 1310 if (di == null) 1311 { 1312 return false; 1313 } return di.getState() == org.jboss.deployers.spi.structure.DeploymentState.DEPLOYED; 1315 } 1316 1317 1325 public DeploymentContext getDeployment(URL url) 1326 { 1327 String name = contextMap.get(url); 1328 DeploymentContext dc = delegate.getDeploymentContext(name); 1329 log.info("getDeploymen, url="+url+", dc="+dc); 1330 return dc; 1331 } 1332 1333 1341 public URL getWatchUrl(URL url) 1342 { 1343 return url; 1344 } 1345 1346 1351 public void checkIncompleteDeployments() throws DeploymentException 1352 { 1353 try 1354 { 1355 Collection waitingForClasses = new HashSet (); 1356 Collection waitingForDepends = (Collection )server.invoke(serviceController, 1357 "listIncompletelyDeployed", 1358 new Object [] {}, 1359 new String [] {}); 1360 Collection allServices = (Collection ) server.invoke(serviceController, 1361 "listDeployed", 1362 new Object [] {}, 1363 new String [] {}); 1364 1365 Collection rootCause = new HashSet (waitingForDepends); 1367 Collection missing = new HashSet (); 1368 for (Iterator i = rootCause.iterator(); i.hasNext();) 1369 { 1370 ServiceContext ctx = (ServiceContext) i.next(); 1371 for (Iterator j = ctx.iDependOn.iterator(); j.hasNext(); ) 1372 { 1373 ServiceContext dependee = (ServiceContext) j.next(); 1374 if (dependee.state != ServiceContext.RUNNING) 1375 { 1376 if (allServices.contains(dependee) == false) 1378 missing.add(dependee); 1379 i.remove(); 1381 break; 1382 } 1383 } 1384 } 1385 rootCause.addAll(missing); 1387 1388 IncompleteDeploymentException ide = new IncompleteDeploymentException( 1389 waitingForClasses, 1390 waitingForDepends, 1391 rootCause, 1392 listIncompletelyDeployed(), 1393 listWaitingForDeployer()); 1394 if (!ide.isEmpty()) 1395 { 1396 throw ide; 1397 } } 1399 catch (JMException jme) 1400 { 1401 throw new DeploymentException(JMXExceptionDecoder.decode(jme)); 1402 } } 1404 1405 1409 private void fillParentAndChildrenSDI(DeploymentInfo parent, Map map) 1410 { 1411 Set subDeployments = parent.subDeployments; 1412 Iterator it = subDeployments.iterator(); 1413 while (it.hasNext()) 1414 { 1415 DeploymentInfo child = (DeploymentInfo) it.next(); 1416 SerializableDeploymentInfo sdichild = returnSDI(child, map); 1417 sdichild.parent = returnSDI(parent, map); 1418 sdichild.parent.subDeployments.add(sdichild); 1419 fillParentAndChildrenSDI(child, map); 1420 } 1421 } 1422 1423 private SerializableDeploymentInfo returnSDI(DeploymentInfo di, Map map) 1424 { 1425 SerializableDeploymentInfo sdi = (SerializableDeploymentInfo) map.get(di.url); 1426 if( sdi == null ) 1427 { 1428 sdi = new SerializableDeploymentInfo(di); 1429 map.put(di.url, sdi); 1430 } 1431 return sdi; 1432 } 1433 1434} 1435 | Popular Tags |