MobilePlatformInternalAdapterFactory.java 9.56 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.contentsync.handler.util.RequestResponseFactory
 *  org.apache.felix.scr.annotations.Component
 *  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.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.engine.SlingRequestProcessor
 *  org.osgi.framework.ServiceReference
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl;

import com.adobe.cq.mobile.platform.impl.FrameworkContentExporter;
import com.adobe.cq.mobile.platform.impl.MobileAppProvider;
import com.adobe.cq.mobile.platform.impl.MobileAppProviderFactory;
import com.day.cq.contentsync.handler.util.RequestResponseFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.felix.scr.annotations.Component;
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.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.engine.SlingRequestProcessor;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={MobilePlatformInternalAdapterFactory.class})
@Property(name="service.description", value={"AEM Mobile Platform Adapter Factory"})
public class MobilePlatformInternalAdapterFactory {
    private static Logger log = LoggerFactory.getLogger(MobilePlatformInternalAdapterFactory.class);
    private static final Class<FrameworkContentExporter> FRAMEWORK_CONTENT_EXPORTER_CLASS = FrameworkContentExporter.class;
    private static final Class<MobileAppProvider> MOBILE_APP_PROVIDER_CLASS = MobileAppProvider.class;
    private ComponentContext context;
    @Property(name="adapters", propertyPrivate=1)
    private static final String[] ADAPTER_CLASSES = new String[]{FRAMEWORK_CONTENT_EXPORTER_CLASS.getName(), MOBILE_APP_PROVIDER_CLASS.getName()};
    @Property(name="adaptables", propertyPrivate=1)
    private static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName(), ResourceResolver.class.getName()};
    @Reference
    RequestResponseFactory requestResponseFactory;
    @Reference
    SlingRequestProcessor requestProcessor;
    @Reference(name="Factory", referenceInterface=MobileAppProviderFactory.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
    private List<ServiceReference> unhandledFactories = new ArrayList<ServiceReference>();
    private SortedMap<ServiceReference, MobileAppProviderFactory> factories;

    public MobilePlatformInternalAdapterFactory() {
        this.factories = new TreeMap<ServiceReference, MobileAppProviderFactory>(new Comparator<ServiceReference>(){

            @Override
            public int compare(ServiceReference ref1, ServiceReference ref2) {
                int rank2;
                long id2;
                long id1 = MobilePlatformInternalAdapterFactory.this.getServiceID(ref1);
                if (id1 == (id2 = MobilePlatformInternalAdapterFactory.this.getServiceID(ref2))) {
                    return 0;
                }
                int rank1 = MobilePlatformInternalAdapterFactory.this.getServiceRanking(ref1);
                if (rank1 == (rank2 = MobilePlatformInternalAdapterFactory.this.getServiceRanking(ref2))) {
                    return (int)(id1 - id2);
                }
                return rank2 - rank1;
            }
        });
    }

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (adaptable instanceof Resource) {
            return this.getAdapter((Resource)adaptable, type);
        }
        if (adaptable instanceof ResourceResolver) {
            return this.getAdapter((ResourceResolver)adaptable, type);
        }
        log.info("Can't adapt {} to {}", (Object)(adaptable == null ? "null" : adaptable.getClass().getName()), type);
        return null;
    }

    private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
        if (MOBILE_APP_PROVIDER_CLASS == type) {
            return (AdapterType)this.findAppProvider(resource);
        }
        return null;
    }

    private <AdapterType> AdapterType getAdapter(ResourceResolver resolver, Class<AdapterType> type) {
        if (FRAMEWORK_CONTENT_EXPORTER_CLASS == type) {
            return (AdapterType)new FrameworkContentExporter(this.requestResponseFactory, this.requestProcessor, resolver);
        }
        return null;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected MobileAppProvider findAppProvider(Resource resource) {
        SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
        synchronized (sortedMap) {
            for (MobileAppProviderFactory factory : this.factories.values()) {
                MobileAppProvider appProvider = factory.getMobileAppProvider(resource);
                if (appProvider == null) continue;
                return appProvider;
            }
        }
        return null;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void activate(ComponentContext context) {
        SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
        synchronized (sortedMap) {
            this.context = context;
            for (ServiceReference reference : this.unhandledFactories) {
                this.registerAppFactory(reference);
            }
            this.unhandledFactories.clear();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void deactivate(ComponentContext context) {
        SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
        synchronized (sortedMap) {
            this.context = null;
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void bindFactory(ServiceReference reference) {
        SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
        synchronized (sortedMap) {
            if (this.context == null) {
                this.unhandledFactories.add(reference);
            } else {
                this.registerAppFactory(reference);
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void unbindFactory(ServiceReference reference) {
        SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
        synchronized (sortedMap) {
            this.unregisterAppFactory(reference);
            this.unhandledFactories.remove((Object)reference);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void registerAppFactory(ServiceReference reference) {
        MobileAppProviderFactory factory = (MobileAppProviderFactory)this.context.locateService("Factory", reference);
        if (factory != null) {
            SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
            synchronized (sortedMap) {
                this.factories.put(reference, factory);
                log.info("App Provider Factories in order: " + this.factories.values().toString());
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void unregisterAppFactory(ServiceReference reference) {
        SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
        synchronized (sortedMap) {
            this.factories.remove((Object)reference);
        }
    }

    private long getServiceID(ServiceReference reference) {
        return (Long)reference.getProperty("service.id");
    }

    private int getServiceRanking(ServiceReference reference) {
        Object obj = reference.getProperty("service.ranking");
        if (obj == null) {
            return 0;
        }
        if (obj instanceof Integer) {
            return (Integer)obj;
        }
        log.warn("Component " + reference.getProperty("component.name") + " has a non-Integer '" + "service.ranking" + "' property");
        return 0;
    }

    protected void bindRequestResponseFactory(RequestResponseFactory requestResponseFactory) {
        this.requestResponseFactory = requestResponseFactory;
    }

    protected void unbindRequestResponseFactory(RequestResponseFactory requestResponseFactory) {
        if (this.requestResponseFactory == requestResponseFactory) {
            this.requestResponseFactory = null;
        }
    }

    protected void bindRequestProcessor(SlingRequestProcessor slingRequestProcessor) {
        this.requestProcessor = slingRequestProcessor;
    }

    protected void unbindRequestProcessor(SlingRequestProcessor slingRequestProcessor) {
        if (this.requestProcessor == slingRequestProcessor) {
            this.requestProcessor = null;
        }
    }

}