protected String deployServiceAssembly()

in core/servicemix-core/src/main/java/org/apache/servicemix/jbi/framework/DeploymentService.java [495:592]


    protected String deployServiceAssembly(File tmpDir, ServiceAssembly sa) throws Exception {
        String assemblyName = sa.getIdentification().getName();
        ServiceAssemblyEnvironment env = environmentContext.getNewServiceAssemblyEnvironment(assemblyName);
        File saDirectory = env.getInstallDir();

        // move the assembly to a well-named holding area
        LOGGER.debug("Moving {} to {}", tmpDir.getAbsolutePath(), saDirectory.getAbsolutePath());
        saDirectory.getParentFile().mkdirs();
        if (!tmpDir.renameTo(saDirectory)) {
            throw ManagementSupport.failure("deploy", "Failed to rename " + tmpDir + " to " + saDirectory);
        }
        // Check all SUs requirements
        ServiceUnit[] sus = sa.getServiceUnits();
        if (sus != null) {
            checkSus(saDirectory, sus);
        }
        // Everything seems ok, so deploy all SUs
        int nbFailures = 0;
        List<Element> componentResults = new ArrayList<Element>();
        List<String> suKeys = new ArrayList<String>();
        if (sus != null) {
            for (int i = 0; i < sus.length; i++) {
                File targetDir = null;
                String suName = sus[i].getIdentification().getName();
                String artifact = sus[i].getTarget().getArtifactsZip();
                String componentName = sus[i].getTarget().getComponentName();
                // TODO: skip duplicates
                // Unpack SU
                try {
                    File artifactFile = new File(saDirectory, artifact);
                    targetDir = env.getServiceUnitDirectory(componentName, suName);
                    LOGGER.debug("Unpack service unit archive {} to {}", artifactFile, targetDir);
                    FileUtil.unpackArchive(artifactFile, targetDir);
                } catch (IOException e) {
                    nbFailures++;
                    componentResults.add(ManagementSupport.createComponentFailure(
                            "deploy", componentName,
                            "Error unpacking service unit", e));
                    continue;
                }
                // Deploy it
                boolean success = false;
                try {
                    ComponentMBeanImpl lcc = container.getComponent(componentName);
                    ServiceUnitManager sum = lcc.getServiceUnitManager();
                    ClassLoader cl = Thread.currentThread().getContextClassLoader();
                    try {
                        Thread.currentThread().setContextClassLoader(lcc.getComponent().getClass().getClassLoader());
                        String resultMsg = sum.deploy(suName, targetDir.getAbsolutePath());
                        success = getComponentTaskResult(resultMsg, componentName, componentResults, true);
                    } finally {
                        Thread.currentThread().setContextClassLoader(cl);
                    }
                    // TODO: need to register the SU somewhere to keep track of its state
                } catch (Exception e) {
                    getComponentTaskError(e, componentName, componentResults);
                }
                if (success) {
                    suKeys.add(registry.registerServiceUnit(sus[i], assemblyName, targetDir));
                } else {
                    nbFailures++;
                }
            }
        }
        // Note: the jbi spec says that if at least one deployment succeeds, 
        // this should be a SUCCESS.  However, ServiceMix handles SA in an
        // atomic way: for a given operation on an SA, all operations on SU
        // should succeed.  This is clearly a minor violation of the spec.
        //
        // Failure
        if (nbFailures > 0) {
            // Undeploy SUs
            for (Iterator<String> iter = suKeys.iterator(); iter.hasNext();) {
                try {
                    String suName = iter.next();
                    ServiceUnitLifeCycle su = registry.getServiceUnit(suName);
                    undeployServiceUnit(su);
                } catch (Exception e) {
                    LOGGER.warn("Error undeploying SU", e);
                }
            }
            // Delete SA deployment directory 
            FileUtil.deleteFile(saDirectory);
            throw ManagementSupport.failure("deploy", componentResults);
        // Success
        } else {
            // Register SA
            String[] deployedSUs = suKeys.toArray(new String[suKeys.size()]);
            ServiceAssemblyLifeCycle salc = registry.registerServiceAssembly(sa, deployedSUs, env);
            salc.writeRunningState();
            // Build result string
            if (nbFailures > 0) {
                return ManagementSupport.createWarningMessage("deploy", "Failed to deploy some service units", componentResults);
            } else {
                return ManagementSupport.createSuccessMessage("deploy", componentResults);
            }
        }
    }