PredicateProviderImpl.java
3.7 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.collections.Predicate
* 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.ServiceReference
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.commons.impl;
import com.day.cq.commons.predicate.PredicateProvider;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.Predicate;
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.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Reference(name="Predicate", referenceInterface=Predicate.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
@Service(value={PredicateProvider.class})
public class PredicateProviderImpl
implements PredicateProvider {
private final Logger log = LoggerFactory.getLogger(PredicateProviderImpl.class);
private static String PRED_REF_NAME = "Predicate";
private static String PREDICATE_NAME = "predicate.name";
private ComponentContext osgiComponentContext;
private List<ServiceReference> delayedRefs;
private Map<String, Predicate> predicates = new HashMap<String, Predicate>();
@Override
public Predicate getPredicate(String name) {
return this.predicates.get(name);
}
protected void activate(ComponentContext componentContext) {
this.osgiComponentContext = componentContext;
if (this.osgiComponentContext != null && this.delayedRefs != null) {
List<ServiceReference> list = this.delayedRefs;
this.delayedRefs = null;
for (ServiceReference ref : list) {
this.initPredicate(ref);
}
}
this.log.info("Predicate service activated.");
}
protected void deactivate(ComponentContext componentContext) {
this.log.info("Predicate service deactivated.");
}
protected void bindPredicate(ServiceReference ref) {
if (this.osgiComponentContext == null) {
if (this.delayedRefs == null) {
this.delayedRefs = new LinkedList<ServiceReference>();
}
this.delayedRefs.add(ref);
} else {
this.initPredicate(ref);
}
}
protected void unbindPredicate(ServiceReference ref) {
String name = (String)ref.getProperty(PREDICATE_NAME);
if (this.predicates.remove(name) == null) {
this.log.warn("removing unregistered predicate {}", (Object)name);
} else {
this.log.info("unregistering predicate {}", (Object)name);
}
}
private void initPredicate(ServiceReference ref) {
Predicate pred = (Predicate)this.osgiComponentContext.locateService(PRED_REF_NAME, ref);
if (pred != null) {
String name = (String)ref.getProperty(PREDICATE_NAME);
if (name == null) {
this.log.error("initFilter: Missing name for predicate {}", (Object)ref);
return;
}
this.log.info("registering predicate {}", (Object)name);
this.predicates.put(name, pred);
}
}
}