ConfigurationAdapterFactory.java
3.25 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* 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.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.webservicesupport.impl;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.impl.ConfigurationImpl;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={AdapterFactory.class})
@Properties(value={@Property(name="service.description", value={"Default Configurations Adapter"})})
public class ConfigurationAdapterFactory
implements AdapterFactory {
private final Logger log;
private static final Class<Resource> RESOURCE_CLASS = Resource.class;
private static final Class<Page> PAGE_CLASS = Page.class;
private static final Class<Configuration> CONFIGURATION_CLASS = Configuration.class;
@Property(name="adapters")
protected static final String[] ADAPTER_CLASSES = new String[]{CONFIGURATION_CLASS.getName()};
@Property(name="adaptables")
protected static final String[] ADAPTABLE_CLASSES = new String[]{RESOURCE_CLASS.getName(), PAGE_CLASS.getName()};
public ConfigurationAdapterFactory() {
this.log = LoggerFactory.getLogger(this.getClass());
}
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (adaptable instanceof Resource) {
return this.getAdapter((Resource)adaptable, type);
}
if (adaptable instanceof Page) {
return this.getAdapter((Page)adaptable, type);
}
this.log.warn("Unable to handle adaptable {}", (Object)adaptable.getClass().getName());
return null;
}
private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
if (type != CONFIGURATION_CLASS) {
return null;
}
if (resource.getChild("jcr:content") != null && resource.getChild("jcr:content").isResourceType("cq/cloudserviceconfigs/components/configpage")) {
return (AdapterType)new ConfigurationImpl(resource);
}
if ("public".equals(resource.getName()) && resource.getParent() == null) {
return (AdapterType)new ConfigurationImpl(resource);
}
this.log.debug("Unable to adapt to resource of type {}", (Object)type.getName());
return null;
}
private <AdapterType> AdapterType getAdapter(Page page, Class<AdapterType> type) {
if (type == CONFIGURATION_CLASS) {
return (AdapterType)new ConfigurationImpl(page.getContentResource());
}
this.log.debug("Unable to adapt to resource of type {}", (Object)type.getName());
return null;
}
}