DeployBMCTask.java 5.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.io.FileUtils
 *  org.apache.commons.io.IOUtils
 *  org.apache.sling.installer.api.tasks.InstallTask
 *  org.apache.sling.installer.api.tasks.InstallationContext
 *  org.apache.sling.installer.api.tasks.ResourceState
 *  org.apache.sling.installer.api.tasks.TaskResource
 *  org.apache.sling.installer.api.tasks.TaskResourceGroup
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.bedrock.installer.internal;

import com.adobe.aemds.bedrock.installer.internal.ArtifactType;
import com.adobe.aemds.bedrock.installer.internal.BMCDeployer;
import com.adobe.aemds.bedrock.installer.internal.Utilities;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.sling.installer.api.tasks.InstallTask;
import org.apache.sling.installer.api.tasks.InstallationContext;
import org.apache.sling.installer.api.tasks.ResourceState;
import org.apache.sling.installer.api.tasks.TaskResource;
import org.apache.sling.installer.api.tasks.TaskResourceGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DeployBMCTask
extends InstallTask {
    private static final String INSTALL_ORDER = "77-";
    private final Logger logger;
    private final BMCDeployer bmcDeployer;
    private final ArtifactType type;

    public DeployBMCTask(TaskResourceGroup erl, BMCDeployer bmcDeployer, ArtifactType type) {
        super(erl);
        this.logger = LoggerFactory.getLogger(this.getClass());
        this.bmcDeployer = bmcDeployer;
        this.type = type;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void execute(InstallationContext ctx) {
        block13 : {
            TaskResource tr = this.getResource();
            String serviceName = (String)tr.getAttribute("Bedrock-Service-Name");
            String osName = (String)tr.getAttribute("Bedrock-Operating-System");
            if (serviceName == null) {
                this.logger.error("Resource {} has no Bedrock Service name - ignoring.", (Object)tr);
                this.getResourceGroup().setFinishState(ResourceState.IGNORED);
                return;
            }
            if (osName != null) {
                boolean isSameOS;
                this.logger.info("Resource {} has specified OS {} which is " + ((isSameOS = this.bmcDeployer.isCurrentOS(osName)) ? "valid" : "not valid") + " for current OS " + (isSameOS ? "- installing." : "- ignoring."), (Object)tr, (Object)osName);
                if (!isSameOS) {
                    this.getResourceGroup().setFinishState(ResourceState.IGNORED);
                    return;
                }
            } else {
                this.logger.info("Resource {} does not have any OS specified.", (Object)tr);
            }
            String nativeDepsFileName = "com.adobe.service." + serviceName + ".nativedeps";
            String nativeDepsFileContents = (String)tr.getAttribute("Bedrock-Native-Dependencies");
            if (tr.getState() == ResourceState.INSTALL) {
                InputStream is = null;
                try {
                    is = tr.getInputStream();
                    if (is == null) {
                        this.logger.error("Resource {} does not provide an input stream!", (Object)tr);
                        this.getResourceGroup().setFinishState(ResourceState.IGNORED);
                        break block13;
                    }
                    this.bmcDeployer.installBMC(serviceName, is, this.type);
                    ctx.log("Installed BMC {} of type {}", new Object[]{serviceName, this.type});
                    this.getResourceGroup().setFinishState(ResourceState.INSTALLED);
                    this.exportNativeDependencies(this.bmcDeployer.getServiceDir(serviceName, ArtifactType.BMC_CONFIG), nativeDepsFileName, nativeDepsFileContents);
                }
                catch (IOException ioe) {
                    this.logger.error("Unable to install BMC " + serviceName + "from resource " + (Object)tr, (Throwable)ioe);
                    this.getResourceGroup().setFinishState(ResourceState.IGNORED);
                }
                finally {
                    IOUtils.closeQuietly((InputStream)is);
                }
            }
            this.bmcDeployer.uninstall(serviceName, this.type);
            this.getResourceGroup().setFinishState(ResourceState.UNINSTALLED);
            try {
                this.unexportNativeDependencies(this.bmcDeployer.getServiceDir(serviceName, ArtifactType.BMC_CONFIG), nativeDepsFileName);
            }
            catch (IOException ioe) {
                this.logger.warn("Unable to delete configuration data for " + serviceName + ": " + nativeDepsFileName, (Throwable)ioe);
            }
        }
    }

    public String getSortKey() {
        return "77-" + this.getResource().getURL();
    }

    private void exportNativeDependencies(File serviceDataDir, String fileName, String fileContents) throws IOException {
        if (!serviceDataDir.exists()) {
            FileUtils.forceMkdir((File)serviceDataDir);
        }
        File serviceConfigFile = new File(serviceDataDir, fileName);
        FileUtils.writeStringToFile((File)serviceConfigFile, (String)fileContents);
    }

    private void unexportNativeDependencies(File serviceDataDir, String fileName) throws IOException {
        File serviceConfigFile = new File(serviceDataDir, fileName);
        if (serviceConfigFile.exists()) {
            FileUtils.forceDelete((File)serviceConfigFile);
        }
        if (serviceDataDir.listFiles().length == 0) {
            Utilities.deleteRecursive(serviceDataDir);
        }
    }
}