ReplicationPathTransformerProviderImpl.java 3.13 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.Service
 *  org.apache.sling.commons.osgi.ServiceUtil
 */
package com.day.cq.replication.impl;

import com.day.cq.replication.Agent;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationPathTransformer;
import com.day.cq.replication.impl.ReplicationPathTransformerProvider;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
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.Service;
import org.apache.sling.commons.osgi.ServiceUtil;

@Component(metatype=0, immediate=1)
@Service(value={ReplicationPathTransformerProvider.class})
public class ReplicationPathTransformerProviderImpl
implements ReplicationPathTransformerProvider {
    @Reference(name="transformer", referenceInterface=ReplicationPathTransformer.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
    private final Map<Comparable<Object>, ReplicationPathTransformer> transformerServiceMap = new TreeMap<Comparable<Object>, ReplicationPathTransformer>();
    private volatile ReplicationPathTransformer[] transformers = new ReplicationPathTransformer[0];

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void bindTransformer(ReplicationPathTransformer transformer, Map<String, Object> props) {
        Map<Comparable<Object>, ReplicationPathTransformer> map = this.transformerServiceMap;
        synchronized (map) {
            this.transformerServiceMap.put(ServiceUtil.getComparableForServiceRanking(props), transformer);
            this.transformers = this.transformerServiceMap.values().toArray(new ReplicationPathTransformer[this.transformerServiceMap.size()]);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void unbindTransformer(ReplicationPathTransformer transformer, Map<String, Object> props) {
        Map<Comparable<Object>, ReplicationPathTransformer> map = this.transformerServiceMap;
        synchronized (map) {
            this.transformerServiceMap.remove(ServiceUtil.getComparableForServiceRanking(props));
            this.transformers = this.transformerServiceMap.values().toArray(new ReplicationPathTransformer[this.transformerServiceMap.size()]);
        }
    }

    @Override
    public ReplicationPathTransformer getTransformer(Session session, ReplicationAction action, Agent agent) {
        for (ReplicationPathTransformer transformer : this.transformers) {
            if (!transformer.accepts(session, action, agent)) continue;
            return transformer;
        }
        return null;
    }
}