Cq62StartupCodeUpgrade.java 12.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserManagementService
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.codeupgrade.impl.cq62;

import com.adobe.granite.security.user.UserManagementService;
import com.day.cq.compat.codeupgrade.CodeUpgradeTask;
import com.day.cq.compat.codeupgrade.impl.StatusNode;
import com.day.cq.compat.codeupgrade.impl.cq62.CQ62Html5SmartFileUpgrade;
import com.day.cq.compat.codeupgrade.impl.cq62.CQ62SearchFacetsConfigUpdate;
import com.day.cq.compat.codeupgrade.impl.cq62.Cq62ConfContentUpgrade;
import com.day.cq.compat.codeupgrade.impl.cq62.Cq62FormsContentUpgrade;
import com.day.cq.compat.codeupgrade.impl.cq62.Cq62MediaContentUpgrade;
import com.day.cq.compat.codeupgrade.impl.cq62.Cq62MobileContentUpgrade;
import com.day.cq.compat.codeupgrade.impl.cq62.Cq62PrimaryComponentsUpgrade;
import com.day.cq.compat.codeupgrade.impl.cq62.Cq62ProjectsCodeUpgrade;
import com.day.cq.compat.codeupgrade.impl.cq62.Cq62TargetCampaignsContentUpgrade;
import com.day.cq.compat.codeupgrade.impl.cq62.Cq62TargetContentUpgrade;
import com.day.cq.compat.codeupgrade.impl.cq62.Cq62WorkflowContentUpgrade;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
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.Service;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
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="Adobe CQ 6.2 upgrader", description="Runs the CodeUpgrader service the first time CQ 6.2 starts.")
@Service
@Property(name="service.ranking", intValue={620})
public class Cq62StartupCodeUpgrade
implements CodeUpgradeTask {
    private final Logger log;
    private StatusNode statusNode;
    private ProgressInfoProvider infoProvider;
    @Reference
    private SlingRepository repository;
    @Reference
    private UserManagementService userManagerService;
    @Reference
    private ResourceResolverFactory resourceResolverFactory;
    @Property(value={"/var/upgrade/status"})
    public static final String PROP_STATUS_NODE_PATH = "status.node.path";
    @Property(value={"Cq62StartupCodeUpgrade"})
    public static final String PROP_STATUS_PROPERTY_NAME = "status.property.name";

    public Cq62StartupCodeUpgrade() {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.repository = null;
        this.userManagerService = null;
        this.resourceResolverFactory = null;
    }

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

    @Activate
    protected 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"));
    }

    @Override
    public void cancelUpgrade() {
    }

    @Override
    public synchronized String getProgressInfo() {
        if (this.infoProvider == null) {
            return "No info from " + this.getClass().getName();
        }
        return this.infoProvider.getProgressInfo();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void run() {
        Session session = null;
        try {
            Cq62StartupCodeUpgrade cq62StartupCodeUpgrade;
            Object pcu;
            session = this.repository.loginService("compat-codeupgrade", null);
            try {
                Cq62WorkflowContentUpgrade wcu = new Cq62WorkflowContentUpgrade(this.userManagerService);
                cq62StartupCodeUpgrade = this;
                synchronized (cq62StartupCodeUpgrade) {
                    this.infoProvider = wcu;
                }
                wcu.doUpgrade(session);
            }
            catch (Exception e) {
                this.log.warn("Exception trying to execute " + Cq62WorkflowContentUpgrade.class.getSimpleName() + ".doUpgrade()", (Throwable)e);
                this.discardPendingChanges(session);
            }
            try {
                Cq62TargetContentUpgrade wcu = new Cq62TargetContentUpgrade();
                cq62StartupCodeUpgrade = this;
                synchronized (cq62StartupCodeUpgrade) {
                    this.infoProvider = wcu;
                }
                wcu.doUpgrade(session);
            }
            catch (Exception e) {
                this.log.warn("Exception trying to execute " + Cq62TargetContentUpgrade.class.getSimpleName() + ".doUpgrade()", (Throwable)e);
                this.discardPendingChanges(session);
            }
            try {
                Cq62TargetCampaignsContentUpgrade wcu = new Cq62TargetCampaignsContentUpgrade();
                cq62StartupCodeUpgrade = this;
                synchronized (cq62StartupCodeUpgrade) {
                    this.infoProvider = wcu;
                }
                wcu.doUpgrade(session);
            }
            catch (Exception e) {
                this.log.warn("Exception trying to execute " + Cq62TargetCampaignsContentUpgrade.class.getSimpleName() + ".doUpgrade()", (Throwable)e);
                this.discardPendingChanges(session);
            }
            try {
                Cq62MediaContentUpgrade mcu = new Cq62MediaContentUpgrade();
                cq62StartupCodeUpgrade = this;
                synchronized (cq62StartupCodeUpgrade) {
                    this.infoProvider = mcu;
                }
                mcu.doUpgrade(session);
            }
            catch (Exception e) {
                this.log.warn("Exception trying to execute " + Cq62MediaContentUpgrade.class.getSimpleName() + ".doUpgrade()", (Throwable)e);
                this.discardPendingChanges(session);
            }
            try {
                Cq62MobileContentUpgrade mobcu = new Cq62MobileContentUpgrade();
                cq62StartupCodeUpgrade = this;
                synchronized (cq62StartupCodeUpgrade) {
                    this.infoProvider = mobcu;
                }
                mobcu.doUpgrade(session);
            }
            catch (Exception e) {
                this.log.warn("Exception trying to execute " + Cq62MobileContentUpgrade.class.getSimpleName() + ".doUpgrade()", (Throwable)e);
                this.discardPendingChanges(session);
            }
            try {
                Cq62PrimaryComponentsUpgrade wcu = new Cq62PrimaryComponentsUpgrade();
                cq62StartupCodeUpgrade = this;
                synchronized (cq62StartupCodeUpgrade) {
                    this.infoProvider = wcu;
                }
                wcu.doUpgrade(session);
            }
            catch (Exception e) {
                this.log.warn("Exception trying to execute " + Cq62PrimaryComponentsUpgrade.class.getSimpleName() + ".doUpgrade()", (Throwable)e);
                this.discardPendingChanges(session);
            }
            try {
                CQ62Html5SmartFileUpgrade wcu = new CQ62Html5SmartFileUpgrade();
                cq62StartupCodeUpgrade = this;
                synchronized (cq62StartupCodeUpgrade) {
                    this.infoProvider = wcu;
                }
                wcu.doUpgrade(session);
            }
            catch (Exception e) {
                this.log.warn("Exception trying to execute " + CQ62Html5SmartFileUpgrade.class.getSimpleName() + ".doUpgrade()", (Throwable)e);
                this.discardPendingChanges(session);
            }
            try {
                ResourceResolver resolver = this.resourceResolverFactory.getResourceResolver(Collections.singletonMap("user.jcr.session", session));
                pcu = new Cq62ProjectsCodeUpgrade();
                Cq62StartupCodeUpgrade cq62StartupCodeUpgrade2 = this;
                synchronized (cq62StartupCodeUpgrade2) {
                    this.infoProvider = pcu;
                }
                pcu.doUpgrade(session, resolver);
            }
            catch (Exception e) {
                this.log.warn("Exception trying to execute {}.doUpgrade()", (Object)Cq62ProjectsCodeUpgrade.class.getSimpleName(), (Object)e);
                this.discardPendingChanges(session);
            }
            try {
                CQ62SearchFacetsConfigUpdate wcu = new CQ62SearchFacetsConfigUpdate();
                pcu = this;
                synchronized (pcu) {
                    this.infoProvider = wcu;
                }
                wcu.doUpgrade(session);
            }
            catch (Exception e) {
                this.log.warn("Exception trying to execute " + CQ62SearchFacetsConfigUpdate.class.getSimpleName() + ".doUpgrade()", (Throwable)e);
                this.discardPendingChanges(session);
            }
            try {
                Cq62ConfContentUpgrade ccu = new Cq62ConfContentUpgrade();
                pcu = this;
                synchronized (pcu) {
                    this.infoProvider = ccu;
                }
                ccu.doUpgrade(session);
            }
            catch (Exception e) {
                this.log.warn("Exception trying to execute " + Cq62ConfContentUpgrade.class.getSimpleName() + ".doUpgrade()", (Throwable)e);
                this.discardPendingChanges(session);
            }
            try {
                Cq62FormsContentUpgrade fcu = new Cq62FormsContentUpgrade();
                pcu = this;
                synchronized (pcu) {
                    this.infoProvider = fcu;
                }
                fcu.doUpgrade(session);
            }
            catch (Exception e3) {
                this.log.warn("Exception trying to execute " + Cq62ConfContentUpgrade.class.getSimpleName() + ".doUpgrade()", (Throwable)e3);
                this.discardPendingChanges(session);
            }
            Cq62StartupCodeUpgrade e3 = this;
            synchronized (e3) {
                this.infoProvider = null;
            }
            this.statusNode.recordUpgrade(this.repository);
        }
        catch (Exception e) {
            this.log.warn("Exception in " + this.getClass().getSimpleName() + ".run()", (Throwable)e);
        }
        finally {
            if (session != null) {
                session.logout();
            }
        }
    }

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

    private void discardPendingChanges(Session session) {
        try {
            if (session.hasPendingChanges()) {
                session.refresh(false);
            }
        }
        catch (RepositoryException e) {
            this.log.error("Exception trying to discard pending changes", (Throwable)e);
        }
    }

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

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

    protected void bindUserManagerService(UserManagementService userManagementService) {
        this.userManagerService = userManagementService;
    }

    protected void unbindUserManagerService(UserManagementService userManagementService) {
        if (this.userManagerService == userManagementService) {
            this.userManagerService = null;
        }
    }

    protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resourceResolverFactory = resourceResolverFactory;
    }

    protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resourceResolverFactory == resourceResolverFactory) {
            this.resourceResolverFactory = null;
        }
    }
}