RHWhiteBoard.java 4.72 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.asset.api.RenditionHandler
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Property
 *  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.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceReference
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.asset.core.impl;

import com.adobe.granite.asset.api.RenditionHandler;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
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.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(metatype=0, immediate=1)
@Property(name="service.description", value={"RenditionHandler White Board"})
@Service(value={RHWhiteBoard.class})
@Reference(name="renditionHandlerRef", referenceInterface=RenditionHandler.class, bind="bindRenditionHandlerRef", unbind="unbindRenditionHandlerRef", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
public class RHWhiteBoard {
    protected static final Logger log = LoggerFactory.getLogger(RHWhiteBoard.class);
    protected BundleContext bundleContext;
    public static final Map<String, RenditionHandler> renditionHandlers = new HashMap<String, RenditionHandler>();
    private List<ServiceReference> renditionHandlerReferences = new ArrayList<ServiceReference>();

    @Activate
    private void activate(BundleContext context, Map<String, Object> config) {
        this.bundleContext = context;
        for (ServiceReference ref : this.renditionHandlerReferences) {
            this.addHandler(ref);
        }
        this.renditionHandlerReferences.clear();
    }

    @Deactivate
    private void deactivate() {
        this.bundleContext = null;
    }

    protected void bindRenditionHandlerRef(ServiceReference ref) {
        if (this.bundleContext == null) {
            this.renditionHandlerReferences.add(ref);
        } else {
            this.addHandler(ref);
        }
    }

    protected void unbindRenditionHandlerRef(ServiceReference ref) {
        this.removeHandler(ref);
    }

    public static RenditionHandler getRenditionHandler(String id) {
        return renditionHandlers.get(id);
    }

    public static Iterator<? extends RenditionHandler> getAllRenditionHandler() {
        return renditionHandlers.values().iterator();
    }

    public static void setRenditionHandler(String id, RenditionHandler rh) {
        renditionHandlers.put(id, rh);
    }

    private void addHandler(ServiceReference ref) {
        RenditionHandler handler = (RenditionHandler)this.bundleContext.getService(ref);
        if (handler != null) {
            String id = (String)ref.getProperty("rendition.handler.id");
            if (StringUtils.isNotEmpty((String)id)) {
                RenditionHandler existingHandler = renditionHandlers.get(id);
                if (existingHandler == null) {
                    log.info("Adding Asset RenditionHandler [ {} ]", (Object)id);
                    renditionHandlers.put(id, handler);
                } else {
                    log.error("RenditionHandler cannot be added. There is an existing RenditionHandler with ID [ {} ]", (Object)id);
                }
            } else {
                log.warn("RenditionHandler [ " + handler.getClass().getName() + " ] cannot be registered, No " + "rendition.handler.id" + " defined.");
            }
        }
    }

    private void removeHandler(ServiceReference ref) {
        String id = (String)ref.getProperty("rendition.handler.id");
        if (StringUtils.isNotEmpty((String)id)) {
            log.info("Removing Asset RenditionHandler [ {} ]", (Object)id);
            renditionHandlers.remove(id);
            this.renditionHandlerReferences.remove((Object)ref);
        }
    }
}