Cq62ConfContentUpgrade.java 3.01 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  org.apache.jackrabbit.commons.JcrUtils
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.codeupgrade.impl.cq62;

import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.util.Arrays;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import org.apache.jackrabbit.commons.JcrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Cq62ConfContentUpgrade
implements ProgressInfoProvider {
    private final Logger log;
    private static final String[] DEFAULT_FALLBACK_CONFIG = new String[]{"/libs", "/apps", "/conf/global"};
    private String progressInfo;

    public Cq62ConfContentUpgrade() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

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

    void setProgressInfo(String info) {
        this.progressInfo = info;
        this.log.info(this.progressInfo);
    }

    public void doUpgrade(Session session) throws RepositoryException {
        this.setProgressInfo("Converting and removing /content/@cq:conf");
        Node content = session.getNode("/content");
        if (content.hasProperty("cq:conf")) {
            try {
                Object[] paths;
                Property prop = content.getProperty("cq:conf");
                if (prop.isMultiple()) {
                    Value[] values = prop.getValues();
                    paths = new String[values.length];
                    for (int i = 0; i < values.length; ++i) {
                        paths[i] = values[i].getString();
                    }
                } else {
                    paths = new String[]{prop.getString()};
                }
                if (!Arrays.equals(paths, DEFAULT_FALLBACK_CONFIG)) {
                    this.setConfMgrOSGiConfiguration(session, (String[])paths);
                }
                prop.remove();
                session.save();
            }
            catch (RepositoryException e) {
                this.log.error("Could not convert cq:conf property on /content to OSGi configuration confmgr.fallback.paths of com.adobe.granite.confmgr.impl.ConfMgrImpl", (Throwable)e);
            }
        }
    }

    private void setConfMgrOSGiConfiguration(Session session, String[] fallbackPaths) throws RepositoryException {
        this.log.info("Converting /content/@cq:conf into OSGi configuration at /apps/system/config/com.adobe.granite.confmgr.impl.ConfMgrImpl");
        Node config = JcrUtils.getOrCreateByPath((Node)session.getNode("/apps"), (String)"system/config/com.adobe.granite.confmgr.impl.ConfMgrImpl", (boolean)false, (String)"sling:Folder", (String)"sling:OsgiConfig", (boolean)false);
        config.setProperty("confmgr.fallback.paths", fallbackPaths);
    }
}