CanvasPageDeleteHandler.java 11.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.ReplicationActionType
 *  com.day.cq.replication.ReplicationException
 *  com.day.cq.replication.Replicator
 *  com.day.cq.search.PredicateGroup
 *  com.day.cq.search.Query
 *  com.day.cq.search.QueryBuilder
 *  com.day.cq.search.result.Hit
 *  com.day.cq.search.result.SearchResult
 *  com.day.cq.wcm.api.PageEvent
 *  com.day.cq.wcm.api.PageModification
 *  com.day.cq.wcm.api.PageModification$ModificationType
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  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.OsgiUtil
 *  org.apache.sling.commons.threads.ModifiableThreadPoolConfig
 *  org.apache.sling.commons.threads.ThreadPool
 *  org.apache.sling.commons.threads.ThreadPoolConfig
 *  org.apache.sling.commons.threads.ThreadPoolConfig$ThreadPriority
 *  org.apache.sling.commons.threads.ThreadPoolManager
 *  org.osgi.service.component.ComponentContext
 *  org.osgi.service.event.Event
 *  org.osgi.service.event.EventHandler
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.designimporter.impl;

import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.Replicator;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import com.day.cq.wcm.api.PageEvent;
import com.day.cq.wcm.api.PageModification;
import com.day.cq.wcm.designimporter.util.ImporterUtil;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
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.Deactivate;
import org.apache.felix.scr.annotations.Properties;
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.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.OsgiUtil;
import org.apache.sling.commons.threads.ModifiableThreadPoolConfig;
import org.apache.sling.commons.threads.ThreadPool;
import org.apache.sling.commons.threads.ThreadPoolConfig;
import org.apache.sling.commons.threads.ThreadPoolManager;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1)
@Service
@Properties(value={@Property(name="event.topics", value={"com/day/cq/wcm/core/page"}, propertyPrivate=1), @Property(name="event.filter", value={"(!(event.application=*))"}, propertyPrivate=1)})
public class CanvasPageDeleteHandler
implements EventHandler {
    private static final Logger logger = LoggerFactory.getLogger(CanvasPageDeleteHandler.class);
    private static final String SERVICE_USER = "canvaspage-delete-service";
    @Reference
    Replicator replicator;
    @Reference
    ResourceResolverFactory resourceResolverFactory;
    @Reference
    private ThreadPoolManager threadPoolManager;
    private ThreadPool threadPool;
    @Property(label="minThreadPoolSize", description="Minimum Thread pool size", intValue={2})
    private static final String MINIMUM_THREAD_POOL_SIZE = "minThreadPoolSize";
    private int minThreadPoolSize;
    @Property(label="maxThreadPoolSize", description="Maximum Thread pool size", intValue={10})
    private static final String MAXIMUM_THREAD_POOL_SIZE = "maxThreadPoolSize";
    private int maxThreadPoolSize;
    @Reference
    QueryBuilder queryBuilder;

    @Activate
    protected final void activate(Map<Object, Object> config) {
        this.minThreadPoolSize = OsgiUtil.toInteger((Object)config.get("minThreadPoolSize"), (int)5);
        this.maxThreadPoolSize = OsgiUtil.toInteger((Object)config.get("maxThreadPoolSize"), (int)10);
        ModifiableThreadPoolConfig threadPoolConfig = new ModifiableThreadPoolConfig();
        if (threadPoolConfig.getMinPoolSize() < this.minThreadPoolSize) {
            threadPoolConfig.setMinPoolSize(this.minThreadPoolSize);
        }
        if (threadPoolConfig.getMaxPoolSize() < this.maxThreadPoolSize) {
            threadPoolConfig.setMaxPoolSize(this.maxThreadPoolSize);
        }
        threadPoolConfig.setPriority(ThreadPoolConfig.ThreadPriority.NORM);
        this.threadPool = this.threadPoolManager.create((ThreadPoolConfig)threadPoolConfig);
        if (this.threadPool == null) {
            throw new IllegalStateException("Could not get a ThreadPool");
        }
    }

    @Deactivate
    protected void deactivate(ComponentContext ctx) throws Exception {
        if (this.threadPool != null) {
            this.threadPoolManager.release(this.threadPool);
            this.threadPoolManager = null;
            this.threadPool = null;
        }
    }

    public void handleEvent(final Event event) {
        PageEvent pageEvent = PageEvent.fromEvent((Event)event);
        if (pageEvent != null) {
            Iterator iter = pageEvent.getModifications();
            while (iter.hasNext()) {
                PageModification mod = (PageModification)iter.next();
                if (!mod.getType().equals((Object)PageModification.ModificationType.DELETED)) continue;
                this.threadPool.execute(new Runnable(){

                    @Override
                    public void run() {
                        CanvasPageDeleteHandler.this.process(event);
                    }
                });
                break;
            }
        }
    }

    private boolean process(Event event) {
        PageEvent pageEvent = PageEvent.fromEvent((Event)event);
        if (pageEvent != null) {
            Iterator iter = pageEvent.getModifications();
            while (iter.hasNext()) {
                PageModification mod = (PageModification)iter.next();
                switch (mod.getType()) {
                    case DELETED: {
                        this.handlePageDelete(mod.getPath());
                    }
                }
            }
        }
        return true;
    }

    private void handlePageDelete(String pagePath) {
        try {
            ResourceResolver resolver = this.getServiceResolver("canvaspage-activate-service");
            Session session = (Session)resolver.adaptTo(Session.class);
            LinkedList<String> deactivationPaths = new LinkedList<String>();
            this.addDesignDeactivationPath(pagePath, session, deactivationPaths);
            this.addComponentDeactivationPath(pagePath, session, deactivationPaths);
            int i = pagePath.lastIndexOf("/");
            if (i >= 0 && i < pagePath.length() - 1) {
                String name = pagePath.substring(i + 1);
                this.addDesignDeactivationPath(pagePath + "/" + name, session, deactivationPaths);
                this.addComponentDeactivationPath(pagePath + "/" + name, session, deactivationPaths);
            }
            ResourceResolver deleteResolver = this.getServiceResolver("canvaspage-delete-service");
            for (String path : deactivationPaths) {
                this.replicator.replicate(session, ReplicationActionType.DEACTIVATE, path);
                Resource resource = deleteResolver.resolve(path);
                ImporterUtil.deleteCanvasArtifact(resource);
            }
            ((Session)deleteResolver.adaptTo(Session.class)).save();
        }
        catch (LoginException e) {
            logger.error("Login error occurred while handling canvas page deletion", (Throwable)e);
        }
        catch (RepositoryException e) {
            logger.error("Error occurred while handling canvas page deletion", (Throwable)e);
        }
        catch (ReplicationException e) {
            logger.error("Replication error occurred while handling canvas page deletion", (Throwable)e);
        }
    }

    private void addDesignDeactivationPath(String canvasPagePath, Session session, List<String> deactivationPaths) throws RepositoryException {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("path", "/etc/designs");
        map.put("property", "cq:ownerCanvasPage");
        map.put("property.value", canvasPagePath);
        Query query = this.queryBuilder.createQuery(PredicateGroup.create(map), session);
        SearchResult result = query.getResult();
        for (Hit hit : result.getHits()) {
            String path = hit.getPath();
            path = path.replaceAll("/jcr:content$", "");
            deactivationPaths.add(path);
        }
    }

    private void addComponentDeactivationPath(String canvasPagePath, Session session, List<String> deactivationPaths) throws RepositoryException {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("path", "/apps");
        map.put("property", "cq:ownerCanvasPage");
        map.put("property.value", canvasPagePath);
        Query query = this.queryBuilder.createQuery(PredicateGroup.create(map), session);
        SearchResult result = query.getResult();
        for (Hit hit : result.getHits()) {
            String path = hit.getPath();
            deactivationPaths.add(path);
        }
    }

    private ResourceResolver getServiceResolver(String userId) throws LoginException {
        Map<String, String> info = Collections.singletonMap("sling.service.subservice", userId);
        return this.resourceResolverFactory.getServiceResourceResolver(info);
    }

    protected void bindReplicator(Replicator replicator) {
        this.replicator = replicator;
    }

    protected void unbindReplicator(Replicator replicator) {
        if (this.replicator == replicator) {
            this.replicator = null;
        }
    }

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

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

    protected void bindThreadPoolManager(ThreadPoolManager threadPoolManager) {
        this.threadPoolManager = threadPoolManager;
    }

    protected void unbindThreadPoolManager(ThreadPoolManager threadPoolManager) {
        if (this.threadPoolManager == threadPoolManager) {
            this.threadPoolManager = null;
        }
    }

    protected void bindQueryBuilder(QueryBuilder queryBuilder) {
        this.queryBuilder = queryBuilder;
    }

    protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
        if (this.queryBuilder == queryBuilder) {
            this.queryBuilder = null;
        }
    }

}