BlueprintManagerFactoryImpl.java 9.66 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.msm.Blueprint
 *  com.day.cq.wcm.msm.api.Blueprint
 *  com.day.cq.wcm.msm.api.BlueprintManager
 *  com.day.cq.wcm.msm.api.BlueprintManagerFactory
 *  com.day.cq.wcm.msm.api.RolloutConfigManager
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  javax.jcr.observation.EventIterator
 *  javax.jcr.observation.EventListener
 *  javax.jcr.observation.ObservationManager
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.adapter.AdapterFactory
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.msm.Blueprint;
import com.day.cq.wcm.msm.api.BlueprintManager;
import com.day.cq.wcm.msm.api.BlueprintManagerFactory;
import com.day.cq.wcm.msm.api.RolloutConfigManager;
import com.day.cq.wcm.msm.impl.BlueprintImpl;
import com.day.cq.wcm.msm.impl.BlueprintManagerImpl;
import com.day.cq.wcm.msm.impl.RolloutConfigManagerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
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.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1, metatype=0)
@Service(value={BlueprintManagerFactory.class, AdapterFactory.class})
public class BlueprintManagerFactoryImpl
implements BlueprintManagerFactory,
AdapterFactory,
EventListener {
    private final Logger log = LoggerFactory.getLogger(BlueprintManagerFactoryImpl.class);
    static final String DEFAULT_BLUEPRINT_ROOT = "/etc/blueprints";
    static final String DEFAULT_BLUEPRINT_RESOURCE_TYPE = "wcm/msm/components/blueprint";
    @Property(value={"wcm/msm/components/blueprint"})
    private static final String BLUEPRINT_RESOURCE_TYPE = "cq.wcm.msm.blueprint.resource_type";
    @Property(value={"/etc/blueprints"}, propertyPrivate=1)
    private static final String BLUEPRINT_ROOT = "cq.wcm.msm.blueprint.root";
    @Property(name="adapters")
    public static final String[] ADAPTER_CLASSES = new String[]{com.day.cq.wcm.msm.api.Blueprint.class.getName(), Blueprint.class.getName(), BlueprintManager.class.getName()};
    @Property(name="adaptables")
    public static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName(), ResourceResolver.class.getName()};
    @Reference
    private ResourceResolverFactory resolverFactory = null;
    @Reference
    private RolloutConfigManagerFactory rolloutConfigMgrFactory = null;
    private Lock lock = new ReentrantLock();
    private List<String> guardedPath;
    private String blueprintResourceType;
    private String bpRoot;
    private ResourceResolver listeningResolver;

    @Activate
    protected void activate(ComponentContext context) throws Exception {
        this.blueprintResourceType = PropertiesUtil.toString(context.getProperties().get("cq.wcm.msm.blueprint.resource_type"), (String)"wcm/msm/components/blueprint");
        this.bpRoot = PropertiesUtil.toString(context.getProperties().get("cq.wcm.msm.blueprint.root"), (String)"/etc/blueprints");
        this.listeningResolver = this.resolverFactory.getServiceResourceResolver(null);
        Session listeningSession = (Session)this.listeningResolver.adaptTo(Session.class);
        listeningSession.getWorkspace().getObservationManager().addEventListener((EventListener)this, 63, this.bpRoot, true, null, null, true);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Deactivate
    protected void deactivate(ComponentContext context) throws RepositoryException {
        this.lock.lock();
        try {
            this.guardedPath = null;
        }
        finally {
            this.lock.unlock();
        }
        if (this.listeningResolver != null && this.listeningResolver.isLive()) {
            ObservationManager om = ((Session)this.listeningResolver.adaptTo(Session.class)).getWorkspace().getObservationManager();
            if (om != null) {
                om.removeEventListener((EventListener)this);
            }
            this.listeningResolver.close();
        }
    }

    public BlueprintManagerImpl getBlueprintManager(ResourceResolver resourceResolver) {
        return new BlueprintManagerImpl(resourceResolver, this.rolloutConfigMgrFactory.create(resourceResolver), this);
    }

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (Blueprint.class.isAssignableFrom(type) && Resource.class.isInstance(adaptable)) {
            Resource resource = (Resource)adaptable;
            Page page = (Page)resource.adaptTo(Page.class);
            if (page != null) {
                return (AdapterType)new BlueprintImpl(page, this.rolloutConfigMgrFactory.create(resource.getResourceResolver()));
            }
            this.log.debug("Resource at {} could not be adapted to a Blueprint: not Page", (Object)((Resource)adaptable).getPath());
            return null;
        }
        if (type == BlueprintManager.class && ResourceResolver.class.isInstance(adaptable)) {
            return (AdapterType)this.getBlueprintManager((ResourceResolver)adaptable);
        }
        return null;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void onEvent(EventIterator events) {
        this.lock.lock();
        try {
            this.guardedPath = null;
        }
        finally {
            this.lock.unlock();
        }
    }

    List<String> getBlueprintPaths() throws RepositoryException {
        ResourceResolver serviceResolver = null;
        try {
            Resource resource;
            this.lock.lock();
            if (this.guardedPath == null && (resource = (serviceResolver = this.resolverFactory.getServiceResourceResolver(null)).getResource(this.bpRoot)) != null) {
                this.guardedPath = new ArrayList<String>();
                this.collectBlueprintPathes(resource, this.guardedPath);
            }
            if (this.guardedPath == null) {
                this.guardedPath = Collections.emptyList();
            }
            resource = this.guardedPath;
            return resource;
        }
        catch (LoginException e) {
            this.log.error("Failed to access Service ResourceResolver {}", (Throwable)e);
            throw new RepositoryException((Throwable)e);
        }
        finally {
            if (serviceResolver != null) {
                serviceResolver.close();
            }
            this.lock.unlock();
        }
    }

    private void collectBlueprintPathes(Resource resource, List<String> all) throws RepositoryException {
        Page page = (Page)resource.adaptTo(Page.class);
        if (page != null) {
            if (this.isBlueprintResource(page)) {
                all.add(resource.getPath());
            }
            Iterator children = page.listChildren();
            while (children.hasNext()) {
                this.collectBlueprintPathes((Resource)((Page)children.next()).adaptTo(Resource.class), all);
            }
        } else {
            Iterator iter = resource.listChildren();
            while (iter.hasNext()) {
                this.collectBlueprintPathes((Resource)iter.next(), all);
            }
        }
    }

    private boolean isBlueprintResource(Page page) {
        return page.getContentResource() != null && page.getContentResource().isResourceType(this.blueprintResourceType);
    }

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

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

    protected void bindRolloutConfigMgrFactory(RolloutConfigManagerFactory rolloutConfigManagerFactory) {
        this.rolloutConfigMgrFactory = rolloutConfigManagerFactory;
    }

    protected void unbindRolloutConfigMgrFactory(RolloutConfigManagerFactory rolloutConfigManagerFactory) {
        if (this.rolloutConfigMgrFactory == rolloutConfigManagerFactory) {
            this.rolloutConfigMgrFactory = null;
        }
    }
}