MSMEventProcessor.java 8.87 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.ReplicationAction
 *  com.day.cq.replication.ReplicationActionType
 *  com.day.cq.wcm.api.PageEvent
 *  com.day.cq.wcm.api.PageModification
 *  com.day.cq.wcm.api.PageModification$ModificationType
 *  com.day.cq.wcm.msm.api.LiveRelationshipManager
 *  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.NonExistingResource
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.osgi.service.component.ComponentContext
 *  org.osgi.service.event.Event
 *  org.osgi.service.event.EventAdmin
 *  org.osgi.service.event.EventHandler
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl;

import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.wcm.api.PageEvent;
import com.day.cq.wcm.api.PageModification;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.impl.BlueprintEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
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.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
@Properties(value={@Property(propertyPrivate=1, name="event.topics", value={"com/day/cq/wcm/core/page", "com/day/cq/replication"}), @Property(name="event.filter", value={"(!(event.application=*))"})})
public class MSMEventProcessor
implements EventHandler {
    private static final Logger log = LoggerFactory.getLogger(MSMEventProcessor.class);
    @Reference
    private LiveRelationshipManager liveRelationshipManager = null;
    @Reference
    private EventAdmin eventAdmin = null;
    private ExecutorService executorService;

    @Activate
    public void activate(ComponentContext context) {
        this.executorService = Executors.newSingleThreadExecutor();
    }

    @Deactivate
    public void deactivate(ComponentContext context) throws InterruptedException {
        this.executorService.shutdown();
        try {
            this.executorService.awaitTermination(15, TimeUnit.SECONDS);
        }
        catch (InterruptedException ie) {
            log.error("Interrupted while waiting to shut-down; Cancel all tasks");
            this.executorService.shutdownNow();
            this.executorService.awaitTermination(15, TimeUnit.SECONDS);
            Thread.currentThread().interrupt();
        }
    }

    public void handleEvent(Event event) {
        PageEvent pageEvent = PageEvent.fromEvent((Event)event);
        if (pageEvent != null) {
            this.executorService.execute(new PageEventBuilder(pageEvent, this.eventAdmin));
        } else if (ReplicationAction.fromEvent((Event)event) != null) {
            this.executorService.execute(new ReplicationActionBuilder(ReplicationAction.fromEvent((Event)event), this.eventAdmin));
        }
    }

    private boolean filterBlueprint(String path) {
        return this.liveRelationshipManager.isSource((Resource)new NonExistingResource(null, path));
    }

    PageEventBuilder createPageEventBuilder(PageEvent event) {
        return new PageEventBuilder(event, this.eventAdmin);
    }

    ReplicationActionBuilder createReplicationActionBuilder(ReplicationAction action) {
        return new ReplicationActionBuilder(action, this.eventAdmin);
    }

    protected void bindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        this.liveRelationshipManager = liveRelationshipManager;
    }

    protected void unbindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        if (this.liveRelationshipManager == liveRelationshipManager) {
            this.liveRelationshipManager = null;
        }
    }

    protected void bindEventAdmin(EventAdmin eventAdmin) {
        this.eventAdmin = eventAdmin;
    }

    protected void unbindEventAdmin(EventAdmin eventAdmin) {
        if (this.eventAdmin == eventAdmin) {
            this.eventAdmin = null;
        }
    }

    class ReplicationActionBuilder
    implements Runnable {
        private final ReplicationAction replicationAction;
        private final EventAdmin eventAdmin;

        public ReplicationActionBuilder(ReplicationAction replicationAction, EventAdmin eventAdmin) {
            this.replicationAction = replicationAction;
            this.eventAdmin = eventAdmin;
        }

        @Override
        public void run() {
            if (this.filterType(this.replicationAction)) {
                ArrayList<String> filteredPaths = new ArrayList<String>(this.replicationAction.getPaths().length);
                for (String path : this.replicationAction.getPaths()) {
                    if (!MSMEventProcessor.this.filterBlueprint(path)) continue;
                    filteredPaths.add(path);
                }
                if (!filteredPaths.isEmpty()) {
                    BlueprintEvent bpEvent = BlueprintEvent.fromReplicationAction(new ReplicationAction(this.replicationAction.getType(), filteredPaths.toArray(new String[filteredPaths.size()]), this.replicationAction.getTime(), this.replicationAction.getUserId(), this.replicationAction.getRevision()));
                    this.eventAdmin.postEvent(bpEvent.toEvent(false));
                } else {
                    log.debug("ReplicationAction {} paths not contained in a Blueprint", (Object)this.replicationAction);
                }
            } else {
                log.debug("ReplicationAction {} is not of required Type", (Object)this.replicationAction);
            }
        }

        private boolean filterType(ReplicationAction replicationAction) {
            ReplicationActionType type = replicationAction.getType();
            return type != ReplicationActionType.INTERNAL_POLL && type != ReplicationActionType.TEST;
        }
    }

    class PageEventBuilder
    implements Runnable {
        private final PageEvent pageEvent;
        private final EventAdmin eventAdmin;

        public PageEventBuilder(PageEvent pageEvent, EventAdmin eventAdmin) {
            this.pageEvent = pageEvent;
            this.eventAdmin = eventAdmin;
        }

        @Override
        public void run() {
            ArrayList<PageModification> filteredEvent = new ArrayList<PageModification>();
            Iterator itr = this.pageEvent.getModifications();
            while (itr.hasNext()) {
                PageModification modification = (PageModification)itr.next();
                if (this.filterType(modification)) {
                    if (MSMEventProcessor.this.filterBlueprint(modification.getPath()) || modification.getDestination() != null && MSMEventProcessor.this.filterBlueprint(modification.getDestination())) {
                        filteredEvent.add(modification);
                        continue;
                    }
                    log.debug("MSM does only deal with Blueprint Resource {}: ignore", (Object)modification.getPath());
                    continue;
                }
                log.debug("MSM does not deal with {}: ignore", (Object)modification.getType().name());
            }
            if (!filteredEvent.isEmpty()) {
                BlueprintEvent bpEvent = BlueprintEvent.fromPageEvent(new PageEvent(filteredEvent));
                this.eventAdmin.sendEvent(bpEvent.toEvent(true));
                this.eventAdmin.postEvent(bpEvent.toEvent(false));
            } else {
                log.debug("PageEvent {} did not contain any PageModification of required type in a Blueprint: ignore", (Object)this.pageEvent);
            }
        }

        private boolean filterType(PageModification modification) {
            PageModification.ModificationType type = modification.getType();
            return !type.equals((Object)PageModification.ModificationType.VERSION_CREATED) && !type.equals((Object)PageModification.ModificationType.VALID) && !type.equals((Object)PageModification.ModificationType.INVALID);
        }
    }

}