Title: [1044] trunk/servicemix-core/src/main/java/org/servicemix/jbi/framework: InstallerMBean from a previously installed component can not be loaded

Diff

Modified: trunk/servicemix-core/src/main/java/org/servicemix/jbi/framework/InstallationService.java (1043 => 1044)

--- trunk/servicemix-core/src/main/java/org/servicemix/jbi/framework/InstallationService.java	2005-12-07 15:07:45 UTC (rev 1043)
+++ trunk/servicemix-core/src/main/java/org/servicemix/jbi/framework/InstallationService.java	2005-12-07 16:38:28 UTC (rev 1044)
@@ -120,12 +120,68 @@
      *            the component name identifying the installer to load.
      * @return - the JMX ObjectName of the InstallerMBean loaded from an existing installation context.
      */
-    public ObjectName loadInstaller(String aComponentName){
-        ObjectName result=null;
-        InstallerMBeanImpl installer=(InstallerMBeanImpl) installers.get(aComponentName);
-        if(installer!=null){
-            result=installer.getObjectName();
+    public ObjectName loadInstaller(String aComponentName) {
+        InstallerMBeanImpl installer = (InstallerMBeanImpl) installers.get(aComponentName);
+        if (installer == null) {
+            LocalComponentConnector cnn = container.getLocalComponentConnector(aComponentName);
+            if (cnn == null) {
+                throw new RuntimeException("Could not find Component : " + aComponentName);
+            }
+            try {
+                ComponentContextImpl context = cnn.getContext();
+                File installationDir = environmentContext.getInstallationDirectory(aComponentName);
+                Descriptor root = AutoDeploymentService.buildDescriptor(installationDir);
+                Component descriptor = root.getComponent();
+
+                String name = descriptor.getIdentification().getName();
+                InstallationContextImpl installationContext = new InstallationContextImpl();
+                installationContext.setInstall(false);
+                installationContext.setComponentName(name);
+                installationContext.setComponentDescription(descriptor.getIdentification().getDescription());
+                installationContext.setInstallRoot(installationDir);
+                installationContext.setComponentClassName(descriptor.getComponentClassName());
+                ClassPath cp = descriptor.getComponentClassPath();
+                if (cp != null) {
+                    installationContext.setClassPathElements(cp.getPathElements());
+                }
+                // now build the ComponentContext
+                installationContext.setContext(context);
+                InstallationDescriptorExtension desc = descriptor.getDescriptorExtension();
+                if (desc != null) {
+                    installationContext.setDescriptorExtension(desc.getDescriptorExtension());
+                }
+                installationContext.setBinding(descriptor.isBindingComponent());
+                installationContext.setEngine(descriptor.isServiceEngine());
+                // now we must initialize the boot strap class
+                String bootstrapClassName = descriptor.getBootstrapClassName();
+                ClassPath bootStrapClassPath = descriptor.getBootstrapClassPath();
+                InstallationClassLoader bootstrapLoader = null;
+                if (bootstrapClassName != null && bootstrapClassName.length() > 0) {
+                    boolean parentFirst = descriptor.isBootstrapClassLoaderDelegationParentFirst();
+                    bootstrapLoader = classLoaderService.buildClassLoader(
+                            installationDir, bootStrapClassPath.getPathElements(), parentFirst);
+                }
+                SharedLibraryList[] lists = descriptor.getSharedLibraries();
+                String componentClassName = descriptor.getComponentClassName();
+                ClassPath componentClassPath = descriptor.getComponentClassPath();
+                boolean parentFirst = descriptor.isComponentClassLoaderDelegationParentFirst();
+                ClassLoader componentClassLoader = classLoaderService.buildClassLoader(installationDir, componentClassPath
+                                .getPathElements(), parentFirst, lists);
+                installer = new InstallerMBeanImpl(container,
+                        installationContext, componentClassLoader,
+                        componentClassName, bootstrapLoader,
+                        bootstrapClassName, true);
+                // create an MBean for the installer
+                ObjectName objectName = managementContext.createCustomComponentMBeanName(InstallerMBean.class.getName(), name);
+                installer.setObjectName(objectName);
+                managementContext.registerMBean(objectName, installer,
+                        InstallerMBean.class,
+                        "standard installation controls for a Component");
+            } catch (Exception e) {
+                throw new RuntimeException("Could not load installer", e);
+            }
         }
+        ObjectName result = installer.getObjectName();
         return result;
     }
 
@@ -420,7 +476,7 @@
             ClassLoader componentClassLoader=classLoaderService.buildClassLoader(installationDir,componentClassPath
                             .getPathElements(),parentFirst,lists);
             result=new InstallerMBeanImpl(container,installationContext,componentClassLoader,componentClassName,
-                            bootstrapLoader,bootstrapClassName);
+                            bootstrapLoader,bootstrapClassName, false);
             // create an MBean for the installer
             ObjectName objectName=managementContext.createCustomComponentMBeanName(InstallerMBean.class.getName(),name);
             result.setObjectName(objectName);

Modified: trunk/servicemix-core/src/main/java/org/servicemix/jbi/framework/InstallerMBeanImpl.java (1043 => 1044)

--- trunk/servicemix-core/src/main/java/org/servicemix/jbi/framework/InstallerMBeanImpl.java	2005-12-07 15:07:45 UTC (rev 1043)
+++ trunk/servicemix-core/src/main/java/org/servicemix/jbi/framework/InstallerMBeanImpl.java	2005-12-07 16:38:28 UTC (rev 1044)
@@ -39,7 +39,7 @@
     private static final Log log = LogFactory.getLog(InstallerMBeanImpl.class);
     private InstallationContextImpl context;
     private Bootstrap bootstrap;
-    private boolean installed = false;
+    private boolean installed;
     private ClassLoader componentClassLoader;
     private String componentClassName;
     private ClassLoader bootstrapClassLoader;
@@ -58,14 +58,20 @@
      * @param bootstrapClassName
      * @throws DeploymentException
      */
-    public InstallerMBeanImpl(JBIContainer container, InstallationContextImpl ic, ClassLoader componentLoader,
-            String componentClassName, ClassLoader bootstrapLoader, String bootstrapClassName) throws DeploymentException {
+    public InstallerMBeanImpl(JBIContainer container, 
+                              InstallationContextImpl ic, 
+                              ClassLoader componentLoader,
+                              String componentClassName, 
+                              ClassLoader bootstrapLoader, 
+                              String bootstrapClassName,
+                              boolean installed) throws DeploymentException {
         this.container = container;
         this.context = ic;
         this.componentClassLoader = componentLoader;
         this.componentClassName = componentClassName;
         this.bootstrapClassLoader = bootstrapLoader;
         this.bootstrapClassName = bootstrapClassName;
+        this.installed = installed;
         //initialize the bootstrap
         Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
         if (bootstrapLoader != null && bootstrapClassName != null && bootstrapClassName.length() > 0){
@@ -78,7 +84,7 @@
                         throw new DeploymentException("Could not find bootstrap class: " + bootstrapClassName);
                     }
                     this.bootstrap = (Bootstrap) bootstrapClass.newInstance();
-                    this.bootstrap.init(ic);
+                    this.bootstrap.init(this.context);
                 }
                 catch (ClassNotFoundException e) {
                     log.error("Class not found: " + bootstrapClassName,e);
@@ -120,6 +126,9 @@
      * @throws javax.jbi.JBIException if the installation fails.
      */
     public ObjectName install() throws JBIException {
+        if (installed) {
+            throw new DeploymentException("Component is already installed");
+        }
         if (bootstrap != null) {
             bootstrap.onInstall();
         }
@@ -169,12 +178,14 @@
      * @throws javax.jbi.JBIException if the uninstallation fails.
      */
     public void uninstall() throws javax.jbi.JBIException {
+        if (!installed) {
+            throw new DeploymentException("Component is already installed");
+        }
         if (bootstrap != null){
             bootstrap.onUninstall();
         }
-        if (isInstalled()) {
-            container.deactivateComponent(context.getComponentName());
-        }
+        container.deactivateComponent(context.getComponentName());
+        installed = false;
         if (bootstrap != null){
             bootstrap.cleanUp();
         }

Modified: trunk/servicemix-core/src/test/java/org/servicemix/jbi/installation/InstallationTest.java (1043 => 1044)

--- trunk/servicemix-core/src/test/java/org/servicemix/jbi/installation/InstallationTest.java	2005-12-07 15:07:45 UTC (rev 1043)
+++ trunk/servicemix-core/src/test/java/org/servicemix/jbi/installation/InstallationTest.java	2005-12-07 16:38:28 UTC (rev 1044)
@@ -370,4 +370,69 @@
         lifecycleMock.verify();
     }
 
+    /**
+     * Installer is created, component installed.
+     * Then we unload the installer and reload it.
+     * @throws Exception
+     */
+    public void testInstallAndReloadInstaller() throws Exception {
+        // Create mocks
+        ExtMockControl bootstrapMock = ExtMockControl.createControl(Bootstrap.class);
+        Bootstrap bootstrap = (Bootstrap) bootstrapMock.getMock();
+        Bootstrap1.setDelegate(bootstrap);
+        ExtMockControl componentMock = ExtMockControl.createControl(Component.class);
+        Component component = (Component) componentMock.getMock();
+        Component1.setDelegate(component);
+        
+        // configure bootstrap
+        bootstrapMock.reset();
+        bootstrap.init(null);
+        bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
+        bootstrap.onInstall();
+        bootstrap.cleanUp();
+        bootstrapMock.replay();
+        // configure component
+        componentMock.reset();
+        componentMock.replay();
+        // test component installation
+        startContainer(true);
+        String installJarUrl = createInstallerArchive("component1").getAbsolutePath();
+        ObjectName installerName = getInstallationService().loadNewInstaller(installJarUrl);
+        InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
+        assertFalse(installer.isInstalled());
+        ObjectName lifecycleName = installer.install();
+        LifeCycleMBean lifecycleMBean = (LifeCycleMBean)  MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
+        assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState());
+        // check mocks
+        bootstrapMock.verify();
+        componentMock.verify();
+        
+        // configure bootstrap
+        bootstrapMock.reset();
+        bootstrapMock.replay();
+        // configure component
+        componentMock.reset();
+        componentMock.replay();
+        // unload installer
+        container.getInstallationService().unloadInstaller("component1", false);
+        // check mocks
+        bootstrapMock.verify();
+        componentMock.verify();
+
+        // configure bootstrap
+        bootstrapMock.reset();
+        bootstrap.init(null);
+        bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
+        bootstrapMock.replay();
+        // configure component
+        componentMock.reset();
+        componentMock.replay();
+        // shutdown container
+        installerName = container.getInstallationService().loadInstaller("component1");
+        assertNotNull(installerName);
+        // check mocks
+        bootstrapMock.verify();
+        componentMock.verify();
+    }
+
 }

Reply via email to