MobilePlatformAdapterFactory.java 6.03 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Activate
 *  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.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl;

import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.impl.MobilePlatformInternalAdapterFactory;
import com.adobe.cq.mobile.platform.impl.MobileResourceImpl;
import com.adobe.cq.mobile.platform.impl.MobileResourceLocatorFactory;
import com.adobe.cq.mobile.platform.impl.MobileResourceLocatorImpl;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
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.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(label="%mobile.platform.adapterfactory.label", description="%mobile.platform.adapterfactory.description", metatype=0)
@Service
public class MobilePlatformAdapterFactory
implements AdapterFactory {
    private static Logger log = LoggerFactory.getLogger(MobilePlatformAdapterFactory.class);
    private static final Class<MobileResource> MOBILE_RESOURCE_CLASS = MobileResource.class;
    private static final Class<MobileResourceLocator> MOBILE_RESOURCE_LOCATOR_CLASS = MobileResourceLocator.class;
    private ComponentContext context;
    @Reference
    private MobilePlatformInternalAdapterFactory mobilePlatformInternalAdapterFactory;
    @Reference(cardinality=ReferenceCardinality.MANDATORY_UNARY, policy=ReferencePolicy.STATIC)
    private MobileResourceLocatorFactory mobileResourceLocatorFactory;
    @Property(name="adapters", propertyPrivate=1)
    private static final String[] ADAPTER_CLASSES = new String[]{MOBILE_RESOURCE_CLASS.getName(), MOBILE_RESOURCE_LOCATOR_CLASS.getName()};
    @Property(name="adaptables", propertyPrivate=1)
    private static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName(), ResourceResolver.class.getName()};
    @Property(label="%mobile.platform.adapterfactory.instancemapping.label", description="mobile.platform.adapterfactory.instancemapping.description", value={"app-instance=mobileapps/core/components/instance", "app-group=mobileapps/core/components/group"})
    private static final String LEGACY_INSTANCE_MAPPING = "cq.mobile.platform.instancemapping";
    private Map<String, String> legacyTypeMap = new HashMap<String, String>();

    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_RESOURCE_CLASS == type) {
            return (AdapterType)new MobileResourceImpl(resource, this.legacyTypeMap, this.mobilePlatformInternalAdapterFactory);
        }
        return null;
    }

    private <AdapterType> AdapterType getAdapter(ResourceResolver resolver, Class<AdapterType> type) {
        if (MOBILE_RESOURCE_LOCATOR_CLASS == type) {
            return (AdapterType)this.mobileResourceLocatorFactory.getMobileResourceLocator(resolver);
        }
        return null;
    }

    @Activate
    protected void activate(ComponentContext ctx) {
        Dictionary props = ctx.getProperties();
        if (props.get("cq.mobile.platform.instancemapping") != null) {
            String[] mappings = PropertiesUtil.toStringArray(props.get("cq.mobile.platform.instancemapping"));
            for (int i = 0; i < mappings.length; ++i) {
                String[] map = mappings[i].split("=");
                if (map.length < 2) continue;
                this.legacyTypeMap.put(map[0], map[1]);
            }
        }
    }

    protected void bindMobilePlatformInternalAdapterFactory(MobilePlatformInternalAdapterFactory mobilePlatformInternalAdapterFactory) {
        this.mobilePlatformInternalAdapterFactory = mobilePlatformInternalAdapterFactory;
    }

    protected void unbindMobilePlatformInternalAdapterFactory(MobilePlatformInternalAdapterFactory mobilePlatformInternalAdapterFactory) {
        if (this.mobilePlatformInternalAdapterFactory == mobilePlatformInternalAdapterFactory) {
            this.mobilePlatformInternalAdapterFactory = null;
        }
    }

    protected void bindMobileResourceLocatorFactory(MobileResourceLocatorFactory mobileResourceLocatorFactory) {
        this.mobileResourceLocatorFactory = mobileResourceLocatorFactory;
    }

    protected void unbindMobileResourceLocatorFactory(MobileResourceLocatorFactory mobileResourceLocatorFactory) {
        if (this.mobileResourceLocatorFactory == mobileResourceLocatorFactory) {
            this.mobileResourceLocatorFactory = null;
        }
    }
}