DPSBannerAdapterFactory.java 4.55 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.adapter.AdapterFactory
 *  org.apache.sling.api.adapter.AdapterManager
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.dps.impl.metadata;

import com.adobe.cq.mobile.dps.DPSBanner;
import com.adobe.cq.mobile.dps.impl.metadata.DPSBannerImpl;
import com.adobe.cq.mobile.dps.impl.utils.DPSUtil;
import com.day.cq.wcm.api.Page;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.Service;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.adapter.AdapterManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0, label="%media.publishing.dpsBanner.adapterfactory.service.name", description="%media.publishing.dpsBanner.adapterfactory.service.description")
@Service
public class DPSBannerAdapterFactory
implements AdapterFactory {
    private static Logger log = LoggerFactory.getLogger(DPSBannerAdapterFactory.class);
    @Property(name="adapters", propertyPrivate=1)
    private static final String[] ADAPTER_CLASSES = new String[]{DPSBanner.class.getName()};
    @Property(name="adaptables", propertyPrivate=1)
    private static final String[] ADAPTABLE_CLASSES = new String[]{Page.class.getName(), Node.class.getName(), Resource.class.getName()};
    @Reference
    private AdapterManager adapterManager;
    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        try {
            if (adaptable instanceof Node) {
                Node node = (Node)adaptable;
                ResourceResolver resourceResolver = DPSUtil.getResourceResolver(this.resourceResolverFactory, node.getSession());
                Resource adaptableResource = resourceResolver.getResource(node.getPath());
                if (adaptableResource != null) {
                    return this.getAdapter((Object)adaptableResource, type);
                }
            } else {
                if (adaptable instanceof Resource) {
                    Page page = (Page)((Resource)adaptable).adaptTo(Page.class);
                    return this.getAdapter(page, type);
                }
                if (adaptable instanceof Page) {
                    return this.getAdapter((Page)adaptable, type);
                }
            }
        }
        catch (RepositoryException e) {
            log.warn("Can't adapt {} to {}", (Object)(adaptable == null ? "null" : adaptable.getClass().getName()), type);
        }
        log.info("Can't adapt {} to {}", (Object)(adaptable == null ? "null" : adaptable.getClass().getName()), type);
        return null;
    }

    private <AdapterType> AdapterType getAdapter(Page page, Class<AdapterType> type) {
        if (DPSUtil.isADPSResource(page) && DPSUtil.getDPSResourceType(page).equals("dps:Banner") && DPSBanner.class == type) {
            DPSBannerImpl implBanner = new DPSBannerImpl(page);
            return (AdapterType)implBanner;
        }
        return null;
    }

    protected void bindAdapterManager(AdapterManager adapterManager) {
        this.adapterManager = adapterManager;
    }

    protected void unbindAdapterManager(AdapterManager adapterManager) {
        if (this.adapterManager == adapterManager) {
            this.adapterManager = null;
        }
    }

    protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resourceResolverFactory = resourceResolverFactory;
    }

    protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resourceResolverFactory == resourceResolverFactory) {
            this.resourceResolverFactory = null;
        }
    }
}