ActionManagerImpl.java 7.87 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.ActionConfig
 *  com.day.cq.wcm.msm.api.ActionManager
 *  com.day.cq.wcm.msm.api.LiveAction
 *  com.day.cq.wcm.msm.api.LiveActionFactory
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.RolloutConfig
 *  com.day.cq.wcm.msm.api.RolloutManager
 *  com.day.cq.wcm.msm.api.RolloutManager$Trigger
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceReference
 *  org.osgi.service.component.ComponentContext
 *  org.osgi.util.tracker.ServiceTracker
 *  org.osgi.util.tracker.ServiceTrackerCustomizer
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl.actions;

import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.ActionConfig;
import com.day.cq.wcm.msm.api.ActionManager;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveActionFactory;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.Utils;
import com.day.cq.wcm.msm.impl.actions.ActionConfigImpl;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0, immediate=0)
@Service
public class ActionManagerImpl
implements ActionManager {
    private final Logger log = LoggerFactory.getLogger(ActionManagerImpl.class);
    private ServiceRegistry<LiveActionFactory> factoryRegistry;

    protected void activate(ComponentContext context) {
        this.factoryRegistry = new ServiceRegistry<LiveActionFactory>(context.getBundleContext(), LiveActionFactory.class);
    }

    protected void deactivate(ComponentContext context) {
        if (this.factoryRegistry != null) {
            this.factoryRegistry.close();
        }
    }

    public Map<String, LiveAction> getRegistredActions() {
        HashMap<String, LiveAction> ret = new HashMap<String, LiveAction>();
        for (ServiceReference ref : this.factoryRegistry.getServiceReferences()) {
            LiveActionFactory factory = this.factoryRegistry.getService(ref);
            if (factory == null) continue;
            try {
                LiveAction action = factory.createAction(null);
                Object prop = ref.getProperty("liveActionName");
                if (prop instanceof String[]) {
                    for (String name : (String[])prop) {
                        ret.put(name, action);
                    }
                    continue;
                }
                ret.put(action.getName(), action);
                continue;
            }
            catch (WCMException e) {
                this.log.error("Failed to add actions {}", (Throwable)e);
            }
        }
        return ret;
    }

    public LiveAction getAction(String name) {
        return this.getRegistredActions().get(name);
    }

    public void executeActions(ResourceResolver resolver, LiveRelationship relation, RolloutManager.Trigger trigger, boolean autoSave) throws WCMException {
        this.executeActions(resolver, relation, trigger, autoSave, false);
    }

    public void executeActions(ResourceResolver resolver, LiveRelationship relation, RolloutManager.Trigger trigger, boolean autoSave, boolean isResetRollout) throws WCMException {
        List rolloutConfigs = relation.getRolloutConfigs();
        for (RolloutConfig rc : rolloutConfigs) {
            if (trigger != null && !trigger.equals((Object)rc.getTrigger())) continue;
            Set configs = rc.getActionsConfig();
            for (ActionConfig config : configs) {
                LiveAction action = this.getAction(config.getName());
                if (action != null) {
                    action.execute(resolver, relation, config, autoSave, isResetRollout);
                    continue;
                }
                throw new WCMException("Unregistered action name " + config.getName());
            }
        }
    }

    public void executeAction(ResourceResolver resolver, LiveRelationship relation, ActionConfig config, boolean autoSave) throws WCMException {
        this.executeAction(resolver, relation, config, autoSave, false);
    }

    public void executeAction(ResourceResolver resolver, LiveRelationship relation, ActionConfig config, boolean autoSave, boolean isResetRollout) throws WCMException {
        LiveAction action = this.getAction(config.getName());
        if (action == null) {
            throw new WCMException("Unregistered action name " + config.getName());
        }
        action.execute(resolver, relation, config, autoSave, isResetRollout);
    }

    public Set<ActionConfig> getActionsConfig(Resource root) throws RepositoryException {
        Node node = (Node)root.adaptTo(Node.class);
        return this.getActionsConfig(node);
    }

    public Set<ActionConfig> getActionsConfig(Node rootNode) throws RepositoryException {
        TreeSet<ActionConfig> res = new TreeSet<ActionConfig>();
        if (rootNode != null) {
            int rank = 0;
            NodeIterator ni = rootNode.getNodes();
            while (ni.hasNext()) {
                ActionConfig ac;
                Node n = ni.nextNode();
                if (!n.isNodeType("cq:LiveSyncAction") || (ac = this.getActionConfig(n, rank++)) == null) continue;
                res.add(ac);
            }
        }
        return res;
    }

    private ActionConfig getActionConfig(Node node, int rank) throws RepositoryException {
        if (node != null && node.isNodeType("cq:LiveSyncAction")) {
            String name = node.getName();
            LiveAction action = this.getAction(name);
            if (action != null) {
                return new ActionConfigImpl(action.getName(), action.getParameterName(), Utils.nodeToMap(node), rank);
            }
            this.log.warn("LiveAction name '{}' specified by node {} not found", (Object)name, (Object)node.getPath());
        }
        return null;
    }

    private static final class ServiceRegistry<Type> {
        private int cnt = -1;
        private final ServiceTracker tracker;
        private final Set<Type> services = new HashSet<Type>();
        private final BundleContext context;

        public ServiceRegistry(BundleContext context, Class<Type> cls) {
            this.context = context;
            this.tracker = new ServiceTracker(context, cls.getName(), null);
            this.tracker.open();
        }

        ServiceReference[] getServiceReferences() {
            return this.tracker.getServiceReferences();
        }

        Type getService(ServiceReference ref) {
            return (Type)this.context.getService(ref);
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        void close() {
            Set<Type> set = this.services;
            synchronized (set) {
                if (this.tracker != null) {
                    this.tracker.close();
                }
                this.services.clear();
            }
        }
    }

}