OffloadingPropertyProvider.java 7.71 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.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.apache.sling.discovery.PropertyProvider
 *  org.osgi.framework.BundleContext
 *  org.osgi.service.cm.Configuration
 *  org.osgi.service.cm.ConfigurationAdmin
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.offloading.impl;

import com.adobe.granite.offloading.impl.JobConsumerInfo;
import com.adobe.granite.offloading.impl.JobConsumerInventoryService;
import java.util.Collection;
import java.util.Dictionary;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.discovery.PropertyProvider;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={PropertyProvider.class})
@Properties(value={@Property(name="instance.properties", value={"com.adobe.granite.offloading.infrastructure.osgiconsole.path", "com.adobe.granite.offloading.job.registeredtopics", "job.consumermanager.whitelist", "job.consumermanager.blacklist"})})
public class OffloadingPropertyProvider
implements PropertyProvider {
    private static final Logger LOG = LoggerFactory.getLogger(OffloadingPropertyProvider.class);
    private ComponentContext componentContext;
    @Reference
    private ConfigurationAdmin configurationAdmin;
    @Reference
    private JobConsumerInventoryService jobConsumerInventoryService;

    public String getProperty(String name) {
        if ("com.adobe.granite.offloading.infrastructure.osgiconsole.path".equals(name)) {
            return this.getOSGIConsolePath();
        }
        if ("com.adobe.granite.offloading.job.registeredtopics".equals(name)) {
            return this.getRegisteredTopics();
        }
        if ("job.consumermanager.whitelist".equals(name)) {
            return this.getWhitelistTopics();
        }
        if ("job.consumermanager.blacklist".equals(name)) {
            return this.getBlacklistTopics();
        }
        return null;
    }

    private String getOSGIConsolePath() {
        String consolePath = null;
        try {
            String filter = "(service.pid=org.apache.felix.webconsole.internal.servlet.OsgiManager)";
            Configuration[] configurations = this.configurationAdmin.listConfigurations(filter);
            if (configurations != null) {
                for (Configuration c : configurations) {
                    Dictionary properties = c.getProperties();
                    consolePath = (String)properties.get("manager.root");
                }
            }
        }
        catch (Exception e) {
            LOG.error("Unable to retrieve Felix's web console path.", (Throwable)e);
        }
        if (StringUtils.isEmpty((String)consolePath)) {
            consolePath = this.componentContext.getBundleContext().getProperty("felix.webconsole.manager.root");
        }
        if (StringUtils.isEmpty((String)consolePath)) {
            consolePath = "/system/console";
        }
        return consolePath;
    }

    private String getRegisteredTopics() {
        TreeSet<String> topics;
        Map<String, List<JobConsumerInfo>> topicToConsumersMap = this.jobConsumerInventoryService.getRegisteredConsumers();
        if (topicToConsumersMap != null && (topics = new TreeSet<String>(topicToConsumersMap.keySet())).size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (String topic : topics) {
                sb.append(topic).append(",");
            }
            if (sb.length() > 0) {
                return sb.substring(0, sb.length() - 1);
            }
        }
        return null;
    }

    private String getWhitelistTopics() {
        String whitelistTopics = null;
        try {
            String filter = "(service.pid=org.apache.sling.event.impl.jobs.JobConsumerManager)";
            Configuration[] configurations = this.configurationAdmin.listConfigurations(filter);
            if (configurations != null) {
                for (Configuration c : configurations) {
                    Dictionary properties = c.getProperties();
                    String[] topicsArray = PropertiesUtil.toStringArray(properties.get("job.consumermanager.whitelist"));
                    if (topicsArray == null) continue;
                    StringBuilder sb = new StringBuilder();
                    for (String topic : topicsArray) {
                        sb.append(topic).append(",");
                    }
                    if (sb.length() <= 0) continue;
                    whitelistTopics = sb.substring(0, sb.length() - 1);
                }
            }
        }
        catch (Exception e) {
            LOG.error("Unable to retrieve whitelist topics.", (Throwable)e);
        }
        return whitelistTopics;
    }

    private String getBlacklistTopics() {
        String blacklistTopics = null;
        try {
            String filter = "(service.pid=org.apache.sling.event.impl.jobs.JobConsumerManager)";
            Configuration[] configurations = this.configurationAdmin.listConfigurations(filter);
            if (configurations != null) {
                for (Configuration c : configurations) {
                    Dictionary properties = c.getProperties();
                    String[] topicsArray = PropertiesUtil.toStringArray(properties.get("job.consumermanager.blacklist"));
                    if (topicsArray == null) continue;
                    StringBuilder sb = new StringBuilder();
                    for (String topic : topicsArray) {
                        sb.append(topic).append(",");
                    }
                    if (sb.length() <= 0) continue;
                    blacklistTopics = sb.substring(0, sb.length() - 1);
                }
            }
        }
        catch (Exception e) {
            LOG.error("Unable to retrieve blacklist topics.", (Throwable)e);
        }
        return blacklistTopics;
    }

    protected void activate(ComponentContext componentContext) {
        this.componentContext = componentContext;
    }

    protected void deactivate(ComponentContext componentContext) {
        if (this.componentContext != null) {
            this.componentContext = null;
        }
    }

    protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
        this.configurationAdmin = configurationAdmin;
    }

    protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
        if (this.configurationAdmin == configurationAdmin) {
            this.configurationAdmin = null;
        }
    }

    protected void bindJobConsumerInventoryService(JobConsumerInventoryService jobConsumerInventoryService) {
        this.jobConsumerInventoryService = jobConsumerInventoryService;
    }

    protected void unbindJobConsumerInventoryService(JobConsumerInventoryService jobConsumerInventoryService) {
        if (this.jobConsumerInventoryService == jobConsumerInventoryService) {
            this.jobConsumerInventoryService = null;
        }
    }
}