JcrPathBuilderManagerImpl.java
5.89 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.workflow.exec.WorkItem
* com.day.cq.workflow.exec.WorkflowData
* com.day.cq.workflow.ui.JcrPathBuilderManager
* com.day.cq.workflow.ui.JcrPayloadPathBuilder
* org.apache.felix.scr.annotations.Component
* 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.osgi.framework.BundleContext
* org.osgi.framework.ServiceReference
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.workflow.ui.impl;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.ui.JcrPathBuilderManager;
import com.day.cq.workflow.ui.JcrPayloadPathBuilder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import org.apache.felix.scr.annotations.Component;
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.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={JcrPathBuilderManager.class})
@Reference(name="pathBuilderRef", referenceInterface=JcrPayloadPathBuilder.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
public class JcrPathBuilderManagerImpl
implements JcrPathBuilderManager {
protected final Logger log = LoggerFactory.getLogger(JcrPathBuilderManagerImpl.class);
Map<Object, JcrPayloadPathBuilder> jcrPayloadPathBuilder = new TreeMap<Object, JcrPayloadPathBuilder>();
JcrPayloadPathBuilder defaultBuilder;
protected BundleContext bundleContext;
protected List<ServiceReference> registeredBuilderReferences;
private Integer currentDefaultRanking;
public JcrPathBuilderManagerImpl() {
this.defaultBuilder = new DefaultJcrPayloadPathBuilder();
this.registeredBuilderReferences = new ArrayList<ServiceReference>();
this.currentDefaultRanking = 0;
}
public String getPath(WorkItem workItem) {
long time = 0;
for (JcrPayloadPathBuilder builder : this.jcrPayloadPathBuilder.values()) {
this.log.debug("Checking PathBuilder {}", (Object)builder.getClass().getName());
try {
if (this.log.isTraceEnabled()) {
time = System.nanoTime();
}
String path = builder.buildPath(workItem);
if (this.log.isTraceEnabled()) {
time = System.nanoTime() - time;
this.log.trace("Builder {} took {} ms.", (Object)builder.getClass().getName(), (Object)TimeUnit.MILLISECONDS.convert(time, TimeUnit.NANOSECONDS));
}
if (path == null) continue;
this.log.debug("PathBuilder {} succeeded in building the path.", (Object)builder.getClass().getName());
return path;
}
catch (Throwable e) {
this.log.trace("Error executing Path Builder", e);
continue;
}
}
return this.defaultBuilder.buildPath(workItem);
}
protected void activate(ComponentContext componentContext) {
this.bundleContext = componentContext.getBundleContext();
for (ServiceReference ref : this.registeredBuilderReferences) {
JcrPayloadPathBuilder pb = (JcrPayloadPathBuilder)this.bundleContext.getService(ref);
if (pb == null) continue;
this.addBuilder(pb, this.getServiceRanking(ref));
}
this.registeredBuilderReferences.clear();
}
protected void deactivate(ComponentContext componentContext) {
this.bundleContext = null;
}
protected void bindPathBuilderRef(ServiceReference ref) {
if (this.bundleContext == null) {
this.registeredBuilderReferences.add(ref);
} else {
JcrPayloadPathBuilder pb = (JcrPayloadPathBuilder)this.bundleContext.getService(ref);
if (pb != null) {
this.addBuilder(pb, this.getServiceRanking(ref));
}
}
}
protected void unbindPathBuilderRef(ServiceReference ref) {
if (this.bundleContext != null) {
JcrPayloadPathBuilder pb = (JcrPayloadPathBuilder)this.bundleContext.getService(ref);
this.removeBuilder(pb);
}
}
private Object getServiceRanking(ServiceReference ref) {
return ref.getProperty("service.ranking");
}
private void addBuilder(JcrPayloadPathBuilder pb, Object serviceRanking) {
if (serviceRanking == null) {
Integer n = this.currentDefaultRanking;
Integer n2 = this.currentDefaultRanking = Integer.valueOf(this.currentDefaultRanking + 1);
this.jcrPayloadPathBuilder.put(n, pb);
} else {
this.jcrPayloadPathBuilder.put(serviceRanking, pb);
}
}
private void removeBuilder(JcrPayloadPathBuilder pb) {
for (Object key : this.jcrPayloadPathBuilder.keySet()) {
JcrPayloadPathBuilder jpb = this.jcrPayloadPathBuilder.get(key);
if (!jpb.getClass().getName().equals(pb.getClass().getName())) continue;
this.jcrPayloadPathBuilder.remove(key);
break;
}
}
public class DefaultJcrPayloadPathBuilder
implements JcrPayloadPathBuilder {
public String buildPath(WorkItem workItem) {
return workItem.getWorkflowData().getPayload().toString();
}
}
}