CommonAdapterFactory.java
3.55 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Component
* 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.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.commons.impl;
import com.day.cq.commons.Externalizer;
import com.day.cq.commons.JcrLabeledResource;
import com.day.cq.commons.LabeledResource;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
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.Service;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={AdapterFactory.class})
public class CommonAdapterFactory
implements AdapterFactory {
private final Logger log;
private static final Class<LabeledResource> LR_CLASS = LabeledResource.class;
private static final Class<Externalizer> EXTERNALIZER_CLASS = Externalizer.class;
@Reference
private Externalizer externalizer;
@Property(name="adapters")
private static final String[] ADAPTER_CLASSES = new String[]{LR_CLASS.getName(), EXTERNALIZER_CLASS.getName()};
@Property(name="adaptables")
private static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName(), ResourceResolver.class.getName()};
public CommonAdapterFactory() {
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 ResourceResolver) {
return this.getAdapter((ResourceResolver)adaptable, type);
}
this.log.warn("Unable to handle adaptable {}", (Object)adaptable.getClass().getName());
return null;
}
private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
Node node;
if (type == LR_CLASS && (node = (Node)resource.adaptTo(Node.class)) != null) {
try {
return (AdapterType)new JcrLabeledResource(node);
}
catch (RepositoryException e) {
this.log.error("Error while creating JcrLabeledResource.", (Throwable)e);
}
}
this.log.debug("Unable to adapt resource of {} to type {}", (Object)resource.getResourceType(), (Object)type.getName());
return null;
}
private <AdapterType> AdapterType getAdapter(ResourceResolver resource, Class<AdapterType> type) {
if (type == EXTERNALIZER_CLASS) {
return (AdapterType)this.externalizer;
}
this.log.debug("Unable to adapt resource resolver to type {}", (Object)type.getName());
return null;
}
protected void bindExternalizer(Externalizer externalizer) {
this.externalizer = externalizer;
}
protected void unbindExternalizer(Externalizer externalizer) {
if (this.externalizer == externalizer) {
this.externalizer = null;
}
}
}