SitecatalystAdapterFactory.java
5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* 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;
}
}
}