InstallLibsTask.java 4.19 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.apache.sling.settings.SlingSettingsService
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.core.libinstaller.internal;

import com.adobe.fd.core.libinstaller.internal.JarSupport;
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.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InstallLibsTask
extends InstallTask {
    private static final String LIBS_DIR = "launchpad/ext";
    private static final String INSTALL_ORDER = "999-";
    private static final Logger logger = LoggerFactory.getLogger(InstallLibsTask.class);
    private SlingSettingsService slingSettingsService;

    public InstallLibsTask(TaskResourceGroup erl, SlingSettingsService slingSettingsService) {
        super(erl);
        this.slingSettingsService = slingSettingsService;
        try {
            InstallLibsTask.class.getClassLoader().loadClass(JarSupport.class.getName());
        }
        catch (ClassNotFoundException e) {
            logger.warn("Error while loading class ", (Throwable)e);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void execute(InstallationContext ctx) {
        block7 : {
            TaskResource tr = this.getResource();
            if (tr.getState() == ResourceState.INSTALL) {
                InputStream is = null;
                try {
                    is = tr.getInputStream();
                    if (is == null) {
                        logger.error("Resource {} does not provide an input stream!", (Object)tr);
                        this.getResourceGroup().setFinishState(ResourceState.IGNORED);
                        break block7;
                    }
                    File libsDir = this.getLibsDir();
                    this.unpackResources(is, libsDir);
                    ctx.log("Installed libs at  {}", new Object[]{libsDir});
                    logger.debug("Installed libs at  {}", (Object)libsDir);
                    this.getResourceGroup().setFinishState(ResourceState.INSTALLED);
                }
                catch (Exception e) {
                    logger.error("Unable to install libs from resource " + (Object)tr, (Throwable)e);
                    this.getResourceGroup().setFinishState(ResourceState.IGNORED);
                }
                finally {
                    IOUtils.closeQuietly((InputStream)is);
                }
            }
            this.getResourceGroup().setFinishState(ResourceState.UNINSTALLED);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void unpackResources(InputStream is, File libsDir) throws IOException {
        JarSupport inputJar = null;
        try {
            inputJar = new JarSupport(is);
            inputJar.extractAll(libsDir);
            inputJar.close();
        }
        finally {
            if (inputJar != null) {
                inputJar.close();
            }
        }
    }

    private File getLibsDir() throws IOException {
        String slingHome = this.slingSettingsService.getSlingHomePath();
        File libsDir = new File(slingHome, "launchpad/ext");
        if (!libsDir.exists()) {
            FileUtils.forceMkdir((File)libsDir);
        }
        return libsDir;
    }

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