SitecatalystAdapterFactory.java 5.84 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  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.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.sitecatalyst.impl;

import com.day.cq.analytics.impl.AnalyticsComponent;
import com.day.cq.analytics.impl.AnalyticsComponentService;
import com.day.cq.analytics.sitecatalyst.Framework;
import com.day.cq.analytics.sitecatalyst.FrameworkComponent;
import com.day.cq.analytics.sitecatalyst.impl.FrameworkImpl;
import java.lang.reflect.Constructor;
import java.util.Dictionary;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
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.Reference;
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.api.resource.ValueMap;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, label="Adobe AEM Analytics Adapter Factory", description="Provides adapter functionality for Analytics objects")
@Service
@Properties(value={@Property(name="service.description", value={"%cq.analytics.adapterfactory.name"})})
public class SitecatalystAdapterFactory
implements AdapterFactory {
    private static final Logger log = LoggerFactory.getLogger(SitecatalystAdapterFactory.class);
    private static final Class<Framework> FRAMEWORK_CLASS = Framework.class;
    private static final Class<FrameworkComponent> FRAMEWORKCOMPONENT_CLASS = FrameworkComponent.class;
    @Property(name="adapters", propertyPrivate=1)
    public static final String[] ADAPTER_CLASSES = new String[]{FRAMEWORKCOMPONENT_CLASS.getName(), FRAMEWORK_CLASS.getName()};
    @Property(name="adaptables", propertyPrivate=1)
    public static final String[] ADAPTABLE_CLASSES = new String[]{ResourceResolver.class.getName(), Resource.class.getName()};
    @Property(label="Context stores", description="Context stores exposed for instrumentation.", value={"activitystream", "eventdata", "geolocation", "mouseposition", "pagedata", "profile", "resolvedsegments", "socialgraph", "surferinfo", "tagcloud"})
    private static final String CONTEXT_STORES = "cq.analytics.adapterfactory.contextstores";
    private String[] contextStores = new String[0];
    @Reference
    private AnalyticsComponentService componentService;

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (adaptable instanceof ResourceResolver) {
            return this.getAdapter((Object)((ResourceResolver)adaptable), type);
        }
        if (adaptable instanceof Resource) {
            return this.getAdapter((Resource)adaptable, type);
        }
        return null;
    }

    private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
        if (type == FRAMEWORK_CLASS && resource != null) {
            return (AdapterType)new FrameworkImpl(resource);
        }
        if (type == FRAMEWORKCOMPONENT_CLASS && resource != null) {
            ValueMap mainProps = (ValueMap)resource.adaptTo(ValueMap.class);
            String componentPath = (String)mainProps.get("cq:componentPath", (Object)"");
            if ("".equals(componentPath)) {
                return null;
            }
            AnalyticsComponent component = this.componentService.getAnalyticsComponent(componentPath);
            if (component == null) {
                return null;
            }
            String mappingComponent = component.getMappingComponent();
            mappingComponent = StringUtils.capitalize((String)mappingComponent);
            try {
                Class c = Class.forName("com.day.cq.analytics.sitecatalyst.impl." + mappingComponent + "FrameworkComponentImpl");
                return (AdapterType)c.getConstructor(Resource.class, String[].class, AnalyticsComponentService.class).newInstance(new Object[]{resource, this.contextStores, this.componentService});
            }
            catch (Exception e) {
                log.error("Unable to adapt resource of {} to type {}", (Object)resource.getResourceType(), (Object)type.getName());
                log.debug("Unable to adapt resource of {} to type {}", (Object)resource.getResourceType(), (Object)type.getName());
                return null;
            }
        }
        log.debug("Unable to adapt resource {} to type {}", (Object)resource, type);
        return null;
    }

    @Activate
    protected void activate(ComponentContext ctx) {
        Dictionary dict = ctx.getProperties();
        this.contextStores = PropertiesUtil.toStringArray(dict.get("cq.analytics.adapterfactory.contextstores"));
    }

    protected void bindComponentService(AnalyticsComponentService analyticsComponentService) {
        this.componentService = analyticsComponentService;
    }

    protected void unbindComponentService(AnalyticsComponentService analyticsComponentService) {
        if (this.componentService == analyticsComponentService) {
            this.componentService = null;
        }
    }
}