BMCDeployer.java 5.06 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.bedrock.CoreConfigService
 *  com.adobe.aemds.bedrock.OS
 *  com.adobe.aemds.bedrock.internal.OSGiUtils
 *  com.adobe.aemds.bedrock.internal.Utilities
 *  com.adobe.service.ConnectionFactory
 *  javax.transaction.SystemException
 *  javax.transaction.Transaction
 *  org.apache.commons.io.FileUtils
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.bedrock.installer.internal;

import com.adobe.aemds.bedrock.CoreConfigService;
import com.adobe.aemds.bedrock.OS;
import com.adobe.aemds.bedrock.installer.internal.ArtifactType;
import com.adobe.aemds.bedrock.installer.internal.Utilities;
import com.adobe.aemds.bedrock.internal.OSGiUtils;
import com.adobe.service.ConnectionFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.naming.NameNotFoundException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BMCDeployer {
    private final Logger logger;
    private CoreConfigService configService;

    public BMCDeployer() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    public void installBMC(String serviceName, InputStream is, ArtifactType type) throws IOException {
        this.uninstall(serviceName, type);
        File dir = this.getServiceDir(serviceName, type);
        if (!dir.exists()) {
            FileUtils.forceMkdir((File)dir);
        }
        Utilities.unpackResources(is, dir, this.shouldSetSharedBit(type));
    }

    public void uninstall(String serviceName, ArtifactType type) {
        this.stopService(serviceName);
        File dir = this.getServiceDir(serviceName, type);
        if (dir.exists()) {
            Utilities.deleteRecursive(dir);
            this.logger.debug("Deleted directory {} upon uninstallation of service {}", (Object)dir, (Object)serviceName);
        }
    }

    public File getServiceDir(String serviceName, ArtifactType artifactType) {
        switch (artifactType) {
            case BMC_NATIVE: {
                return new File(this.configService.getServerNativeDir(), serviceName);
            }
            case BMC_CONFIG: {
                return new File(this.configService.getServerDataDir(), serviceName);
            }
        }
        throw new IllegalArgumentException("ArtifactType " + (Object)((Object)artifactType) + " is not supported");
    }

    public boolean isCurrentOS(String osName) {
        return OS.fromName((String)osName) == OS.getCurrent();
    }

    public void setConfigService(CoreConfigService configService) {
        this.configService = configService;
    }

    private void stopService(String serviceName) {
        this.logger.info("Stopping service {}", (Object)serviceName);
        ConnectionFactory cf = null;
        try {
            cf = com.adobe.aemds.bedrock.internal.Utilities.serviceLookup((String)serviceName);
        }
        catch (NameNotFoundException nnfe) {
            this.logger.info("Service {} not registered, nothing to stop.", (Object)serviceName);
            this.logger.debug("Unable to look up connection factory for service {}, any running processes might not be terminated during uninstallation. Caused by:", (Object)serviceName, (Object)nnfe);
        }
        if (cf != null) {
            this.logger.info("Found connection factory {} for service {}, shutting down any running processes", (Object)cf, (Object)serviceName);
            Transaction tx = null;
            try {
                tx = OSGiUtils.getTransactionManager().getTransaction();
                if (tx == null) {
                    OSGiUtils.getTransactionManager().begin();
                }
                try {
                    cf.resetProcesses();
                }
                catch (AssertionError ae) {
                    this.logger.debug("Ignoring an assertion error thrown by resetProcesses() while stopping service {}", (Object)serviceName, (Object)ae);
                }
                OSGiUtils.getTransactionManager().commit();
            }
            catch (Exception e) {
                this.logger.warn("An exception occurred while stopping service {}, any running processes might not have been terminated", (Object)serviceName, (Object)e);
                try {
                    if (tx != null) {
                        OSGiUtils.getTransactionManager().rollback();
                    }
                }
                catch (SystemException se) {
                    this.logger.debug("An exception occurred during transaction rollback while stopping service {}, any running processes might not have been terminated", (Object)serviceName, (Object)se);
                }
            }
        }
    }

    private boolean isHP() {
        return System.getProperty("os.name").toLowerCase().startsWith("hp");
    }

    private boolean shouldSetSharedBit(ArtifactType type) {
        if (ArtifactType.BMC_NATIVE == type) {
            return this.isHP();
        }
        return false;
    }

}