BVPManager.java
8.57 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* aQute.bnd.annotation.component.Deactivate
* com.adobe.granite.xss.XSSAPI
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Modified
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.PropertyUnbounded
* org.apache.felix.scr.annotations.Reference
* org.apache.sling.commons.osgi.PropertiesUtil
* org.apache.sling.scripting.api.BindingsValuesProvider
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceReference
* org.osgi.framework.ServiceRegistration
* org.osgi.service.cm.Configuration
* org.osgi.service.cm.ConfigurationAdmin
* 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.wcm.scripting.impl;
import aQute.bnd.annotation.component.Deactivate;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.wcm.scripting.impl.WCMBindingsValuesProvider;
import java.util.Dictionary;
import java.util.Hashtable;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.scripting.api.BindingsValuesProvider;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
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.
*/
@Component(immediate=1, metatype=1, label="Adobe CQ WCM Bindings Manager", description="The Adobe CQ WCM Bindings Manager dynamically registers BindingsValuesProvider services for scripting support.")
@Properties(value={@Property(name="com.day.cq.wcm.scripting.bvp.script.engines", value={"jsp", "sightly"}, label="Scripting engines", description="The scripting engines for which the WCMBindingsValuesProvider should be active.", unbounded=PropertyUnbounded.ARRAY)})
public class BVPManager {
private static final Logger LOG = LoggerFactory.getLogger(BVPManager.class);
public static final String SCRIPT_ENGINES = "com.day.cq.wcm.scripting.bvp.script.engines";
public static final String[] SCRIPT_ENGINES_DEFAULT = new String[]{"jsp", "sightly"};
private volatile boolean active = false;
private ServiceTracker serviceTracker;
private ServiceRegistration bvpServiceRegistration;
private static final String WCM_CORE_BVP_PID = "com.day.cq.wcm.core.impl.WCMBindingsValuesProvider";
@Reference
private XSSAPI xssapi = null;
@Reference
private ConfigurationAdmin configurationAdmin = null;
@Activate
protected void activate(final ComponentContext componentContext) {
block3 : {
String[] scriptEngines = null;
try {
Configuration[] configurations = this.configurationAdmin.listConfigurations("(service.pid=" + BVPManager.class.getName() + ")");
if (configurations != null || (configurations = this.configurationAdmin.listConfigurations("(service.pid=com.day.cq.wcm.core.impl.WCMBindingsValuesProvider)")) == null) break block3;
for (Configuration c : configurations) {
scriptEngines = PropertiesUtil.toStringArray(c.getProperties().get("javax.script.name"), (String[])null);
if (scriptEngines == null) continue;
Hashtable<String, String[]> bvpManagerProperties = new Hashtable<String, String[]>();
bvpManagerProperties.put("com.day.cq.wcm.scripting.bvp.script.engines", scriptEngines);
this.updateBVPManagerConfiguration(bvpManagerProperties);
break;
}
}
catch (Exception e) {
LOG.error("Unable to determine if " + BVPManager.class.getName() + " was configured or not.", (Throwable)e);
}
}
this.modified(componentContext);
final BundleContext bundleContext = componentContext.getBundleContext();
this.serviceTracker = new ServiceTracker(bundleContext, BindingsValuesProvider.class.getName(), null){
public Object addingService(ServiceReference reference) {
if ("com.day.cq.wcm.core.impl.WCMBindingsValuesProvider".equals(reference.getProperty("component.name"))) {
LOG.warn("Detected a previous version of a WCM BVP: {}. Disabling the newer BVP ({}) to maintain backwards compatibility.", (Object)"com.day.cq.wcm.core.impl.WCMBindingsValuesProvider", (Object)WCMBindingsValuesProvider.class.getName());
BVPManager.this.unregisterBVP();
}
return super.addingService(reference);
}
public void removedService(ServiceReference reference, Object obj) {
if ("com.day.cq.wcm.core.impl.WCMBindingsValuesProvider".equals(reference.getProperty("component.name")) && BVPManager.this.active) {
LOG.info("{} was removed. Activating a newer version of that BVP: {}.", (Object)"com.day.cq.wcm.core.impl.WCMBindingsValuesProvider", (Object)WCMBindingsValuesProvider.class.getName());
BVPManager.this.registerBVP(bundleContext, BVPManager.this.getBVPProperties(componentContext));
}
super.removedService(reference, obj);
}
};
this.serviceTracker.open();
this.active = true;
}
@Modified
protected void modified(ComponentContext componentContext) {
this.unregisterBVP();
this.registerBVP(componentContext.getBundleContext(), this.getBVPProperties(componentContext));
}
@Deactivate
protected void deactivate(ComponentContext componentContext) {
this.active = false;
if (this.serviceTracker != null) {
this.serviceTracker.close();
this.serviceTracker = null;
}
this.unregisterBVP();
}
private Dictionary<String, Object> getBVPProperties(ComponentContext componentContext) {
Hashtable<String, Object> properties = new Hashtable<String, Object>();
properties.put("service.pid", WCMBindingsValuesProvider.class.getName());
properties.put("service.vendor", "Adobe Systems");
properties.put("service.ranking", 1000);
properties.put("javax.script.name", PropertiesUtil.toStringArray(componentContext.getProperties().get("com.day.cq.wcm.scripting.bvp.script.engines"), (String[])SCRIPT_ENGINES_DEFAULT));
return properties;
}
private void updateBVPManagerConfiguration(Dictionary properties) {
try {
Configuration bvpManagerConfiguration = this.configurationAdmin.getConfiguration(BVPManager.class.getName());
bvpManagerConfiguration.update(properties);
}
catch (Exception e) {
LOG.error("Unable to update configuration for " + BVPManager.class.getName(), (Throwable)e);
}
}
private void registerBVP(BundleContext bundleContext, Dictionary properties) {
this.bvpServiceRegistration = bundleContext.registerService(BindingsValuesProvider.class.getName(), (Object)new WCMBindingsValuesProvider(this.xssapi), properties);
}
private void unregisterBVP() {
if (this.bvpServiceRegistration != null) {
this.bvpServiceRegistration.unregister();
this.bvpServiceRegistration = null;
}
}
protected void bindXssapi(XSSAPI xSSAPI) {
this.xssapi = xSSAPI;
}
protected void unbindXssapi(XSSAPI xSSAPI) {
if (this.xssapi == xSSAPI) {
this.xssapi = null;
}
}
protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
this.configurationAdmin = configurationAdmin;
}
protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
if (this.configurationAdmin == configurationAdmin) {
this.configurationAdmin = null;
}
}
}