JobConsumerInventoryServiceImpl.java 4.63 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang.StringUtils
 *  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.event.jobs.consumer.JobConsumer
 *  org.apache.sling.event.jobs.consumer.JobExecutor
 *  org.osgi.framework.ServiceReference
 */
package com.adobe.granite.offloading.impl;

import com.adobe.granite.offloading.impl.JobConsumerInfo;
import com.adobe.granite.offloading.impl.JobConsumerInfoImpl;
import com.adobe.granite.offloading.impl.JobConsumerInventoryService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang.StringUtils;
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.event.jobs.consumer.JobConsumer;
import org.apache.sling.event.jobs.consumer.JobExecutor;
import org.osgi.framework.ServiceReference;

@Component
@Service(value={JobConsumerInventoryService.class})
@References(value={@Reference(referenceInterface=JobConsumer.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC, name="jobConsumer"), @Reference(referenceInterface=JobExecutor.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC, name="jobExecutor")})
public class JobConsumerInventoryServiceImpl
implements JobConsumerInventoryService {
    private Map<String, List<JobConsumerInfo>> topicToConsumersMap = new ConcurrentHashMap<String, List<JobConsumerInfo>>();

    @Override
    public Map<String, List<JobConsumerInfo>> getRegisteredConsumers() {
        return this.topicToConsumersMap;
    }

    protected void bindJobExecutor(ServiceReference serviceReference) {
        this.bindService(serviceReference, true);
    }

    protected void unbindJobExecutor(ServiceReference serviceReference) {
        this.unbindService(serviceReference, true);
    }

    protected void bindJobConsumer(ServiceReference serviceReference) {
        this.bindService(serviceReference, false);
    }

    protected void unbindJobConsumer(ServiceReference serviceReference) {
        this.unbindService(serviceReference, false);
    }

    private void bindService(ServiceReference serviceReference, boolean isExecutor) {
        String[] topics = isExecutor ? PropertiesUtil.toStringArray((Object)serviceReference.getProperty("job.topics")) : PropertiesUtil.toStringArray((Object)serviceReference.getProperty("job.topics"));
        if (topics != null) {
            for (String topic : topics) {
                if (!StringUtils.isNotEmpty((String)topic)) continue;
                List<JobConsumerInfo> consumers = this.topicToConsumersMap.get(topic = topic.trim());
                if (consumers == null) {
                    consumers = new ArrayList<JobConsumerInfo>();
                    this.topicToConsumersMap.put(topic, consumers);
                }
                JobConsumerInfoImpl consumerInfo = new JobConsumerInfoImpl(serviceReference, isExecutor);
                consumers.add(consumerInfo);
                Collections.sort(consumers);
            }
        }
    }

    private void unbindService(ServiceReference serviceReference, boolean isExecutor) {
        String[] topics = isExecutor ? PropertiesUtil.toStringArray((Object)serviceReference.getProperty("job.topics")) : PropertiesUtil.toStringArray((Object)serviceReference.getProperty("job.topics"));
        if (topics != null) {
            for (String topic : topics) {
                if (!StringUtils.isNotEmpty((String)topic)) continue;
                List<JobConsumerInfo> consumers = this.topicToConsumersMap.get(topic = topic.trim());
                if (consumers != null) {
                    JobConsumerInfoImpl consumerInfo = new JobConsumerInfoImpl(serviceReference, isExecutor);
                    consumers.remove(consumerInfo);
                    Collections.sort(consumers);
                }
                if (consumers.size() != 0) continue;
                this.topicToConsumersMap.remove(topic);
            }
        }
    }
}