String resultMsg = sum.deploy(suName, targetDir.getAbsolutePath()); success = getComponentTaskResult(resultMsg, componentName, componentResults, true);
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); }
    protected String deployServiceAssembly(File tmpDir, ServiceAssembly sa) throws Exception {        String assemblyName = sa.getIdentification().getName();        File oldSaDirectory = environmentContext.getSARootDirectory(assemblyName);        FileUtil.deleteFile(oldSaDirectory);        File saDirectory = environmentContext.createSARootDirectory(assemblyName);        // move the assembly to a well-named holding area        if (log.isDebugEnabled()) {            log.debug("Moving " + tmpDir.getAbsolutePath() + " to " + saDirectory.getAbsolutePath());        }        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) {            for (int i = 0; i < sus.length; i++) {                String suName = sus[i].getIdentification().getName();                String artifact = sus[i].getTarget().getArtifactsZip();                String componentName = sus[i].getTarget().getComponentName();                File artifactFile = new File(saDirectory, artifact);                if (!artifactFile.exists()) {                    throw ManagementSupport.failure("deploy", "Artifact " + artifact + " not found for service unit " + suName);                }                ComponentMBeanImpl lcc = container.getComponent(componentName);                if (lcc == null) {                    throw ManagementSupport.failure("deploy", "Target component " + componentName + " for service unit " + suName + " is not installed");                }                if (!lcc.isStarted()) {                    throw ManagementSupport.failure("deploy", "Target component " + componentName + " for service unit " + suName + " is not started");                }                if (lcc.getServiceUnitManager() == null) {                    throw ManagementSupport.failure("deploy", "Target component " + componentName + " for service unit " + suName + " does not accept deployments");                }                // TODO: check duplicates here ?                if (isDeployedServiceUnit(componentName, suName)) {                    throw ManagementSupport.failure("deploy", "Service unit " + suName + " is already deployed on component " + componentName);                }            }        }        // Everything seems ok, so deploy all SUs        int nbSuccess = 0;        int nbFailures = 0;        List componentResults = new ArrayList();        List suKeys = new ArrayList();        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 = environmentContext.getServiceUnitDirectory(componentName, suName, assemblyName);                    if (log.isDebugEnabled()) {                        log.debug("Unpack service unit archive " + artifactFile + " to " + 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();                    String resultMsg = sum.deploy(suName, targetDir.getAbsolutePath());                    success = getComponentTaskResult(resultMsg, componentName, componentResults, true);                    // TODO: need to register the SU somewhere to keep track of its state                } catch (Exception e) {                    // Delete SU deployment dir                    FileUtil.deleteFile(targetDir);                    getComponentTaskResult(e.getMessage(), componentName, componentResults, false);                }                if (success) {                    nbSuccess++;                    suKeys.add(registry.registerServiceUnit(sus[i], assemblyName));                } else {                    nbFailures++;                }            }        }        // Total failure        if (nbSuccess == 0 && nbFailures > 0) {            // Delete SA deployment directory             FileUtil.deleteFile(tmpDir);            throw ManagementSupport.failure("deploy", componentResults);        }        // Success        else {            // Register SA            String[] deployedSUs = (String[]) suKeys.toArray(new String[suKeys.size()]);            ServiceAssemblyLifeCycle salc = registry.registerServiceAssembly(sa, deployedSUs);            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);            }        }    }