CFMAdapterFactory.java
4.89 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
125
126
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.cfm.ContentFragment
* com.adobe.cq.dam.cfm.ContentFragmentManager
* com.adobe.cq.dam.cfm.FragmentTemplate
* com.day.cq.dam.api.Asset
* 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.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.resource.collection.ResourceCollection
* org.apache.sling.resource.collection.ResourceCollectionManager
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.cfm.impl;
import com.adobe.cq.dam.cfm.ContentFragment;
import com.adobe.cq.dam.cfm.ContentFragmentManager;
import com.adobe.cq.dam.cfm.FragmentTemplate;
import com.adobe.cq.dam.cfm.impl.CFMUtils;
import com.adobe.cq.dam.cfm.impl.FragmentImpl;
import com.adobe.cq.dam.cfm.impl.FragmentTemplateImpl;
import com.adobe.cq.dam.cfm.impl.InvalidFragmentException;
import com.adobe.cq.dam.cfm.impl.InvalidTemplateException;
import com.day.cq.dam.api.Asset;
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.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
@Component(metatype=0)
public class CFMAdapterFactory
implements AdapterFactory {
@Reference
private ContentFragmentManager manager;
@Property(name="adapters")
public static final String[] ADAPTER_CLASSES = new String[]{ContentFragment.class.getName(), FragmentTemplate.class.getName()};
@Property(name="adaptables")
public static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName()};
private final Logger log;
@Reference
private ResourceCollectionManager collectionManager;
public CFMAdapterFactory() {
this.log = LoggerFactory.getLogger(this.getClass());
}
private ContentFragment fromResource(Resource resource) {
ResourceCollection associatedContent;
Asset asset = (Asset)resource.adaptTo(Asset.class);
if (asset == null) {
return null;
}
Resource assocContentResource = resource.getChild("associated");
ResourceCollection resourceCollection = associatedContent = assocContentResource != null ? this.collectionManager.getCollection(assocContentResource) : null;
if (associatedContent == null) {
try {
associatedContent = CFMUtils.createEmptyAssocContent(resource, this.collectionManager);
}
catch (PersistenceException pe) {
this.log.debug("Getting a content fragment failed due to missing associated content definition.");
return null;
}
}
try {
return new FragmentImpl(asset, associatedContent, false);
}
catch (InvalidFragmentException ife) {
this.log.debug("Getting a content fragment failed due to an invalid content structure.");
return null;
}
}
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (adaptable instanceof Resource) {
if (type == ContentFragment.class) {
return (AdapterType)this.fromResource((Resource)adaptable);
}
if (type == FragmentTemplate.class) {
try {
return (AdapterType)new FragmentTemplateImpl((Resource)adaptable);
}
catch (InvalidTemplateException ite) {
this.log.debug("Getting a content fragment template failed due to an invalid content structure.");
return null;
}
}
}
return null;
}
protected void bindManager(ContentFragmentManager contentFragmentManager) {
this.manager = contentFragmentManager;
}
protected void unbindManager(ContentFragmentManager contentFragmentManager) {
if (this.manager == contentFragmentManager) {
this.manager = null;
}
}
protected void bindCollectionManager(ResourceCollectionManager resourceCollectionManager) {
this.collectionManager = resourceCollectionManager;
}
protected void unbindCollectionManager(ResourceCollectionManager resourceCollectionManager) {
if (this.collectionManager == resourceCollectionManager) {
this.collectionManager = null;
}
}
}