CqMergeBackupUpgrade.java 4.57 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.upgrades.backup.UpgradeBackupMerger
 *  com.adobe.cq.upgrades.backup.UpgradeBackupMerger$ProgressInfo
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.codeupgrade.impl.mergebackup;

import com.adobe.cq.upgrades.backup.UpgradeBackupMerger;
import com.day.cq.compat.codeupgrade.CodeUpgradeTask;
import com.day.cq.compat.codeupgrade.impl.StatusNode;
import java.util.Dictionary;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1, metatype=1, label="CqMergeBackupUpgrade", description="Restores content saved by pre-upgrade backup")
@Service
@Property(name="service.ranking", intValue={10000})
public class CqMergeBackupUpgrade
implements CodeUpgradeTask,
UpgradeBackupMerger.ProgressInfo {
    private final Logger log;
    private StatusNode statusNode;
    private String progressInfo;
    @Reference
    private SlingRepository repository;
    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
    private volatile UpgradeBackupMerger upgradeBackupMerger;
    @Property(value={"/var/upgrade/status"})
    public static final String PROP_STATUS_NODE_PATH = "status.node.path";
    @Property(value={"Cq62MergeBackupUpgrade"})
    public static final String PROP_STATUS_PROPERTY_NAME = "status.property.name";

    public CqMergeBackupUpgrade() {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.progressInfo = "No progress info yet";
        this.repository = null;
    }

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

    public void activate(ComponentContext ctx) throws Exception {
        Dictionary properties = ctx.getProperties();
        this.statusNode = new StatusNode((String)properties.get("status.node.path"), (String)properties.get("status.property.name"));
    }

    public void info(String msg) {
        this.progressInfo = msg;
    }

    @Override
    public void cancelUpgrade() {
    }

    @Override
    public synchronized String getProgressInfo() {
        return this.progressInfo;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void run() {
        Session s = null;
        try {
            if (this.upgradeBackupMerger == null) {
                this.log.error("No UpgradeBackupMerger available, cannot run");
            } else {
                s = this.repository.loginService("compat-codeupgrade", null);
                this.upgradeBackupMerger.merge(s, (UpgradeBackupMerger.ProgressInfo)this);
            }
            this.statusNode.recordUpgrade(this.repository);
        }
        catch (Exception e) {
            this.log.warn("Exception in " + this.getClass().getSimpleName() + ".run()", (Throwable)e);
        }
        finally {
            if (s != null) {
                s.logout();
            }
            this.progressInfo = "All done.";
        }
    }

    @Override
    public boolean upgradeNeeded() {
        return this.statusNode.upgradeNeeded(this.repository);
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

    protected void bindUpgradeBackupMerger(UpgradeBackupMerger upgradeBackupMerger) {
        this.upgradeBackupMerger = upgradeBackupMerger;
    }

    protected void unbindUpgradeBackupMerger(UpgradeBackupMerger upgradeBackupMerger) {
        if (this.upgradeBackupMerger == upgradeBackupMerger) {
            this.upgradeBackupMerger = null;
        }
    }
}