EmulatorAdapterFactory.java 3.26 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.components.Component
 *  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.apache.sling.api.resource.ResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.emulator.impl;

import com.day.cq.wcm.emulator.Emulator;
import com.day.cq.wcm.emulator.impl.EmulatorImpl;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
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.ResourceUtil;
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)
@Service(value={AdapterFactory.class})
@Properties(value={@Property(name="service.description", value={"Default Emulator Adapter"})})
public class EmulatorAdapterFactory
implements AdapterFactory {
    private static final Logger log = LoggerFactory.getLogger(EmulatorAdapterFactory.class);
    private static final Class<Emulator> EMULATOR_CLASS = Emulator.class;
    @Property(name="adapters")
    public static final String[] ADAPTER_CLASSES = new String[]{EMULATOR_CLASS.getName()};
    @Property(name="adaptables")
    public static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName()};

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (adaptable instanceof Resource) {
            return this.getAdapter((Resource)adaptable, type);
        }
        if (adaptable instanceof com.day.cq.wcm.api.components.Component) {
            return this.getAdapter((com.day.cq.wcm.api.components.Component)adaptable, type);
        }
        log.warn("Unable to handle adaptable [{}]", (Object)adaptable.getClass().getName());
        return null;
    }

    private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
        if (EMULATOR_CLASS == type && EmulatorAdapterFactory.isEmulator(resource)) {
            return (AdapterType)new EmulatorImpl(resource);
        }
        log.debug("Unable to adapt resource of [{}] to type [{}]", (Object)resource.getResourceType(), (Object)type.getName());
        return null;
    }

    private <AdapterType> AdapterType getAdapter(com.day.cq.wcm.api.components.Component component, Class<AdapterType> type) {
        if (EMULATOR_CLASS == type) {
            return (AdapterType)component.getLocalResource(".").adaptTo(Emulator.class);
        }
        log.debug("Unable to adapt component [{}] to type [{}]", (Object)component.getResourceType(), (Object)type.getName());
        return null;
    }

    private static boolean isEmulator(Resource resource) {
        return ResourceUtil.isA((Resource)resource, (String)"cq:Component") && ResourceUtil.isA((Resource)resource, (String)"wcm/emulator/components/base");
    }
}