LegacyLiveActionRegistration.java
8.15 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.msm.api.ActionConfig
* com.day.cq.wcm.msm.api.LiveAction
* com.day.cq.wcm.msm.api.LiveActionFactory
* com.day.cq.wcm.msm.api.LiveRelationship
* javax.jcr.Node
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Component
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.osgi.framework.BundleContext
* org.osgi.framework.InvalidSyntaxException
* org.osgi.framework.ServiceEvent
* org.osgi.framework.ServiceListener
* org.osgi.framework.ServiceReference
* org.osgi.framework.ServiceRegistration
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl.actions;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.ActionConfig;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveActionFactory;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.impl.Utils;
import com.day.cq.wcm.msm.impl.actions.ActionConfigImpl;
import java.util.Collection;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, immediate=1)
public class LegacyLiveActionRegistration
implements ServiceListener {
private BundleContext bundleContext;
private static final Logger log = LoggerFactory.getLogger(LegacyLiveActionRegistration.class);
private final Map<String, ServiceRegistration> registeredServices = new HashMap<String, ServiceRegistration>();
protected void activate(ComponentContext componentContext) throws InvalidSyntaxException, WCMException {
this.bundleContext = componentContext.getBundleContext();
ServiceReference[] references = this.bundleContext.getServiceReferences(LiveAction.class.getName(), null);
if (references != null) {
for (ServiceReference ref : references) {
this.register(ref);
}
}
this.bundleContext.addServiceListener((ServiceListener)this, String.format("(%s=%s)", "objectClass", LiveAction.class.getName()));
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void deactivate(ComponentContext componentContext) {
BundleContext ctx = componentContext.getBundleContext();
Map<String, ServiceRegistration> map = this.registeredServices;
synchronized (map) {
ctx.removeServiceListener((ServiceListener)this);
for (ServiceRegistration reg : this.registeredServices.values()) {
reg.unregister();
}
}
}
public void serviceChanged(ServiceEvent serviceEvent) {
try {
switch (serviceEvent.getType()) {
case 2: {
this.unregister(serviceEvent.getServiceReference());
this.register(serviceEvent.getServiceReference());
break;
}
case 1: {
this.register(serviceEvent.getServiceReference());
break;
}
case 4: {
this.unregister(serviceEvent.getServiceReference());
}
}
}
catch (WCMException e) {
log.error("Failed to register Service {}", (Throwable)e);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void register(ServiceReference serviceReference) throws WCMException {
Map<String, ServiceRegistration> map = this.registeredServices;
synchronized (map) {
LiveAction action = (LiveAction)this.bundleContext.getService(serviceReference);
if (action == null) {
log.warn("Received Service Event but could not access service {}", (Object)serviceReference);
return;
}
Properties dict = new Properties();
dict.put("liveActionName", action.getName());
ServiceRegistration registration = this.bundleContext.registerService(LiveActionFactory.class.getName(), (Object)new LegacyLiveActionFactory(action), (Dictionary)dict);
this.registeredServices.put((String)serviceReference.getProperty("service.pid"), registration);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void unregister(ServiceReference serviceReference) {
Map<String, ServiceRegistration> map = this.registeredServices;
synchronized (map) {
String service = (String)serviceReference.getProperty("service.pid");
if (this.registeredServices.containsKey(service)) {
ServiceRegistration registration = this.registeredServices.remove(service);
registration.unregister();
}
}
}
private static final class LegacyLiveAction
implements LiveAction {
private final ActionConfig config;
private final LiveAction delegatee;
public LegacyLiveAction(LiveAction action, ActionConfig ac) {
this.config = ac;
this.delegatee = action;
}
public String getName() {
return this.delegatee.getName();
}
public void execute(Resource source, Resource target, LiveRelationship relation, boolean autoSave, boolean isResetRollout) throws WCMException {
this.execute(source.getResourceResolver(), relation, this.config, autoSave, isResetRollout);
}
public void execute(ResourceResolver resolver, LiveRelationship relation, ActionConfig config, boolean autoSave) throws WCMException {
this.execute(resolver, relation, config, autoSave, false);
}
public void execute(ResourceResolver resolver, LiveRelationship relation, ActionConfig config, boolean autoSave, boolean isResetRollout) throws WCMException {
this.delegatee.execute(resolver, relation, config, autoSave, isResetRollout);
}
public String getParameterName() {
return this.delegatee.getParameterName();
}
public String getTitle() {
return this.delegatee.getTitle();
}
public String[] getPropertiesNames() {
return this.delegatee.getPropertiesNames();
}
public int getRank() {
return this.delegatee.getRank();
}
public void write(JSONWriter out) throws JSONException {
this.delegatee.write(out);
}
}
private static final class LegacyLiveActionFactory
implements LiveActionFactory<LiveAction> {
private final LiveAction action;
private LegacyLiveActionFactory(LiveAction action) throws WCMException {
this.action = action;
}
public String createsAction() {
return this.action.getName();
}
public LiveAction createAction(Resource resource) throws WCMException {
try {
return new LegacyLiveAction(this.action, new ActionConfigImpl(this.action.getName(), this.action.getParameterName(), Utils.nodeToMap((Node)resource.adaptTo(Node.class)), this.action.getRank()));
}
catch (RepositoryException e) {
throw new WCMException((Throwable)e);
}
}
}
}