BlueprintManagerFactoryImpl.java
9.66 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.msm.Blueprint
* com.day.cq.wcm.msm.api.Blueprint
* com.day.cq.wcm.msm.api.BlueprintManager
* com.day.cq.wcm.msm.api.BlueprintManagerFactory
* com.day.cq.wcm.msm.api.RolloutConfigManager
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.observation.EventIterator
* javax.jcr.observation.EventListener
* javax.jcr.observation.ObservationManager
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* 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.LoginException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.msm.Blueprint;
import com.day.cq.wcm.msm.api.BlueprintManager;
import com.day.cq.wcm.msm.api.BlueprintManagerFactory;
import com.day.cq.wcm.msm.api.RolloutConfigManager;
import com.day.cq.wcm.msm.impl.BlueprintImpl;
import com.day.cq.wcm.msm.impl.BlueprintManagerImpl;
import com.day.cq.wcm.msm.impl.RolloutConfigManagerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
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.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1, metatype=0)
@Service(value={BlueprintManagerFactory.class, AdapterFactory.class})
public class BlueprintManagerFactoryImpl
implements BlueprintManagerFactory,
AdapterFactory,
EventListener {
private final Logger log = LoggerFactory.getLogger(BlueprintManagerFactoryImpl.class);
static final String DEFAULT_BLUEPRINT_ROOT = "/etc/blueprints";
static final String DEFAULT_BLUEPRINT_RESOURCE_TYPE = "wcm/msm/components/blueprint";
@Property(value={"wcm/msm/components/blueprint"})
private static final String BLUEPRINT_RESOURCE_TYPE = "cq.wcm.msm.blueprint.resource_type";
@Property(value={"/etc/blueprints"}, propertyPrivate=1)
private static final String BLUEPRINT_ROOT = "cq.wcm.msm.blueprint.root";
@Property(name="adapters")
public static final String[] ADAPTER_CLASSES = new String[]{com.day.cq.wcm.msm.api.Blueprint.class.getName(), Blueprint.class.getName(), BlueprintManager.class.getName()};
@Property(name="adaptables")
public static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName(), ResourceResolver.class.getName()};
@Reference
private ResourceResolverFactory resolverFactory = null;
@Reference
private RolloutConfigManagerFactory rolloutConfigMgrFactory = null;
private Lock lock = new ReentrantLock();
private List<String> guardedPath;
private String blueprintResourceType;
private String bpRoot;
private ResourceResolver listeningResolver;
@Activate
protected void activate(ComponentContext context) throws Exception {
this.blueprintResourceType = PropertiesUtil.toString(context.getProperties().get("cq.wcm.msm.blueprint.resource_type"), (String)"wcm/msm/components/blueprint");
this.bpRoot = PropertiesUtil.toString(context.getProperties().get("cq.wcm.msm.blueprint.root"), (String)"/etc/blueprints");
this.listeningResolver = this.resolverFactory.getServiceResourceResolver(null);
Session listeningSession = (Session)this.listeningResolver.adaptTo(Session.class);
listeningSession.getWorkspace().getObservationManager().addEventListener((EventListener)this, 63, this.bpRoot, true, null, null, true);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Deactivate
protected void deactivate(ComponentContext context) throws RepositoryException {
this.lock.lock();
try {
this.guardedPath = null;
}
finally {
this.lock.unlock();
}
if (this.listeningResolver != null && this.listeningResolver.isLive()) {
ObservationManager om = ((Session)this.listeningResolver.adaptTo(Session.class)).getWorkspace().getObservationManager();
if (om != null) {
om.removeEventListener((EventListener)this);
}
this.listeningResolver.close();
}
}
public BlueprintManagerImpl getBlueprintManager(ResourceResolver resourceResolver) {
return new BlueprintManagerImpl(resourceResolver, this.rolloutConfigMgrFactory.create(resourceResolver), this);
}
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (Blueprint.class.isAssignableFrom(type) && Resource.class.isInstance(adaptable)) {
Resource resource = (Resource)adaptable;
Page page = (Page)resource.adaptTo(Page.class);
if (page != null) {
return (AdapterType)new BlueprintImpl(page, this.rolloutConfigMgrFactory.create(resource.getResourceResolver()));
}
this.log.debug("Resource at {} could not be adapted to a Blueprint: not Page", (Object)((Resource)adaptable).getPath());
return null;
}
if (type == BlueprintManager.class && ResourceResolver.class.isInstance(adaptable)) {
return (AdapterType)this.getBlueprintManager((ResourceResolver)adaptable);
}
return null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void onEvent(EventIterator events) {
this.lock.lock();
try {
this.guardedPath = null;
}
finally {
this.lock.unlock();
}
}
List<String> getBlueprintPaths() throws RepositoryException {
ResourceResolver serviceResolver = null;
try {
Resource resource;
this.lock.lock();
if (this.guardedPath == null && (resource = (serviceResolver = this.resolverFactory.getServiceResourceResolver(null)).getResource(this.bpRoot)) != null) {
this.guardedPath = new ArrayList<String>();
this.collectBlueprintPathes(resource, this.guardedPath);
}
if (this.guardedPath == null) {
this.guardedPath = Collections.emptyList();
}
resource = this.guardedPath;
return resource;
}
catch (LoginException e) {
this.log.error("Failed to access Service ResourceResolver {}", (Throwable)e);
throw new RepositoryException((Throwable)e);
}
finally {
if (serviceResolver != null) {
serviceResolver.close();
}
this.lock.unlock();
}
}
private void collectBlueprintPathes(Resource resource, List<String> all) throws RepositoryException {
Page page = (Page)resource.adaptTo(Page.class);
if (page != null) {
if (this.isBlueprintResource(page)) {
all.add(resource.getPath());
}
Iterator children = page.listChildren();
while (children.hasNext()) {
this.collectBlueprintPathes((Resource)((Page)children.next()).adaptTo(Resource.class), all);
}
} else {
Iterator iter = resource.listChildren();
while (iter.hasNext()) {
this.collectBlueprintPathes((Resource)iter.next(), all);
}
}
}
private boolean isBlueprintResource(Page page) {
return page.getContentResource() != null && page.getContentResource().isResourceType(this.blueprintResourceType);
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
protected void bindRolloutConfigMgrFactory(RolloutConfigManagerFactory rolloutConfigManagerFactory) {
this.rolloutConfigMgrFactory = rolloutConfigManagerFactory;
}
protected void unbindRolloutConfigMgrFactory(RolloutConfigManagerFactory rolloutConfigManagerFactory) {
if (this.rolloutConfigMgrFactory == rolloutConfigManagerFactory) {
this.rolloutConfigMgrFactory = null;
}
}
}