ServiceTrackerImpl.java 9.56 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.References
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.apache.sling.commons.osgi.ServiceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl;

import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.ContentBuilder;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationContent;
import com.day.cq.replication.ReplicationContentFactory;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.TransportHandler;
import com.day.cq.replication.impl.ServiceTracker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.References;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.commons.osgi.ServiceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@References(value={@Reference(name="contentBuilder", referenceInterface=ContentBuilder.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC), @Reference(name="transportHandler", referenceInterface=TransportHandler.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)})
@Service(value={ServiceTracker.class})
public class ServiceTrackerImpl
implements ServiceTracker {
    private final Logger logger;
    private final Map<String, List<ContentBuilderDesc>> contentBuildersByName;
    private final List<TransportHandlerDesc> transportHandlers;

    public ServiceTrackerImpl() {
        this.logger = LoggerFactory.getLogger(this.getClass());
        this.contentBuildersByName = new HashMap<String, List<ContentBuilderDesc>>();
        this.transportHandlers = new ArrayList<TransportHandlerDesc>();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void bindContentBuilder(ContentBuilder builder, Map<String, Object> props) {
        String name = PropertiesUtil.toString((Object)props.get("name"), (String)null);
        if (name != null) {
            Map<String, List<ContentBuilderDesc>> map = this.contentBuildersByName;
            synchronized (map) {
                List<ContentBuilderDesc> list = this.contentBuildersByName.get(name);
                if (list == null) {
                    list = new ArrayList<ContentBuilderDesc>();
                    this.contentBuildersByName.put(name, list);
                }
                ContentBuilderDesc desc = new ContentBuilderDesc();
                desc.contentBuilder = new ContentBuilderWrapper(builder);
                desc.properties = props;
                list.add(desc);
                Collections.sort(list);
            }
            this.logger.debug("Registering content builder {} : {}", (Object)name, (Object)builder);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void bindTransportHandler(TransportHandler handler, Map<String, Object> props) {
        List<TransportHandlerDesc> list = this.transportHandlers;
        synchronized (list) {
            TransportHandlerDesc desc = new TransportHandlerDesc();
            desc.transportHandler = handler;
            desc.properties = props;
            this.transportHandlers.add(desc);
            Collections.sort(this.transportHandlers);
        }
        this.logger.debug("Registering transport handler: {}", (Object)handler);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void unbindContentBuilder(ContentBuilder builder, Map<String, Object> props) {
        String name = PropertiesUtil.toString((Object)props.get("name"), (String)null);
        if (name != null) {
            Map<String, List<ContentBuilderDesc>> map = this.contentBuildersByName;
            synchronized (map) {
                Long key = (Long)props.get("service.id");
                List<ContentBuilderDesc> list = this.contentBuildersByName.get(name);
                if (list != null) {
                    Iterator<ContentBuilderDesc> i = list.iterator();
                    while (i.hasNext()) {
                        ContentBuilderDesc desc = i.next();
                        if (key != (Long)desc.properties.get("service.id")) continue;
                        i.remove();
                        break;
                    }
                    if (list.size() == 0) {
                        this.contentBuildersByName.remove(name);
                    }
                }
            }
        }
        this.logger.debug("Unregistering content builder {} : {}", (Object)name, (Object)builder);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void unbindTransportHandler(TransportHandler handler, Map<String, Object> props) {
        Long key = (Long)props.get("service.id");
        List<TransportHandlerDesc> list = this.transportHandlers;
        synchronized (list) {
            Iterator<TransportHandlerDesc> i = this.transportHandlers.iterator();
            while (i.hasNext()) {
                TransportHandlerDesc desc = i.next();
                if (key != (Long)desc.properties.get("service.id")) continue;
                i.remove();
                break;
            }
            this.transportHandlers.remove(key);
        }
        this.logger.debug("Unregistering transport handler: {}", (Object)handler);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public ContentBuilder getContentBuilder(String name) {
        Map<String, List<ContentBuilderDesc>> map = this.contentBuildersByName;
        synchronized (map) {
            List<ContentBuilderDesc> list = this.contentBuildersByName.get(name);
            if (list != null && list.size() > 0) {
                return list.get((int)0).contentBuilder;
            }
            return null;
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public TransportHandler getTransportHandler(AgentConfig config) {
        List<TransportHandlerDesc> list = this.transportHandlers;
        synchronized (list) {
            for (TransportHandlerDesc desc : this.transportHandlers) {
                if (!desc.transportHandler.canHandle(config)) continue;
                return desc.transportHandler;
            }
            return null;
        }
    }

    public class ContentBuilderWrapper
    implements ContentBuilder {
        private final ContentBuilder delegatee;
        private final boolean hasNewMethod;

        public ContentBuilderWrapper(ContentBuilder cb) {
            this.delegatee = cb;
            boolean hasMethod = false;
            try {
                this.delegatee.create(null, null, null, null);
                hasMethod = true;
            }
            catch (AbstractMethodError var4_4) {
            }
            catch (Throwable t) {
                hasMethod = true;
            }
            this.hasNewMethod = hasMethod;
        }

        @Override
        public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory contentFactory) throws ReplicationException {
            return this.delegatee.create(session, action, contentFactory);
        }

        @Override
        public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory contentFactory, Map<String, Object> parameters) throws ReplicationException {
            if (this.hasNewMethod) {
                return this.delegatee.create(session, action, contentFactory, parameters);
            }
            return this.delegatee.create(session, action, contentFactory);
        }

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

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

    protected static final class TransportHandlerDesc
    implements Comparable<TransportHandlerDesc> {
        public TransportHandler transportHandler;
        public Map<String, Object> properties;

        protected TransportHandlerDesc() {
        }

        @Override
        public int compareTo(TransportHandlerDesc o) {
            Comparable tc = ServiceUtil.getComparableForServiceRanking(this.properties);
            return - tc.compareTo(o.properties);
        }
    }

    protected static final class ContentBuilderDesc
    implements Comparable<ContentBuilderDesc> {
        public ContentBuilderWrapper contentBuilder;
        public Map<String, Object> properties;

        protected ContentBuilderDesc() {
        }

        @Override
        public int compareTo(ContentBuilderDesc o) {
            Comparable tc = ServiceUtil.getComparableForServiceRanking(this.properties);
            return - tc.compareTo(o.properties);
        }
    }

}