ApiEndpointResourceProviderFactoryImpl.java
5.28 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:
* org.apache.felix.scr.annotations.Activate
* 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.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.ResourceProvider
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.framework.BundleContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.rest.impl;
import com.adobe.granite.rest.ApiResourceProviderFactory;
import com.adobe.granite.rest.impl.ApiEndpointResourceProvider;
import com.adobe.granite.rest.impl.ApiEndpointResourceProviderFactory;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.felix.scr.annotations.Activate;
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.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceProvider;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Adobe Granite REST ApiEndpointResourceProviderFactory")
@Service
@Properties(value={@Property(name="provider.roots", value={"api"})})
public class ApiEndpointResourceProviderFactoryImpl
implements ApiEndpointResourceProviderFactory {
private static final String DEFAULT_ROOT = "api";
private final Logger logger;
private final ReadWriteLock providerFactoriesLock;
private String root;
@Reference(referenceInterface=ApiResourceProviderFactory.class, bind="bindApiResourceProviderFactory", unbind="unbindApiResourceProviderFactory", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
private Map<String, ApiResourceProviderFactory> apiProviderFactories;
public ApiEndpointResourceProviderFactoryImpl() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.providerFactoriesLock = new ReentrantReadWriteLock();
this.apiProviderFactories = new HashMap<String, ApiResourceProviderFactory>();
}
public ResourceProvider getResourceProvider(Map<String, Object> authenticationInfo) throws LoginException {
return new ApiEndpointResourceProvider(this);
}
public ResourceProvider getAdministrativeResourceProvider(Map<String, Object> authenticationInfo) throws LoginException {
return this.getResourceProvider(authenticationInfo);
}
@Override
public String getRootContextPath() {
String rootContext = this.root;
if (!rootContext.startsWith("/")) {
rootContext = "/" + rootContext;
}
return rootContext;
}
@Override
public ApiResourceProviderFactory getApiProviderFactory(String type) {
return this.apiProviderFactories.get(type);
}
@Override
public Collection<ApiResourceProviderFactory> getApiProviderFactories() {
return this.apiProviderFactories.values();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindApiResourceProviderFactory(ApiResourceProviderFactory apiResourceProviderFactory, Map<String, Object> properties) {
this.providerFactoriesLock.writeLock().lock();
try {
String type = PropertiesUtil.toString((Object)properties.get("provider.type"), (String)null);
this.apiProviderFactories.put(type, apiResourceProviderFactory);
this.logger.info("ApiResourceProviderFactory (type={},class={}) bound.", (Object)type, (Object)apiResourceProviderFactory.getClass().getName());
}
finally {
this.providerFactoriesLock.writeLock().unlock();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindApiResourceProviderFactory(ApiResourceProviderFactory apiResourceProviderFactory, Map<String, Object> properties) {
this.providerFactoriesLock.writeLock().lock();
try {
String type = PropertiesUtil.toString((Object)properties.get("provider.type"), (String)null);
this.apiProviderFactories.remove(type);
this.logger.info("ApiResourceProviderFactory (type={},class={}) unbound.", (Object)type, (Object)apiResourceProviderFactory.getClass().getName());
}
finally {
this.providerFactoriesLock.writeLock().unlock();
}
}
@Activate
protected void activate(BundleContext context) {
this.root = PropertiesUtil.toString((Object)context.getProperty("provider.roots"), (String)"api");
}
}