1 22 package org.objectweb.petals.jbi.management.service; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.net.URI ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.zip.ZipFile ; 34 35 import javax.jbi.management.LifeCycleMBean; 36 import javax.jbi.management.MBeanNames; 37 import javax.management.MBeanServer ; 38 import javax.management.ObjectName ; 39 import javax.naming.InitialContext ; 40 import javax.naming.NamingException ; 41 42 import org.apache.commons.io.FileUtils; 43 import org.objectweb.fractal.fraclet.annotation.FractalComponent; 44 import org.objectweb.fractal.fraclet.annotation.Interface; 45 import org.objectweb.fractal.fraclet.annotation.LifeCycle; 46 import org.objectweb.fractal.fraclet.annotation.LifeCycleType; 47 import org.objectweb.fractal.fraclet.annotation.Monolog; 48 import org.objectweb.fractal.fraclet.annotation.Provides; 49 import org.objectweb.fractal.fraclet.annotation.Requires; 50 import org.objectweb.petals.PetalsException; 51 import org.objectweb.petals.classloader.LoaderManager; 52 import org.objectweb.petals.jbi.component.context.ComponentContextImpl; 53 import org.objectweb.petals.jbi.component.context.ComponentInitialContext; 54 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle; 55 import org.objectweb.petals.jbi.component.lifecycle.Installer; 56 import org.objectweb.petals.jbi.management.autoload.AutoLoaderImpl; 57 import org.objectweb.petals.jbi.management.systemstate.ComponentState; 58 import org.objectweb.petals.jbi.management.systemstate.SharedLibraryState; 59 import org.objectweb.petals.jbi.management.systemstate.SystemState; 60 import org.objectweb.petals.jbi.registry.ConsumerEndpoint; 61 import org.objectweb.petals.jbi.registry.Registry; 62 import org.objectweb.petals.jbi.routing.Router; 63 import org.objectweb.petals.repository.RepositoryService; 64 import org.objectweb.petals.tools.jbicommon.descriptor.ComponentDescription; 65 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 66 import org.objectweb.petals.tools.jbicommon.descriptor.SharedLibrary; 67 import org.objectweb.petals.tools.jbicommon.descriptor.SharedLibraryList; 68 import org.objectweb.petals.util.LoggingUtil; 69 import org.objectweb.petals.util.ParameterCheckHelper; 70 import org.objectweb.petals.util.StringHelper; 71 import org.objectweb.petals.util.SystemUtil; 72 import org.objectweb.petals.util.ZipUtil; 73 import org.objectweb.util.monolog.api.Logger; 74 75 83 @FractalComponent 84 @Provides(interfaces=@Interface(name="service",signature=org.objectweb.petals.jbi.management.service.InstallationService.class)) 85 public class InstallationServiceImpl implements InstallationService { 86 87 public static final String ERROR_INSTALL_SHARED_LIBRARY = "Could not install the shared library "; 88 89 93 @Requires(name="endpoint-service",signature=org.objectweb.petals.jbi.management.service.EndpointService.class) 94 protected EndpointService endpointService; 95 96 99 @Requires(name="lifecyclemanager",signature=org.objectweb.petals.jbi.management.service.LifeCycleManagerService.class) 100 protected LifeCycleManagerService jmxAdmin; 101 102 105 @Requires(name="classloader",signature=org.objectweb.petals.classloader.LoaderManager.class) 106 protected LoaderManager loaderSrv; 107 108 111 protected LoggingUtil log; 112 113 116 @Monolog(name="logger") 117 protected Logger logger; 118 119 122 protected PackageHandler packageHandler; 123 124 127 protected List <String > componentLoadedCache; 128 129 132 protected List <String > slLoadedCache; 133 134 137 @Requires(name="systemstate",signature=org.objectweb.petals.jbi.management.systemstate.SystemState.class) 138 protected SystemState recoverySrv; 139 140 143 @Requires(name="registry",signature=org.objectweb.petals.jbi.registry.Registry.class) 144 protected Registry registryService; 145 146 149 @Requires(name="repository",signature=org.objectweb.petals.repository.RepositoryService.class) 150 protected RepositoryService repositorySrv; 151 152 155 @Requires(name="router",signature=org.objectweb.petals.jbi.routing.Router.class) 156 protected Router router; 157 158 protected InitialContext componentInitialContext; 159 160 163 public InstallationServiceImpl() { 164 165 } 166 167 172 public synchronized String installSharedLibrary(String slZipURL) { 173 log.start(); 174 ParameterCheckHelper.isNullParameterWithLog(slZipURL, "slZipURL", log); 175 ParameterCheckHelper.isEmptyParameterWithLog(slZipURL, "slZipURL", log); 176 try { 177 new URL (slZipURL); 178 } catch (MalformedURLException e) { 179 throw new IllegalArgumentException ("slZipURL is not a valid URL"); 180 } 181 182 String msg; 183 String result = null; 184 185 JBIDescriptor descriptor = null; 186 String slName = null; 187 URI archiveURI = null; 188 189 try { 190 193 194 archiveURI = packageHandler.processAndGetPackageURI(slZipURL, true); 195 196 199 200 descriptor = packageHandler.loadDescriptor(archiveURI); 201 202 205 206 checkSharedLibrary(descriptor, archiveURI); 207 } catch (Throwable e) { 208 msg = "Shared library package (" + slZipURL + ") isn't validated."; 209 log.error(msg, e); 210 throw new RuntimeException (msg); 211 } 212 try { 213 slName = getSharedLibraryName(descriptor); 214 217 218 URI installationRoot = explodeSL(slName, archiveURI); 219 220 223 224 result = installSharedLibrary(installationRoot, descriptor); 225 226 229 File file = createFileFromURI(archiveURI); 230 File installedArchive = getInstalledArchive(file); 231 recoverySrv.createSharedLibraryStateHolder(slName, installationRoot 232 .toURL().toString(), installedArchive.toURL().toString()); 233 234 237 copyFile(file, installedArchive); 238 239 242 createSuccessFile(installedArchive); 243 244 log.info("Shared library " + slName + " correctly installed !"); 245 } catch (Throwable e) { 246 msg = ERROR_INSTALL_SHARED_LIBRARY + slZipURL; 247 log.error(msg, e); 248 249 reverseSLInstallation(slName); 253 254 throw new RuntimeException (msg); 255 } 256 257 log.end(); 258 259 return result; 260 } 261 262 274 protected URI explodeSL(String slName, URI archiveURI) 275 throws ManagementException { 276 String msg; 277 URI installationRoot = null; 278 279 try { 280 installationRoot = repositorySrv.addSharedLibPackage(slName, 281 archiveURI); 282 283 } catch (PetalsException pe) { 284 msg = "Unexpected error adding JBI installation " 285 + "package into repository. "; 286 log.error(msg, pe); 287 throw new ManagementException(msg, pe); 288 } 289 290 if (installationRoot == null) { 291 msg = "Unexpected Platform repository service error. The SL" 292 + "installation root must NOT be null: " + slName; 293 log.error(msg); 294 throw new ManagementException(msg); 295 } 296 return installationRoot; 297 } 298 299 305 public synchronized String installSharedLibrary(URI installationRoot, 306 JBIDescriptor descriptor) throws PetalsException { 307 log.start(); 308 ParameterCheckHelper.isNullParameterWithLog(installationRoot, 309 "installationRoot", log); 310 ParameterCheckHelper.isNullParameterWithLog(descriptor, "descriptor", 311 log); 312 String msg; 313 314 317 SharedLibrary sl = descriptor.getSharedLibrary(); 318 319 322 boolean parentFirst = isParentFirst(sl); 323 324 327 328 List <URL > urls = new ArrayList <URL >(); 329 330 try { 331 urls.add(installationRoot.toURL()); 332 } catch (MalformedURLException e) { 333 msg = "Unexpected error creating URL for shared lib classloader."; 337 log.info(msg, e); 338 throw new PetalsException(msg, e); 339 } 340 341 URL [] baseUrls = urls.toArray(new URL [] {}); 342 343 346 String slName = sl.getIdentification().getName(); 347 try { 348 loaderSrv.createSharedLibraryClassLoader(slName, baseUrls, sl 349 .getSharedLibraryClassPath(), parentFirst); 350 } catch (PetalsException pe) { 351 msg = "Unexpected error creating class loader. "; 352 log.error(msg, pe); 353 throw new PetalsException(msg, pe); 354 } 355 356 359 slLoadedCache.add(slName); 360 361 log.end(); 362 return slName; 363 } 364 365 371 public boolean isSLLoaded(String slID) { 372 return slLoadedCache.contains(slID); 373 } 374 375 381 public boolean isComponentLoaded(String compID) { 382 return componentLoadedCache.contains(compID); 383 } 384 385 388 public synchronized ObjectName loadInstaller(String componentName) { 389 ParameterCheckHelper.isNullParameterWithLog(componentName, 390 "componentName", log); 391 ParameterCheckHelper.isEmptyParameterWithLog(componentName, 392 "componentName", log); 393 ObjectName result = null; 394 result = jmxAdmin.getInstallerByName(componentName); 395 return result; 396 } 397 398 407 public synchronized ObjectName loadNewInstaller(String installZipURL) { 408 log.start(); 409 ParameterCheckHelper.isNullParameterWithLog(installZipURL, 410 "installZipURL", log); 411 ParameterCheckHelper.isEmptyParameterWithLog(installZipURL, 412 "installZipURL", log); 413 try { 414 new URL (installZipURL); 415 } catch (Throwable e) { 416 throw new IllegalArgumentException ( 417 "installZipUrl must be a legal url", e); 418 } 419 String msg = null; 420 ObjectName result = null; 421 JBIDescriptor descriptor = null; 422 String componentName = null; 423 URI archiveURI = null; 424 425 try { 426 429 430 archiveURI = packageHandler.processAndGetPackageURI(installZipURL, 431 true); 432 433 436 437 descriptor = packageHandler.loadDescriptor(archiveURI); 438 439 442 443 checkComponent(descriptor, archiveURI); 444 445 } catch (Throwable e) { 446 msg = "Component package (" + installZipURL + ") isn't validated."; 447 log.error(msg, e); 448 449 throw new RuntimeException (msg); 450 } 451 452 try { 453 componentName = getComponentNameFromJBIDescriptor(descriptor); 454 455 458 459 URI installationRoot = explodeComponent(componentName, archiveURI); 460 461 464 465 result = loadNewInstaller(installationRoot, descriptor); 466 467 470 File file = createFileFromURI(archiveURI); 471 File installedArchive = getInstalledArchive(file); 472 recoverySrv.createComponentStateHolder(componentName, 473 installationRoot.toURL().toString(), installedArchive.toURI() 474 .toString(), Installer.UNINSTALLED, LifeCycleMBean.UNKNOWN); 475 476 479 copyFile(file, installedArchive); 480 481 484 createSuccessFile(installedArchive); 485 486 log.info("Component " + componentName + " correctly installed ! "); 487 } catch (Throwable e) { 488 msg = "Component can't be installed: " + installZipURL; 489 log.error(msg, e); 490 491 reverseComponentInstallation(componentName, result); 494 495 throw new RuntimeException (msg); 496 } 497 log.end(); 498 499 return result; 500 } 501 502 514 protected URI explodeComponent(String componentName, URI archiveURI) 515 throws ManagementException { 516 String msg; 517 URI installationRoot = null; 518 519 try { 520 installationRoot = repositorySrv.addComponentPackage(componentName, 521 archiveURI); 522 523 } catch (PetalsException pe) { 524 msg = "Unexpected error adding JBI installation " 525 + "package into repository. "; 526 log.error(msg, pe); 527 throw new ManagementException(msg, pe); 528 } 529 530 if (installationRoot == null) { 531 msg = "Unexpected Platform repository service error. The Component" 532 + "installation root must NOT be null: " + componentName; 533 log.error(msg); 534 throw new ManagementException(msg); 535 } 536 return installationRoot; 537 } 538 539 545 public synchronized ObjectName loadNewInstaller(URI installationRoot, 546 JBIDescriptor descriptor) throws PetalsException { 547 log.start(); 548 ParameterCheckHelper.isNullParameterWithLog(installationRoot, 549 "installationRoot", log); 550 ParameterCheckHelper.isNullParameterWithLog(descriptor, "descriptor", 551 log); 552 553 558 checkInstalledSLForComp(descriptor); 559 560 563 564 ComponentContextImpl compContextImpl = createComponentContext( 565 descriptor, installationRoot); 566 567 570 571 ObjectName result = createAndRegisterInstallerMBean(compContextImpl, 572 descriptor, installationRoot); 573 574 577 578 componentLoadedCache.add(getComponentNameFromJBIDescriptor(descriptor)); 579 580 log.end(); 581 return result; 582 } 583 584 587 public synchronized boolean uninstallSharedLibrary(String slName) { 588 log.start(); 589 ParameterCheckHelper.isNullParameterWithLog(slName, "slName", log); 590 ParameterCheckHelper.isEmptyParameterWithLog(slName, "slName", log); 591 boolean result = false; 592 593 597 if (checkComponentShutdownStateForSL(slName)) { 598 601 loaderSrv.deleteClassLoader(slName); 602 603 slLoadedCache.remove(slName); 604 605 608 SharedLibraryState slState = null; 609 try { 610 slState = recoverySrv.deleteSharedLibraryStateHolder(slName); 611 } catch (Exception e) { 612 String msg = "Shared library isn't completely uninstalled ! " 613 + "State holder isn't deleted"; 614 log.error(msg, e); 615 return false; 616 } 617 618 621 try { 622 result = repositorySrv.removeSharedLibPackage(slName); 623 } catch (PetalsException e1) { 624 String msg = "Shared library isn't completely uninstalled ! " 625 + "Failed to remove shared lib package."; 626 log.error(msg, e1); 627 return false; 628 } 629 630 633 try { 634 File installedArchive = null; 635 if (slState != null) { 636 URI archiveURI = packageHandler.processAndGetPackageURI( 637 slState.getArchiveURL(), false); 638 installedArchive = createFileFromURI(archiveURI); 639 } 640 if (installedArchive != null && installedArchive.exists()) { 641 File uninstalledArchive = getUninstalledArchive(installedArchive); 642 installedArchive.renameTo(uninstalledArchive); 643 installedArchive = null; 644 645 } 646 } catch (IllegalArgumentException e) { 647 log.error("Error getting URI from package url. ", e); 648 return false; 649 } 650 } 651 log.end(); 652 653 return result; 654 } 655 656 664 public synchronized boolean unloadInstaller(String componentName, 665 boolean isToBeDeleted) { 666 log.start(); 667 ParameterCheckHelper.isNullParameterWithLog(componentName, 668 "componentName", log); 669 ParameterCheckHelper.isEmptyParameterWithLog(componentName, 670 "componentName", log); 671 boolean result = false; 672 673 676 ObjectName instName = jmxAdmin.getInstallerByName(componentName); 677 MBeanServer mbeanSrv = jmxAdmin.getMBeanServer(); 678 Object [] nullObjects = new Object [0]; 679 String [] nullStrings = new String [0]; 680 try { 681 boolean isInstalled = ((Boolean ) mbeanSrv.getAttribute(instName, 682 "Installed")).booleanValue(); 683 if (isInstalled) { 684 mbeanSrv 685 .invoke(instName, "uninstall", nullObjects, nullStrings); 686 } 687 } catch (Throwable e) { 688 log.error("Error uninstalling component : " + componentName + "." 689 + e.getMessage(), e); 690 return false; 691 } 692 693 696 try { 697 jmxAdmin.unregisterInstaller(instName); 698 } catch (ManagementException e) { 699 log.error("Error uninstalling component : " + componentName + "." 700 + e.getMessage(), e); 701 return false; 702 } 703 704 707 componentLoadedCache.remove(componentName); 708 709 712 ComponentState componentState = null; 713 try { 714 componentState = recoverySrv 715 .deleteComponentStateHolder(componentName); 716 } catch (Exception e) { 717 String msg = "Component isn't completely uninstalled ! " 718 + "State holder isn't deleted"; 719 log.error(msg, e); 720 return false; 721 } 722 723 726 727 try { 728 result = repositorySrv.removeComponentPackage(componentName); 729 } catch (PetalsException e1) { 730 String msg = "Component isn't completely uninstalled ! " 731 + "Failed to delete component package"; 732 log.error(msg, e1); 733 return false; 734 } 735 736 739 try { 740 File installedArchive = null; 741 if (componentState != null) { 742 URI archiveURI = packageHandler.processAndGetPackageURI( 743 componentState.getArchiveURL(), false); 744 installedArchive = createFileFromURI(archiveURI); 745 746 } 747 if (installedArchive != null && installedArchive.exists()) { 748 File uninstalledArchive = getUninstalledArchive(installedArchive); 749 installedArchive.renameTo(uninstalledArchive); 750 installedArchive = null; 751 755 756 } 757 762 } catch (IllegalArgumentException e) { 763 log.error("Error uninstalling component : " + componentName + "." 764 + e.getMessage(), e); 765 return false; 766 } 767 768 log.end(); 769 return result; 770 } 771 772 781 protected void checkClassPathElements(ZipFile archive, 782 List <String > classPath) throws PetalsException { 783 String msg; 784 785 for (String pathElement : classPath) { 786 787 790 if (pathElement == null || "".equals(pathElement.trim())) { 791 msg = "An empty class-path element found " 792 + "in the JBI Descriptor file (jbi.xml)."; 793 throw new PetalsException(msg); 794 } 795 796 799 if (!hasEntry(archive, pathElement)) { 800 msg = "A class-path element " + pathElement + " declared in " 801 + "JBI Descriptor not found within installation " 802 + "package Zip archive file."; 803 throw new PetalsException(msg); 804 } 805 } 806 } 807 808 821 protected void checkComponent(JBIDescriptor descriptor, URI archiveURI) 822 throws PetalsException { 823 String msg = null; 824 825 828 829 ComponentDescription comp = descriptor.getComponent(); 830 if (comp == null) { 831 msg = "The component production element was not found in " 832 + "JBI Descriptor within installation package: " 833 + archiveURI.getPath(); 834 log.error(msg); 835 throw new PetalsException(msg); 836 } else { 837 838 841 842 String compName = comp.getIdentification().getName(); 843 844 if (StringHelper.isEmpty(compName)) { 845 msg = "The component name must be non null and non empty for the new component :" 846 + archiveURI.getPath(); 847 log.error(msg); 848 throw new PetalsException(msg); 849 } 850 851 if (isComponentLoaded(compName)) { 852 msg = "Other installation package already exists loaded in " 853 + "the component framework: " + compName; 854 855 log.error(msg); 856 throw new PetalsException(msg); 857 } else { 858 859 862 863 ZipFile zipArchive = openZipFile(archiveURI); 864 865 try { 866 checkClassPathElements(zipArchive, comp 867 .getBootstrapClassPath()); 868 checkClassPathElements(zipArchive, comp 869 .getComponentClassPath()); 870 } finally { 871 try { 872 zipArchive.close(); 873 } catch (IOException e) { 874 msg = "Component archive can't be closed"; 875 log.error(msg); 876 throw new PetalsException(msg, e); 877 } 878 } 879 } 880 } 881 } 882 883 892 protected boolean checkComponentShutdownStateForSL(String slName) { 893 boolean result = true; 894 895 898 Collection <ComponentLifeCycle> bindings = jmxAdmin 899 .getBindingCompoLifeCycles().values(); 900 for (Iterator iter = bindings.iterator(); iter.hasNext() && result;) { 901 ComponentLifeCycle cycle = (ComponentLifeCycle) iter.next(); 902 903 906 ComponentDescription description = cycle.getComponentDescription(); 907 List <SharedLibraryList> sls = description.getSharedLibraryList(); 908 909 if (sls != null) { 910 for (Iterator iter2 = sls.iterator(); iter2.hasNext() && result;) { 911 SharedLibraryList sl = (SharedLibraryList) iter2.next(); 912 913 917 if (slName.equals(sl.getName()) 918 && !(LifeCycleMBean.SHUTDOWN.equals(cycle 919 .getCurrentState()))) { 920 log 921 .error("The shared lib can't be uninstalled because the following " 922 + "component isn't in the shutdown state : " 923 + description.getIdentification().getName()); 924 result = false; 925 } 926 } 927 } 928 } 929 930 933 Collection <ComponentLifeCycle> engines = jmxAdmin 934 .getEngineCompoLifeCycles().values(); 935 for (Iterator iter = engines.iterator(); iter.hasNext() && result;) { 936 ComponentLifeCycle cycle = (ComponentLifeCycle) iter.next(); 937 938 941 ComponentDescription description = cycle.getComponentDescription(); 942 List <SharedLibraryList> sls = description.getSharedLibraryList(); 943 944 if (sls != null) { 945 for (Iterator iter2 = sls.iterator(); iter2.hasNext() && result;) { 946 SharedLibraryList sl = (SharedLibraryList) iter2.next(); 947 948 952 if (slName.equals(sl.getName()) 953 && !(LifeCycleMBean.SHUTDOWN.equals(cycle 954 .getCurrentState()))) { 955 log 956 .error("The shared lib " 957 + slName 958 + " can't be uninstalled because" 959 + " the following component isn't in the shutdown state : " 960 + description.getIdentification().getName()); 961 result = false; 962 } 963 } 964 } 965 } 966 967 return result; 968 } 969 970 979 protected void checkInstalledSLForComp(JBIDescriptor descriptor) 980 throws PetalsException { 981 List <SharedLibraryList> sls = descriptor.getComponent() 982 .getSharedLibraryList(); 983 984 if (sls != null) { 985 for (SharedLibraryList list : sls) { 986 if (!slLoadedCache.contains(list.getName())) { 987 String msg = "Component depends on a not installed shared library : " 988 + list.getName(); 989 990 log.error(msg); 991 throw new PetalsException(msg); 992 } 993 } 994 } 995 } 996 997 1008 protected void checkSharedLibrary(JBIDescriptor descriptor, URI archiveURI) 1009 throws PetalsException { 1010 String msg = null; 1011 1012 1015 1016 SharedLibrary sl = descriptor.getSharedLibrary(); 1017 if (sl == null) { 1018 msg = "The shared-library production element was not found in " 1019 + "JBI Descriptor within installation package: " 1020 + archiveURI.getPath(); 1021 1022 log.error(msg); 1023 throw new PetalsException(msg); 1024 } else { 1025 1026 1029 1030 String slName = sl.getIdentification().getName(); 1031 1032 if (isSLLoaded(slName)) { 1033 msg = "Other installation package already exists loaded in " 1034 + "the component framework: " + slName; 1035 1036 log.error(msg); 1037 1038 throw new PetalsException(msg); 1039 } else { 1040 1041 1044 1045 ZipFile zipArchive = openZipFile(archiveURI); 1046 1047 try { 1048 checkClassPathElements(zipArchive, sl 1049 .getSharedLibraryClassPath()); 1050 } finally { 1051 try { 1052 zipArchive.close(); 1053 } catch (IOException e) { 1054 msg = "Shared library archive can't be closed"; 1055 log.error(msg, e); 1056 throw new PetalsException(msg, e); 1057 } 1058 } 1059 } 1060 1061 } 1062 } 1063 1064 1074 protected void copyFile(File src, File dest) throws IOException { 1075 FileUtils.copyFile(src, dest); 1076 } 1077 1078 1092 protected ObjectName createAndRegisterInstallerMBean( 1093 ComponentContextImpl componentContext, JBIDescriptor descriptor, 1094 URI installationRoot) throws PetalsException { 1095 log.start(); 1096 ObjectName result = null; 1097 ComponentDescription componentDescription = descriptor.getComponent(); 1098 try { 1099 MBeanNamesImpl mbeanNames = jmxAdmin.getMBeanNames(); 1100 result = mbeanNames.createInstallerMBeanName(componentDescription 1101 .getIdentification().getName()); 1102 1103 Installer installerMBean = new Installer(installationRoot, 1104 loaderSrv, componentContext, componentDescription, jmxAdmin, 1105 recoverySrv, result, logger); 1106 1107 jmxAdmin.registerInstaller(installerMBean); 1108 } catch (ManagementException e) { 1109 String msg = "Error creating installerMBean."; 1110 log.error(msg, e); 1111 throw new PetalsException(msg, e); 1112 } 1113 log.end(); 1114 return result; 1115 } 1116 1117 1129 protected ComponentContextImpl createComponentContext( 1130 JBIDescriptor descriptor, URI installationRoot) throws PetalsException { 1131 log.start(); 1132 1133 String componentName = descriptor.getComponent().getIdentification() 1134 .getName(); 1135 1136 String installationRootPath = new File (installationRoot) 1137 .getAbsolutePath(); 1138 1139 String workingRootPath = repositorySrv.getComponentWorkRoot( 1140 componentName).getAbsolutePath(); 1141 1142 MBeanServer beanServer = jmxAdmin.getMBeanServer(); 1143 MBeanNames beanNames = jmxAdmin.getMBeanNames(); 1144 1145 ConsumerEndpoint address = new ConsumerEndpoint(componentName, 1146 SystemUtil.getContainerName()); 1147 1148 if (componentInitialContext == null) { 1149 try { 1150 componentInitialContext = new ComponentInitialContext( 1151 registryService.getUsersContext()); 1152 } catch (NamingException e) { 1153 String msg = "Error creating InitialContext for components"; 1154 log.error(msg, e); 1155 throw new PetalsException(msg, e); 1156 } 1157 } 1158 1159 ComponentContextImpl componentContextImpl = new ComponentContextImpl( 1160 descriptor, installationRootPath, workingRootPath, beanNames, 1161 beanServer, jmxAdmin, address, endpointService, logger, router, 1162 componentInitialContext); 1163 1164 log.end(); 1165 1166 return componentContextImpl; 1167 } 1168 1169 1176 protected File createFileFromURI(URI fileURI) { 1177 return new File (fileURI); 1178 } 1179 1180 1189 protected void createSuccessFile(File file) throws IOException { 1190 new File (AutoLoaderImpl.getWorkDirectory(), file.getName() + ".success") 1191 .createNewFile(); 1192 } 1193 1194 1201 protected String getComponentNameFromJBIDescriptor(JBIDescriptor descriptor) { 1202 return descriptor.getComponent().getIdentification().getName(); 1203 } 1204 1205 1212 protected File getInstalledArchive(File file) { 1213 return new File (AutoLoaderImpl.getInstalledDirectory(), file.getName()); 1214 } 1215 1216 1223 protected String getSharedLibraryName(JBIDescriptor descriptor) { 1224 return descriptor.getSharedLibrary().getIdentification().getName(); 1225 } 1226 1227 1234 protected File getUninstalledArchive(File file) { 1235 return new File (AutoLoaderImpl.getUninstalledDirectory(), file 1236 .getName()); 1237 } 1238 1239 1248 protected boolean hasEntry(ZipFile archive, String pathElement) { 1249 return ZipUtil.hasEntry(archive, pathElement); 1250 } 1251 1252 1255 @LifeCycle(on=LifeCycleType.START) 1256 protected void start() { 1257 log = new LoggingUtil(logger); 1258 log.start(); 1259 1260 1263 slLoadedCache = new ArrayList <String >(); 1264 componentLoadedCache = new ArrayList <String >(); 1265 1266 1269 packageHandler = new PackageHandler(logger, repositorySrv); 1270 1271 log.end(); 1272 } 1273 1274 1278 protected boolean isParentFirst(SharedLibrary sl) { 1279 return JBIDescriptor.isParentFirst(sl.getClassLoaderDelegation()); 1280 } 1281 1282 1291 protected ZipFile openZipFile(URI archiveURI) throws PetalsException { 1292 return ZipUtil.openZipFile(archiveURI); 1293 } 1294 1295 1304 protected void reverseComponentInstallation(String componentName, 1305 ObjectName installerMBeanName) { 1306 if (!StringHelper.isEmpty(componentName)) { 1307 if (installerMBeanName != null) { 1309 try { 1310 jmxAdmin.unregisterInstaller(installerMBeanName); 1311 } catch (ManagementException e) { 1312 log 1314 .warning("Component installer can't be unregistered or doesn't exist"); 1315 } 1316 } 1317 1318 if (isComponentLoaded(componentName)) { 1320 componentLoadedCache.remove(componentName); 1321 } 1322 1323 try { 1325 recoverySrv.deleteComponentStateHolder(componentName); 1326 } catch (Exception e) { 1327 log 1329 .warning("Component state can't be deleted or doesn't exist"); 1330 } 1331 1332 try { 1334 File compRoot = repositorySrv.getComponentRoot(componentName); 1335 if (compRoot.exists()) { 1336 1337 FileUtils.forceDelete(compRoot); 1338 } 1339 } catch (Exception e) { 1340 log.warning("Component root can't be deleted or doesn't exist"); 1342 } 1343 } 1344 } 1345 1346 1353 protected void reverseSLInstallation(String slName) { 1354 if (!StringHelper.isEmpty(slName)) { 1355 1356 loaderSrv.deleteClassLoader(slName); 1358 1359 if (isComponentLoaded(slName)) { 1361 slLoadedCache.remove(slName); 1362 } 1363 1364 try { 1366 recoverySrv.deleteSharedLibraryStateHolder(slName); 1367 } catch (Exception e) { 1368 log 1370 .warning("Shared lib state can't be deleted or doesn't exist"); 1371 } 1372 1373 try { 1375 File compRoot = repositorySrv.getSharedLibRoot(slName); 1376 if (compRoot.exists()) { 1377 1378 FileUtils.forceDelete(compRoot); 1379 } 1380 } catch (Exception e) { 1381 log 1383 .warning("Shared lib root can't be deleted or doesn't exist"); 1384 } 1385 } 1386 } 1387 1388 1391 @LifeCycle(on=LifeCycleType.STOP) 1392 public void stop() { 1393 log.start(); 1394 1395 slLoadedCache = null; 1396 componentLoadedCache = null; 1397 1398 log.end(); 1399 } 1400 1401 1405 public void shutdown() throws Exception { 1406 List <String > removeList = new ArrayList <String >(componentLoadedCache); 1408 for (String compName : removeList) { 1409 unloadInstaller(compName, true); 1410 } 1411 1412 removeList = new ArrayList <String >(slLoadedCache); 1414 for (String slName : removeList) { 1415 uninstallSharedLibrary(slName); 1416 } 1417 1418 } 1419 1420} 1421 | Popular Tags |