LegacyLiveActionRegistration.java 8.15 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.LiveAction
 *  com.day.cq.wcm.msm.api.LiveActionFactory
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.InvalidSyntaxException
 *  org.osgi.framework.ServiceEvent
 *  org.osgi.framework.ServiceListener
 *  org.osgi.framework.ServiceReference
 *  org.osgi.framework.ServiceRegistration
 *  org.osgi.service.component.ComponentContext
 *  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.LiveAction;
import com.day.cq.wcm.msm.api.LiveActionFactory;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.impl.Utils;
import com.day.cq.wcm.msm.impl.actions.ActionConfigImpl;
import java.util.Collection;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0, immediate=1)
public class LegacyLiveActionRegistration
implements ServiceListener {
    private BundleContext bundleContext;
    private static final Logger log = LoggerFactory.getLogger(LegacyLiveActionRegistration.class);
    private final Map<String, ServiceRegistration> registeredServices = new HashMap<String, ServiceRegistration>();

    protected void activate(ComponentContext componentContext) throws InvalidSyntaxException, WCMException {
        this.bundleContext = componentContext.getBundleContext();
        ServiceReference[] references = this.bundleContext.getServiceReferences(LiveAction.class.getName(), null);
        if (references != null) {
            for (ServiceReference ref : references) {
                this.register(ref);
            }
        }
        this.bundleContext.addServiceListener((ServiceListener)this, String.format("(%s=%s)", "objectClass", LiveAction.class.getName()));
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void deactivate(ComponentContext componentContext) {
        BundleContext ctx = componentContext.getBundleContext();
        Map<String, ServiceRegistration> map = this.registeredServices;
        synchronized (map) {
            ctx.removeServiceListener((ServiceListener)this);
            for (ServiceRegistration reg : this.registeredServices.values()) {
                reg.unregister();
            }
        }
    }

    public void serviceChanged(ServiceEvent serviceEvent) {
        try {
            switch (serviceEvent.getType()) {
                case 2: {
                    this.unregister(serviceEvent.getServiceReference());
                    this.register(serviceEvent.getServiceReference());
                    break;
                }
                case 1: {
                    this.register(serviceEvent.getServiceReference());
                    break;
                }
                case 4: {
                    this.unregister(serviceEvent.getServiceReference());
                }
            }
        }
        catch (WCMException e) {
            log.error("Failed to register Service {}", (Throwable)e);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void register(ServiceReference serviceReference) throws WCMException {
        Map<String, ServiceRegistration> map = this.registeredServices;
        synchronized (map) {
            LiveAction action = (LiveAction)this.bundleContext.getService(serviceReference);
            if (action == null) {
                log.warn("Received Service Event but could not access service {}", (Object)serviceReference);
                return;
            }
            Properties dict = new Properties();
            dict.put("liveActionName", action.getName());
            ServiceRegistration registration = this.bundleContext.registerService(LiveActionFactory.class.getName(), (Object)new LegacyLiveActionFactory(action), (Dictionary)dict);
            this.registeredServices.put((String)serviceReference.getProperty("service.pid"), registration);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void unregister(ServiceReference serviceReference) {
        Map<String, ServiceRegistration> map = this.registeredServices;
        synchronized (map) {
            String service = (String)serviceReference.getProperty("service.pid");
            if (this.registeredServices.containsKey(service)) {
                ServiceRegistration registration = this.registeredServices.remove(service);
                registration.unregister();
            }
        }
    }

    private static final class LegacyLiveAction
    implements LiveAction {
        private final ActionConfig config;
        private final LiveAction delegatee;

        public LegacyLiveAction(LiveAction action, ActionConfig ac) {
            this.config = ac;
            this.delegatee = action;
        }

        public String getName() {
            return this.delegatee.getName();
        }

        public void execute(Resource source, Resource target, LiveRelationship relation, boolean autoSave, boolean isResetRollout) throws WCMException {
            this.execute(source.getResourceResolver(), relation, this.config, autoSave, isResetRollout);
        }

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

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

        public String getParameterName() {
            return this.delegatee.getParameterName();
        }

        public String getTitle() {
            return this.delegatee.getTitle();
        }

        public String[] getPropertiesNames() {
            return this.delegatee.getPropertiesNames();
        }

        public int getRank() {
            return this.delegatee.getRank();
        }

        public void write(JSONWriter out) throws JSONException {
            this.delegatee.write(out);
        }
    }

    private static final class LegacyLiveActionFactory
    implements LiveActionFactory<LiveAction> {
        private final LiveAction action;

        private LegacyLiveActionFactory(LiveAction action) throws WCMException {
            this.action = action;
        }

        public String createsAction() {
            return this.action.getName();
        }

        public LiveAction createAction(Resource resource) throws WCMException {
            try {
                return new LegacyLiveAction(this.action, new ActionConfigImpl(this.action.getName(), this.action.getParameterName(), Utils.nodeToMap((Node)resource.adaptTo(Node.class)), this.action.getRank()));
            }
            catch (RepositoryException e) {
                throw new WCMException((Throwable)e);
            }
        }
    }

}