BedrockInstaller.java 9.41 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.bedrock.CoreConfigService
 *  org.apache.commons.io.IOUtils
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.installer.api.tasks.InstallTask
 *  org.apache.sling.installer.api.tasks.InstallTaskFactory
 *  org.apache.sling.installer.api.tasks.RegisteredResource
 *  org.apache.sling.installer.api.tasks.ResourceTransformer
 *  org.apache.sling.installer.api.tasks.TaskResource
 *  org.apache.sling.installer.api.tasks.TaskResourceGroup
 *  org.apache.sling.installer.api.tasks.TransformationResult
 *  org.apache.sling.settings.SlingSettingsService
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.bedrock.installer.internal;

import com.adobe.aemds.bedrock.CoreConfigService;
import com.adobe.aemds.bedrock.installer.internal.ArtifactType;
import com.adobe.aemds.bedrock.installer.internal.BMCDeployer;
import com.adobe.aemds.bedrock.installer.internal.DeployBMCTask;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.installer.api.tasks.InstallTask;
import org.apache.sling.installer.api.tasks.InstallTaskFactory;
import org.apache.sling.installer.api.tasks.RegisteredResource;
import org.apache.sling.installer.api.tasks.ResourceTransformer;
import org.apache.sling.installer.api.tasks.TaskResource;
import org.apache.sling.installer.api.tasks.TaskResourceGroup;
import org.apache.sling.installer.api.tasks.TransformationResult;
import org.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class BedrockInstaller
implements ResourceTransformer,
InstallTaskFactory {
    public static final String AEMDS_ARTIFACT_TYPE = "AEMDS-Artifact-Type";
    public static final String AEMDS_ARTIFACT_NAME = "AEMDS-Artifact-Name";
    public static final String BEDROCK_SERVICE_NAME = "Bedrock-Service-Name";
    public static final String BEDROCK_OS_NAME = "Bedrock-Operating-System";
    public static final String BEDROCK_NATIVE_DEPENDENCIES = "Bedrock-Native-Dependencies";
    public static final String BUNDLE_VERSION = "Bundle-Version";
    private final Logger logger;
    private BMCDeployer bmcDeployer;
    private static final Pattern VERSION_PATTERN = Pattern.compile("\\d+\\.\\d+\\.\\d+(-SNAPSHOT)?");
    @Reference
    private CoreConfigService coreConfigService;
    @Reference
    private SlingSettingsService settingsService;

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

    public TransformationResult[] transform(RegisteredResource resource) {
        if (resource.getType().equals("file")) {
            try {
                Manifest m = this.getManifest(resource.getInputStream());
                if (m != null) {
                    String artifactTypeCode = m.getMainAttributes().getValue("AEMDS-Artifact-Type");
                    String serviceName = m.getMainAttributes().getValue("Bedrock-Service-Name");
                    if (artifactTypeCode == null && serviceName != null) {
                        artifactTypeCode = ArtifactType.BMC_NATIVE.getTypeCode();
                    }
                    if (artifactTypeCode == null) {
                        return null;
                    }
                    ArtifactType artifactType = ArtifactType.fromCode(artifactTypeCode);
                    if (artifactType == null) {
                        return null;
                    }
                    TransformationResult tr = null;
                    switch (artifactType) {
                        case BMC_NATIVE: 
                        case BMC_CONFIG: {
                            tr = this.transformAsBMC(resource, m, artifactType);
                        }
                    }
                    if (tr != null) {
                        return new TransformationResult[]{tr};
                    }
                }
            }
            catch (IOException ignore) {
                this.logger.warn("Error encountered during transformation of installable resource " + (Object)resource, (Throwable)ignore);
            }
        }
        return null;
    }

    private String sniffVersionFromArtifactFileName(String path) {
        if (path == null) {
            return null;
        }
        Matcher m = VERSION_PATTERN.matcher(path);
        if (m.find()) {
            String gr = m.group(0);
            if (gr == null || gr.length() == 0) {
                return null;
            }
            gr = gr.replace('-', '.');
            return gr;
        }
        return null;
    }

    private String getVersion(RegisteredResource r, Manifest m) {
        String result = m.getMainAttributes().getValue("Bundle-Version");
        if (result == null) {
            result = this.sniffVersionFromArtifactFileName(r.getURL());
        }
        return result;
    }

    private TransformationResult transformAsBMC(RegisteredResource resource, Manifest m, ArtifactType type) {
        String serviceName = m.getMainAttributes().getValue("Bedrock-Service-Name");
        if (serviceName == null) {
            serviceName = m.getMainAttributes().getValue("AEMDS-Artifact-Name");
        }
        if (serviceName != null) {
            String nativeDependencies;
            String osName = m.getMainAttributes().getValue("Bedrock-Operating-System");
            if (osName == null && type == ArtifactType.BMC_NATIVE) {
                this.logger.warn("OS Name(Bedrock-Operating-System) not specified for resource {} with Service Name {}", (Object)resource, (Object)serviceName);
            }
            if ((nativeDependencies = m.getMainAttributes().getValue("Bedrock-Native-Dependencies")) == null && type == ArtifactType.BMC_NATIVE) {
                this.logger.warn("Mandatory attribute 'Bedrock-Native-Dependencies' not specified for resource {} with Service Name {}", (Object)resource, (Object)serviceName);
            }
            HashMap<String, String> attr = new HashMap<String, String>();
            attr.put("AEMDS-Artifact-Name", serviceName);
            attr.put("Bedrock-Service-Name", serviceName);
            attr.put("Bedrock-Operating-System", osName);
            attr.put("Bedrock-Native-Dependencies", nativeDependencies == null ? "" : nativeDependencies);
            String version = this.getVersion(resource, m);
            if (version != null) {
                attr.put("Bundle-Version", version);
            }
            TransformationResult tr = new TransformationResult();
            String id = serviceName + "-" + osName;
            if (type == ArtifactType.BMC_CONFIG) {
                id = serviceName + "-" + type.getTypeCode();
            }
            tr.setId(id);
            tr.setResourceType(type.getTypeCode());
            tr.setAttributes(attr);
            return tr;
        }
        return null;
    }

    public InstallTask createTask(TaskResourceGroup toActivate) {
        String typeCode = toActivate.getActiveResource().getType();
        ArtifactType type = ArtifactType.fromCode(typeCode);
        if (ArtifactType.BMC_NATIVE == type || ArtifactType.BMC_CONFIG == type) {
            return new DeployBMCTask(toActivate, this.bmcDeployer, type);
        }
        return null;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private Manifest getManifest(InputStream ins) throws IOException {
        Manifest result = null;
        if (ins != null) {
            JarInputStream jis = null;
            try {
                jis = new JarInputStream(ins);
                result = jis.getManifest();
            }
            finally {
                if (jis != null) {
                    IOUtils.closeQuietly((InputStream)jis);
                } else {
                    IOUtils.closeQuietly((InputStream)ins);
                }
            }
        }
        return result;
    }

    @Activate
    private void activate() {
        this.bmcDeployer.setConfigService(this.coreConfigService);
    }

    @Deactivate
    private void deactivate() {
        this.bmcDeployer.setConfigService(null);
    }

    protected void bindCoreConfigService(CoreConfigService coreConfigService) {
        this.coreConfigService = coreConfigService;
    }

    protected void unbindCoreConfigService(CoreConfigService coreConfigService) {
        if (this.coreConfigService == coreConfigService) {
            this.coreConfigService = null;
        }
    }

    protected void bindSettingsService(SlingSettingsService slingSettingsService) {
        this.settingsService = slingSettingsService;
    }

    protected void unbindSettingsService(SlingSettingsService slingSettingsService) {
        if (this.settingsService == slingSettingsService) {
            this.settingsService = null;
        }
    }

}