Cq53StartupCodeUpgrade.java 7.24 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.codeupgrade.impl.cq53;

import com.day.cq.compat.codeupgrade.CodeUpgradeTask;
import com.day.cq.compat.codeupgrade.impl.StatusNode;
import com.day.cq.compat.codeupgrade.impl.UpgradeNodetypes;
import com.day.cq.compat.codeupgrade.impl.cq53.UpgradeDamAssetContent;
import com.day.cq.compat.codeupgrade.internal.api.CodeUpgrader;
import com.day.cq.compat.codeupgrade.internal.api.CodeUpgraderFactory;
import com.day.cq.compat.codeupgrade.internal.api.NodeBackup;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.io.Writer;
import java.util.Dictionary;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class Cq53StartupCodeUpgrade
implements CodeUpgradeTask {
    @Property(value={"/apps", "/etc/designs", "/etc/replication", "/etc/security", "/var/linkchecker", "/etc/workflow"})
    public static final String PROP_PATHS_TO_UPGRADE = "paths.to.upgrade";
    public static final String DEFAULT_CONFIG_PATH = "/libs/cq/compat/components/codeupgrade/5.3";
    @Property(value={"/libs/cq/compat/components/codeupgrade/5.3"})
    public static final String PROP_CONFIG_PATH = "upgrade.configuration.path";
    public static final int DEFAULT_MAX_FILE_SIZE = 100000;
    @Property(intValue={100000})
    public static final String PROP_MAX_FILE_SIZE = "max.file.size";
    public static final String DEFAULT_FILE_ACCEPT_REGEXP = "^[a-zA-Z0-9_].*\\.(jsp|jspf|jspx|esp|jst|ecma|js)$";
    @Property(value={"^[a-zA-Z0-9_].*\\.(jsp|jspf|jspx|esp|jst|ecma|js)$"})
    public static final String PROP_FILE_ACCEPT_REGEXP = "file.accept.regexp";
    @Property(value={"/var/upgrade/status"})
    public static final String PROP_STATUS_NODE_PATH = "status.node.path";
    @Property(value={"Cq53StartupCodeUpgrade"})
    public static final String PROP_STATUS_PROPERTY_NAME = "status.property.name";
    private final Logger log;
    private String[] pathsToUpgrade;
    private String configPath;
    private int maxFilesSize;
    private String fileAcceptRegexp;
    private ProgressInfoProvider infoProvider;
    private String progressInfo;
    @Reference
    private SlingRepository repository;
    @Reference
    private CodeUpgraderFactory codeUpgraderFactory;
    private CodeUpgrader upgrader;
    private ComponentContext componentContext;
    private StatusNode statusNode;

    Cq53StartupCodeUpgrade() {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.progressInfo = "No info yet";
    }

    public String toString() {
        return this.getClass().getSimpleName();
    }

    public void activate(ComponentContext ctx) throws Exception {
        this.componentContext = ctx;
    }

    public void deactivate(ComponentContext ctx) throws InterruptedException {
        this.componentContext = null;
        if (this.upgrader != null) {
            this.upgrader.stop();
        }
        this.upgrader = null;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void run() {
        long start = System.currentTimeMillis();
        Session s = null;
        try {
            s = this.repository.loginService("compat-codeupgrade", null);
            this.doUpgrade(s);
            this.statusNode.recordUpgrade(this.repository);
        }
        catch (Exception e) {
            this.log.error("Code upgrade error", (Throwable)e);
        }
        finally {
            if (s != null) {
                s.logout();
            }
        }
        this.log.info("Code upgrade done, took {} msec", (Object)(System.currentTimeMillis() - start));
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void doUpgrade(Session s) throws Exception {
        this.progressInfo = "Starting upgrade";
        if (this.pathsToUpgrade == null) {
            throw new IllegalStateException("pathsToUpgrade is null");
        }
        NodeBackup b = this.codeUpgraderFactory.createNodeBackup();
        for (String path : this.pathsToUpgrade) {
            long start = System.currentTimeMillis();
            this.upgrader = this.codeUpgraderFactory.createUpgrader(s, this.configPath);
            Cq53StartupCodeUpgrade cq53StartupCodeUpgrade = this;
            synchronized (cq53StartupCodeUpgrade) {
                this.infoProvider = this.upgrader;
            }
            this.upgrader.setMaxFileSize(this.maxFilesSize);
            this.upgrader.setFileAcceptRegexp(this.fileAcceptRegexp);
            this.log.info("Upgrader configured, max file size={}, file accept regexp={}", (Object)this.upgrader.getMaxFileSize(), (Object)this.upgrader.getFileAcceptRegexp());
            this.upgrader.upgrade(s, path, b, null);
            this.log.info("Done upgrading path {} (took {} msec)", (Object)path, (Object)(System.currentTimeMillis() - start));
        }
        Object udac = new UpgradeDamAssetContent();
        Cq53StartupCodeUpgrade len$ = this;
        synchronized (len$) {
            this.infoProvider = udac;
        }
        udac.doUpgrade(s);
        udac = this;
        synchronized (udac) {
            this.infoProvider = null;
        }
        this.progressInfo = "Upgrading node types";
        new UpgradeNodetypes().doUpgrade(s, this.configPath + "/nodetypes.cnd");
        this.progressInfo = "All done";
    }

    @Override
    public void cancelUpgrade() {
        if (this.upgrader != null) {
            this.log.info("Calling upgrader.stop() to cancel upgrade...");
            this.upgrader.stop();
        }
    }

    @Override
    public synchronized String getProgressInfo() {
        if (this.infoProvider == null) {
            return this.progressInfo;
        }
        return this.infoProvider.getProgressInfo();
    }

    @Override
    public boolean upgradeNeeded() {
        boolean upgradeNeeded = false;
        Dictionary properties = this.componentContext.getProperties();
        this.statusNode = new StatusNode((String)properties.get("status.node.path"), (String)properties.get("status.property.name"));
        upgradeNeeded = this.statusNode.upgradeNeeded(this.repository);
        if (upgradeNeeded) {
            this.pathsToUpgrade = PropertiesUtil.toStringArray(properties.get("paths.to.upgrade"));
            this.configPath = PropertiesUtil.toString(properties.get("upgrade.configuration.path"), (String)"/libs/cq/compat/components/codeupgrade/5.3");
            this.maxFilesSize = PropertiesUtil.toInteger(properties.get("max.file.size"), (int)100000);
            this.fileAcceptRegexp = PropertiesUtil.toString(properties.get("file.accept.regexp"), (String)"^[a-zA-Z0-9_].*\\.(jsp|jspf|jspx|esp|jst|ecma|js)$");
        }
        return upgradeNeeded;
    }
}