MobilePlatformAdapterFactory.java
6.03 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
121
122
123
124
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Activate
* 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.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.impl.MobilePlatformInternalAdapterFactory;
import com.adobe.cq.mobile.platform.impl.MobileResourceImpl;
import com.adobe.cq.mobile.platform.impl.MobileResourceLocatorFactory;
import com.adobe.cq.mobile.platform.impl.MobileResourceLocatorImpl;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
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.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="%mobile.platform.adapterfactory.label", description="%mobile.platform.adapterfactory.description", metatype=0)
@Service
public class MobilePlatformAdapterFactory
implements AdapterFactory {
private static Logger log = LoggerFactory.getLogger(MobilePlatformAdapterFactory.class);
private static final Class<MobileResource> MOBILE_RESOURCE_CLASS = MobileResource.class;
private static final Class<MobileResourceLocator> MOBILE_RESOURCE_LOCATOR_CLASS = MobileResourceLocator.class;
private ComponentContext context;
@Reference
private MobilePlatformInternalAdapterFactory mobilePlatformInternalAdapterFactory;
@Reference(cardinality=ReferenceCardinality.MANDATORY_UNARY, policy=ReferencePolicy.STATIC)
private MobileResourceLocatorFactory mobileResourceLocatorFactory;
@Property(name="adapters", propertyPrivate=1)
private static final String[] ADAPTER_CLASSES = new String[]{MOBILE_RESOURCE_CLASS.getName(), MOBILE_RESOURCE_LOCATOR_CLASS.getName()};
@Property(name="adaptables", propertyPrivate=1)
private static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName(), ResourceResolver.class.getName()};
@Property(label="%mobile.platform.adapterfactory.instancemapping.label", description="mobile.platform.adapterfactory.instancemapping.description", value={"app-instance=mobileapps/core/components/instance", "app-group=mobileapps/core/components/group"})
private static final String LEGACY_INSTANCE_MAPPING = "cq.mobile.platform.instancemapping";
private Map<String, String> legacyTypeMap = new HashMap<String, String>();
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);
}
log.info("Can't adapt {} to {}", (Object)(adaptable == null ? "null" : adaptable.getClass().getName()), type);
return null;
}
private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
if (MOBILE_RESOURCE_CLASS == type) {
return (AdapterType)new MobileResourceImpl(resource, this.legacyTypeMap, this.mobilePlatformInternalAdapterFactory);
}
return null;
}
private <AdapterType> AdapterType getAdapter(ResourceResolver resolver, Class<AdapterType> type) {
if (MOBILE_RESOURCE_LOCATOR_CLASS == type) {
return (AdapterType)this.mobileResourceLocatorFactory.getMobileResourceLocator(resolver);
}
return null;
}
@Activate
protected void activate(ComponentContext ctx) {
Dictionary props = ctx.getProperties();
if (props.get("cq.mobile.platform.instancemapping") != null) {
String[] mappings = PropertiesUtil.toStringArray(props.get("cq.mobile.platform.instancemapping"));
for (int i = 0; i < mappings.length; ++i) {
String[] map = mappings[i].split("=");
if (map.length < 2) continue;
this.legacyTypeMap.put(map[0], map[1]);
}
}
}
protected void bindMobilePlatformInternalAdapterFactory(MobilePlatformInternalAdapterFactory mobilePlatformInternalAdapterFactory) {
this.mobilePlatformInternalAdapterFactory = mobilePlatformInternalAdapterFactory;
}
protected void unbindMobilePlatformInternalAdapterFactory(MobilePlatformInternalAdapterFactory mobilePlatformInternalAdapterFactory) {
if (this.mobilePlatformInternalAdapterFactory == mobilePlatformInternalAdapterFactory) {
this.mobilePlatformInternalAdapterFactory = null;
}
}
protected void bindMobileResourceLocatorFactory(MobileResourceLocatorFactory mobileResourceLocatorFactory) {
this.mobileResourceLocatorFactory = mobileResourceLocatorFactory;
}
protected void unbindMobileResourceLocatorFactory(MobileResourceLocatorFactory mobileResourceLocatorFactory) {
if (this.mobileResourceLocatorFactory == mobileResourceLocatorFactory) {
this.mobileResourceLocatorFactory = null;
}
}
}