MCMAdapterFactory.java
6.59 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* 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.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.adapter.AdapterFactory
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.mcm.impl;
import com.day.cq.mcm.api.Brand;
import com.day.cq.mcm.api.Campaign;
import com.day.cq.mcm.api.Experience;
import com.day.cq.mcm.api.MCMFacade;
import com.day.cq.mcm.api.MCMPlugin;
import com.day.cq.mcm.api.MCMResourceType;
import com.day.cq.mcm.api.Touchpoint;
import com.day.cq.mcm.impl.BrandImpl;
import com.day.cq.mcm.impl.CampaignImpl;
import com.day.cq.mcm.util.NormalizedResource;
import com.day.cq.wcm.api.Page;
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.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={AdapterFactory.class})
@Properties(value={@Property(name="service.description", value={"MCM Adapter Factory, adapts to MCM classes."}), @Property(name="adaptables", value={"org.apache.sling.api.resource.Resource", "com.day.cq.wcm.api.Page"}), @Property(name="adapters", value={"com.day.cq.mcm.api.Campaign", "com.day.cq.mcm.api.Brand", "com.day.cq.mcm.api.Experience", "com.day.cq.mcm.api.Touchpoint"})})
public class MCMAdapterFactory
implements AdapterFactory {
private static final Logger log = LoggerFactory.getLogger(MCMAdapterFactory.class);
@Reference
private MCMFacade mcmFacade = null;
/*
* Enabled aggressive block sorting
*/
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
log.debug("Adapting {} to {}", adaptable, type);
Resource resource = null;
Page page = null;
if (adaptable instanceof Page) {
page = (Page)adaptable;
resource = (Resource)page.adaptTo(Resource.class);
} else {
if (!(adaptable instanceof Resource)) {
log.warn("Could not adapt '" + adaptable + "' to " + type + ". ");
return null;
}
resource = (Resource)adaptable;
}
NormalizedResource normalizedResource = new NormalizedResource();
if (resource != null) {
normalizedResource.setResource(resource);
}
if (type.isAssignableFrom(Experience.class)) {
if (resource == null) {
log.warn("Could not adapt '" + adaptable + "' to Experience. " + "Only Resource and Page can be adapted.");
return null;
}
if (!this.verifyMCMType(normalizedResource, MCMResourceType.Experience)) {
log.warn("Cannot adapt {} to Experience, not a known experience resource type.", adaptable);
return null;
}
MCMPlugin p = this.mcmFacade.getPlugin(resource);
if (p != null) return (AdapterType)p.makeExperience(resource);
log.warn("Cannot adapt {} to {}, no plugin for it registered.", adaptable, type);
return null;
}
if (type.isAssignableFrom(Touchpoint.class)) {
if (resource == null) {
log.warn("Could not adapt '" + adaptable + "' to Touchpoint. " + "Only Resource and Page can be adapted.");
return null;
}
MCMPlugin p = this.mcmFacade.getPlugin(resource);
if (!this.verifyMCMType(normalizedResource, MCMResourceType.Touchpoint)) {
log.warn("Cannot adapt {} to Touchpoint, not a known touchpoint resource type.", adaptable);
return null;
}
if (p != null) return (AdapterType)p.makeTouchpoint(resource);
log.warn("Cannot adapt {} to {}, no plugin for it registered.", adaptable, type);
return null;
}
if (type == Campaign.class) {
if (resource == null) {
log.warn("Could not adapt '" + adaptable + "' to Campaign. " + "Only Resource and Page can be adapted.");
return null;
}
if (!this.verifyType(normalizedResource, "cq/personalization/components/campaignpage")) {
log.warn("Could not adapt '" + adaptable + "' to Campaign. " + "ComponentType not matching.");
return null;
}
CampaignImpl ci = new CampaignImpl();
ci.setNormalizedResource(normalizedResource);
return (AdapterType)ci;
}
if (type != Brand.class) {
log.error("Wrong component config or framework problem: Cannot adapt to '" + type + "'.");
return null;
}
if (resource == null) {
log.warn("Could not adapt '" + adaptable + "' to Brand. " + "Only Resource and Page can be adapted.");
return null;
}
if (!this.verifyType(normalizedResource, "mcm/components/brandpage")) {
log.warn("Could not adapt '" + adaptable + "' to Brand. " + "ComponentType not matching.");
return null;
}
BrandImpl bi = new BrandImpl();
bi.setNormalizedResource(normalizedResource);
return (AdapterType)bi;
}
private boolean verifyMCMType(NormalizedResource normalizedResource, MCMResourceType mcmType) {
ValueMap contentVals = normalizedResource.getContentVals();
String resType = contentVals != null ? (String)contentVals.get("sling:resourceType", String.class) : null;
return mcmType.equals((Object)this.mcmFacade.getMCMType(resType));
}
private boolean verifyType(NormalizedResource nr, String compType) {
ValueMap contentVals = nr.getContentVals();
if (contentVals != null) {
String checkedType = (String)contentVals.get("sling:resourceType", String.class);
return compType.equals(checkedType);
}
return false;
}
protected void bindMcmFacade(MCMFacade mCMFacade) {
this.mcmFacade = mCMFacade;
}
protected void unbindMcmFacade(MCMFacade mCMFacade) {
if (this.mcmFacade == mCMFacade) {
this.mcmFacade = null;
}
}
}