ProfileAdapterFactory.java
11.7 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.security.user.UserProperties
* com.day.cq.personalization.ProfileProvider
* com.day.cq.personalization.UserPropertiesProvider
* com.day.cq.security.profile.Profile
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.adapter.AdapterFactory
* org.apache.sling.api.resource.ResourceResolver
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceReference
* org.osgi.service.component.ComponentContext
* org.osgi.util.tracker.ServiceTracker
* org.osgi.util.tracker.ServiceTrackerCustomizer
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.personalization.impl;
import com.adobe.granite.security.user.UserProperties;
import com.day.cq.personalization.ProfileProvider;
import com.day.cq.personalization.UserPropertiesProvider;
import com.day.cq.security.profile.Profile;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class ProfileAdapterFactory
implements AdapterFactory {
private final Logger log = LoggerFactory.getLogger(ProfileAdapterFactory.class);
private static final String REQUEST_ATTRIBUTE_NAME_PROFILE = Profile.class.getName();
private static final String REQUEST_ATTRIBUTE_NAME_USERPROPERTIES = UserProperties.class.getName();
private ProfileProviderServiceTracker serviceTracker;
private UserPropertiesProviderServiceTracker userPropsServiceTracker;
private Map<ServiceReference, ProfileProvider> providerCache = new TreeMap<ServiceReference, ProfileProvider>();
private Map<ServiceReference, UserPropertiesProvider> userPropsProviderCache = new TreeMap<ServiceReference, UserPropertiesProvider>();
private static final Class<Profile> PROFILE_CLASS = Profile.class;
private static final Class<UserProperties> USERPROPERTIES_CLASS = UserProperties.class;
public static final String[] ADAPTER_CLASSES = new String[]{PROFILE_CLASS.getName(), USERPROPERTIES_CLASS.getName()};
public static final String[] ADAPTABLE_CLASSES = new String[]{ResourceResolver.class.getName(), SlingHttpServletRequest.class.getName()};
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (adaptable instanceof ResourceResolver) {
return this.getAdapter((ResourceResolver)adaptable, type);
}
if (adaptable instanceof SlingHttpServletRequest) {
return this.getAdapter((SlingHttpServletRequest)adaptable, type);
}
this.log.warn("Unable to handle adaptable [{}]", (Object)adaptable.getClass().getName());
return null;
}
private <AdapterType> AdapterType getAdapter(ResourceResolver resolver, Class<AdapterType> type) {
if (PROFILE_CLASS == type) {
return (AdapterType)this.getProfile(resolver);
}
if (USERPROPERTIES_CLASS == type) {
return (AdapterType)this.getUserProperties(resolver);
}
this.log.debug("Unable to adapt resolver [{}] to type [{}]", (Object)resolver, (Object)type.getName());
return null;
}
private <AdapterType> AdapterType getAdapter(SlingHttpServletRequest request, Class<AdapterType> type) {
if (PROFILE_CLASS == type) {
return (AdapterType)this.getProfile(request);
}
if (USERPROPERTIES_CLASS == type) {
return (AdapterType)this.getUserProperties(request);
}
this.log.debug("Unable to adapt request [{}] to type [{}]", (Object)request.getRequestURI(), (Object)type.getName());
return null;
}
public Profile getProfile(SlingHttpServletRequest request) {
Profile profile = (Profile)request.getAttribute(REQUEST_ATTRIBUTE_NAME_PROFILE);
Iterator<ProfileProvider> providers = this.providerCache.values().iterator();
while (null == profile && providers.hasNext()) {
ProfileProvider provider = providers.next();
this.log.debug("asking provider [{}] for profile on [{}]...", (Object)provider, (Object)request.getRequestURI());
profile = provider.getProfile(request);
if (profile == null) continue;
this.log.debug("provider [{}] returned profile [{}].", (Object)provider, (Object)profile);
request.setAttribute(REQUEST_ATTRIBUTE_NAME_PROFILE, (Object)profile);
}
return profile;
}
public Profile getProfile(ResourceResolver resolver) {
Profile profile = null;
Iterator<ProfileProvider> providers = this.providerCache.values().iterator();
while (null == profile && providers.hasNext()) {
ProfileProvider provider = providers.next();
this.log.debug("asking provider [{}] for profile on [{}]...", (Object)provider, (Object)resolver);
profile = provider.getProfile(resolver);
if (profile == null) continue;
this.log.debug("provider [{}] returned profile [{}].", (Object)provider, (Object)profile);
}
return profile;
}
public UserProperties getUserProperties(SlingHttpServletRequest request) {
UserProperties userProperties = (UserProperties)request.getAttribute(REQUEST_ATTRIBUTE_NAME_USERPROPERTIES);
Iterator<UserPropertiesProvider> providers = this.userPropsProviderCache.values().iterator();
while (null == userProperties && providers.hasNext()) {
UserPropertiesProvider provider = providers.next();
this.log.debug("asking provider [{}] for userProperties on [{}]...", (Object)provider, (Object)request.getRequestURI());
userProperties = provider.getUserProperties(request);
if (userProperties == null) continue;
this.log.debug("provider [{}] returned userProperties [{}].", (Object)provider, (Object)userProperties);
request.setAttribute(REQUEST_ATTRIBUTE_NAME_USERPROPERTIES, (Object)userProperties);
}
return userProperties;
}
public UserProperties getUserProperties(ResourceResolver resolver) {
UserProperties userProperties = null;
Iterator<UserPropertiesProvider> providers = this.userPropsProviderCache.values().iterator();
while (null == userProperties && providers.hasNext()) {
UserPropertiesProvider provider = providers.next();
this.log.debug("asking provider [{}] for userProperties on [{}]...", (Object)provider, (Object)resolver);
userProperties = provider.getUserProperties(resolver);
if (userProperties == null) continue;
this.log.debug("provider [{}] returned userProperties [{}].", (Object)provider, (Object)userProperties);
}
return userProperties;
}
protected void activate(ComponentContext context) {
BundleContext bundleContext = context.getBundleContext();
this.serviceTracker = new ProfileProviderServiceTracker(bundleContext, ProfileProvider.class.getName(), this.providerCache);
this.serviceTracker.open();
this.userPropsServiceTracker = new UserPropertiesProviderServiceTracker(bundleContext, UserPropertiesProvider.class.getName(), this.userPropsProviderCache);
this.userPropsServiceTracker.open();
}
protected void deactivate(ComponentContext context) {
this.serviceTracker.close();
this.userPropsServiceTracker.close();
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
static final class UserPropertiesProviderServiceTracker
extends ServiceTracker {
final Map<ServiceReference, UserPropertiesProvider> serviceCache;
UserPropertiesProviderServiceTracker(BundleContext context, String clazz, Map<ServiceReference, UserPropertiesProvider> serviceCache) {
super(context, clazz, null);
this.serviceCache = serviceCache;
}
public Object addingService(ServiceReference reference) {
Object service = super.addingService(reference);
if (null != service) {
this.bindService(reference, service);
}
return service;
}
public void modifiedService(ServiceReference reference, Object service) {
this.unbindService(reference);
if (null != service) {
this.bindService(reference, service);
}
}
public void removedService(ServiceReference reference, Object service) {
this.unbindService(reference);
super.removedService(reference, service);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void bindService(ServiceReference reference, Object service) {
Map<ServiceReference, UserPropertiesProvider> map = this.serviceCache;
synchronized (map) {
this.serviceCache.put(reference, (UserPropertiesProvider)service);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void unbindService(ServiceReference reference) {
Map<ServiceReference, UserPropertiesProvider> map = this.serviceCache;
synchronized (map) {
this.serviceCache.remove((Object)reference);
}
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
@Deprecated
static final class ProfileProviderServiceTracker
extends ServiceTracker {
final Map<ServiceReference, ProfileProvider> serviceCache;
ProfileProviderServiceTracker(BundleContext context, String clazz, Map<ServiceReference, ProfileProvider> serviceCache) {
super(context, clazz, null);
this.serviceCache = serviceCache;
}
public Object addingService(ServiceReference reference) {
Object service = super.addingService(reference);
if (null != service) {
this.bindService(reference, service);
}
return service;
}
public void modifiedService(ServiceReference reference, Object service) {
this.unbindService(reference);
if (null != service) {
this.bindService(reference, service);
}
}
public void removedService(ServiceReference reference, Object service) {
this.unbindService(reference);
super.removedService(reference, service);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void bindService(ServiceReference reference, Object service) {
Map<ServiceReference, ProfileProvider> map = this.serviceCache;
synchronized (map) {
this.serviceCache.put(reference, (ProfileProvider)service);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void unbindService(ServiceReference reference) {
Map<ServiceReference, ProfileProvider> map = this.serviceCache;
synchronized (map) {
this.serviceCache.remove((Object)reference);
}
}
}
}