ServiceAdapterFactory.java 2.86 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.webservicesupport.impl;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Service;
import com.day.cq.wcm.webservicesupport.impl.ServiceImpl;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@org.apache.felix.scr.annotations.Service(value={AdapterFactory.class})
@Properties(value={@Property(name="service.description", value={"Default Service Adapter"})})
public class ServiceAdapterFactory
implements AdapterFactory {
    private final Logger log;
    private static final Class<Resource> RESOURCE_CLASS = Resource.class;
    private static final Class<Page> PAGE_CLASS = Page.class;
    private static final Class<Service> SERVICE_CLASS = Service.class;
    @Property(name="adapters")
    protected static final String[] ADAPTER_CLASSES = new String[]{SERVICE_CLASS.getName()};
    @Property(name="adaptables")
    protected static final String[] ADAPTABLE_CLASSES = new String[]{RESOURCE_CLASS.getName(), PAGE_CLASS.getName()};

    public ServiceAdapterFactory() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (adaptable instanceof Resource) {
            return this.getAdapter((Resource)adaptable, type);
        }
        if (adaptable instanceof Page) {
            return this.getAdapter((Page)adaptable, type);
        }
        this.log.warn("Unable to handle adaptable {}", (Object)adaptable.getClass().getName());
        return null;
    }

    private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
        if (type == SERVICE_CLASS && resource.getChild("jcr:content") != null) {
            return (AdapterType)new ServiceImpl(resource);
        }
        this.log.debug("Unable to adapt to resource of type {}", (Object)type.getName());
        return null;
    }

    private <AdapterType> AdapterType getAdapter(Page page, Class<AdapterType> type) {
        if (type == SERVICE_CLASS) {
            return (AdapterType)new ServiceImpl(page.getContentResource());
        }
        this.log.debug("Unable to adapt to resource of type {}", (Object)type.getName());
        return null;
    }
}