LaunchesAdapterFactory.java 4.49 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.launches.api.Launch
 *  com.adobe.cq.launches.api.LaunchException
 *  com.adobe.cq.launches.api.LaunchManager
 *  com.adobe.cq.launches.api.LaunchManagerFactory
 *  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.osgi.service.event.EventAdmin
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.launches.impl;

import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchException;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.wcm.launches.impl.LaunchImpl;
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.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0, label="%launches.adapterfactory.name", description="%launches.adapterfactory.description")
@Service
public class LaunchesAdapterFactory
implements AdapterFactory {
    private static Logger log = LoggerFactory.getLogger(LaunchesAdapterFactory.class);
    private static final Class<Launch> LAUNCH_CLASS = Launch.class;
    private static final Class<LaunchManager> LAUNCH_MANAGER_CLASS = LaunchManager.class;
    @Property(name="adapters")
    public static final String[] ADAPTER_CLASSES = new String[]{LAUNCH_CLASS.getName(), LAUNCH_MANAGER_CLASS.getName()};
    @Property(name="adaptables")
    public static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName(), ResourceResolver.class.getName()};
    @Reference(cardinality=ReferenceCardinality.MANDATORY_UNARY, policy=ReferencePolicy.STATIC)
    private EventAdmin eventAdmin;
    @Reference(cardinality=ReferenceCardinality.MANDATORY_UNARY, policy=ReferencePolicy.STATIC)
    private LaunchManagerFactory launchManagerFactory;

    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);
        }
        return null;
    }

    private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
        try {
            if (LAUNCH_CLASS == type) {
                return (AdapterType)new LaunchImpl(resource);
            }
        }
        catch (LaunchException e) {
            log.debug("Failed adapting resource [{}] to [" + type + "]: {}", (Object)resource.getPath(), (Object)e);
        }
        return null;
    }

    private <AdapterType> AdapterType getAdapter(ResourceResolver resolver, Class<AdapterType> type) {
        try {
            if (LAUNCH_MANAGER_CLASS == type) {
                return (AdapterType)this.launchManagerFactory.getLaunchManager(resolver);
            }
        }
        catch (LaunchException e) {
            log.debug("Failed adapting resource resolver [user: {}] to [" + type + "]: {}", (Object)resolver.getUserID(), (Object)e);
        }
        return null;
    }

    protected void bindEventAdmin(EventAdmin eventAdmin) {
        this.eventAdmin = eventAdmin;
    }

    protected void unbindEventAdmin(EventAdmin eventAdmin) {
        if (this.eventAdmin == eventAdmin) {
            this.eventAdmin = null;
        }
    }

    protected void bindLaunchManagerFactory(LaunchManagerFactory launchManagerFactory) {
        this.launchManagerFactory = launchManagerFactory;
    }

    protected void unbindLaunchManagerFactory(LaunchManagerFactory launchManagerFactory) {
        if (this.launchManagerFactory == launchManagerFactory) {
            this.launchManagerFactory = null;
        }
    }
}