ActionManagerImpl.java
7.87 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
/*
* 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.ActionManager
* com.day.cq.wcm.msm.api.LiveAction
* com.day.cq.wcm.msm.api.LiveActionFactory
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.RolloutConfig
* com.day.cq.wcm.msm.api.RolloutManager
* com.day.cq.wcm.msm.api.RolloutManager$Trigger
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* 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.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.ActionManager;
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.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.Utils;
import com.day.cq.wcm.msm.impl.actions.ActionConfigImpl;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
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;
@Component(metatype=0, immediate=0)
@Service
public class ActionManagerImpl
implements ActionManager {
private final Logger log = LoggerFactory.getLogger(ActionManagerImpl.class);
private ServiceRegistry<LiveActionFactory> factoryRegistry;
protected void activate(ComponentContext context) {
this.factoryRegistry = new ServiceRegistry<LiveActionFactory>(context.getBundleContext(), LiveActionFactory.class);
}
protected void deactivate(ComponentContext context) {
if (this.factoryRegistry != null) {
this.factoryRegistry.close();
}
}
public Map<String, LiveAction> getRegistredActions() {
HashMap<String, LiveAction> ret = new HashMap<String, LiveAction>();
for (ServiceReference ref : this.factoryRegistry.getServiceReferences()) {
LiveActionFactory factory = this.factoryRegistry.getService(ref);
if (factory == null) continue;
try {
LiveAction action = factory.createAction(null);
Object prop = ref.getProperty("liveActionName");
if (prop instanceof String[]) {
for (String name : (String[])prop) {
ret.put(name, action);
}
continue;
}
ret.put(action.getName(), action);
continue;
}
catch (WCMException e) {
this.log.error("Failed to add actions {}", (Throwable)e);
}
}
return ret;
}
public LiveAction getAction(String name) {
return this.getRegistredActions().get(name);
}
public void executeActions(ResourceResolver resolver, LiveRelationship relation, RolloutManager.Trigger trigger, boolean autoSave) throws WCMException {
this.executeActions(resolver, relation, trigger, autoSave, false);
}
public void executeActions(ResourceResolver resolver, LiveRelationship relation, RolloutManager.Trigger trigger, boolean autoSave, boolean isResetRollout) throws WCMException {
List rolloutConfigs = relation.getRolloutConfigs();
for (RolloutConfig rc : rolloutConfigs) {
if (trigger != null && !trigger.equals((Object)rc.getTrigger())) continue;
Set configs = rc.getActionsConfig();
for (ActionConfig config : configs) {
LiveAction action = this.getAction(config.getName());
if (action != null) {
action.execute(resolver, relation, config, autoSave, isResetRollout);
continue;
}
throw new WCMException("Unregistered action name " + config.getName());
}
}
}
public void executeAction(ResourceResolver resolver, LiveRelationship relation, ActionConfig config, boolean autoSave) throws WCMException {
this.executeAction(resolver, relation, config, autoSave, false);
}
public void executeAction(ResourceResolver resolver, LiveRelationship relation, ActionConfig config, boolean autoSave, boolean isResetRollout) throws WCMException {
LiveAction action = this.getAction(config.getName());
if (action == null) {
throw new WCMException("Unregistered action name " + config.getName());
}
action.execute(resolver, relation, config, autoSave, isResetRollout);
}
public Set<ActionConfig> getActionsConfig(Resource root) throws RepositoryException {
Node node = (Node)root.adaptTo(Node.class);
return this.getActionsConfig(node);
}
public Set<ActionConfig> getActionsConfig(Node rootNode) throws RepositoryException {
TreeSet<ActionConfig> res = new TreeSet<ActionConfig>();
if (rootNode != null) {
int rank = 0;
NodeIterator ni = rootNode.getNodes();
while (ni.hasNext()) {
ActionConfig ac;
Node n = ni.nextNode();
if (!n.isNodeType("cq:LiveSyncAction") || (ac = this.getActionConfig(n, rank++)) == null) continue;
res.add(ac);
}
}
return res;
}
private ActionConfig getActionConfig(Node node, int rank) throws RepositoryException {
if (node != null && node.isNodeType("cq:LiveSyncAction")) {
String name = node.getName();
LiveAction action = this.getAction(name);
if (action != null) {
return new ActionConfigImpl(action.getName(), action.getParameterName(), Utils.nodeToMap(node), rank);
}
this.log.warn("LiveAction name '{}' specified by node {} not found", (Object)name, (Object)node.getPath());
}
return null;
}
private static final class ServiceRegistry<Type> {
private int cnt = -1;
private final ServiceTracker tracker;
private final Set<Type> services = new HashSet<Type>();
private final BundleContext context;
public ServiceRegistry(BundleContext context, Class<Type> cls) {
this.context = context;
this.tracker = new ServiceTracker(context, cls.getName(), null);
this.tracker.open();
}
ServiceReference[] getServiceReferences() {
return this.tracker.getServiceReferences();
}
Type getService(ServiceReference ref) {
return (Type)this.context.getService(ref);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
void close() {
Set<Type> set = this.services;
synchronized (set) {
if (this.tracker != null) {
this.tracker.close();
}
this.services.clear();
}
}
}
}