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
|
/*******************************************************************************
* Copyright (c) 2005, 2018 Cognos Incorporated, IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Cognos Incorporated - initial API and implementation
* IBM Corporation - bug fixes and enhancements
*******************************************************************************/
package org.eclipse.equinox.internal.cm;
import java.util.*;
import org.osgi.framework.*;
import org.osgi.service.cm.ConfigurationPlugin;
import org.osgi.util.tracker.ServiceTracker;
/**
* PluginManager tracks and allows customization via ConfigurationPlugin
*/
public class PluginManager {
private final PluginTracker pluginTracker;
public PluginManager(BundleContext context) {
pluginTracker = new PluginTracker(context);
}
public void start() {
pluginTracker.open();
}
public void stop() {
pluginTracker.close();
}
public void modifyConfiguration(ServiceReference<?> managedReference, Dictionary<String, Object> properties) {
if (properties == null)
return;
ServiceReference<ConfigurationPlugin>[] references = pluginTracker.getServiceReferences();
for (int i = 0; i < references.length; ++i) {
String[] pids = (String[]) references[i].getProperty(ConfigurationPlugin.CM_TARGET);
if (pids != null) {
String pid = (String) properties.get(Constants.SERVICE_PID);
if (!Arrays.asList(pids).contains(pid))
continue;
}
ConfigurationPlugin plugin = pluginTracker.getService(references[i]);
if (plugin != null)
plugin.modifyConfiguration(managedReference, properties);
}
}
private static class PluginTracker extends ServiceTracker<ConfigurationPlugin, ConfigurationPlugin> {
final Integer ZERO = Integer.valueOf(0);
private TreeSet<ServiceReference<ConfigurationPlugin>> serviceReferences = new TreeSet<>(new Comparator<ServiceReference<ConfigurationPlugin>>() {
@Override
public int compare(ServiceReference<ConfigurationPlugin> s1, ServiceReference<ConfigurationPlugin> s2) {
int rankCompare = getRank(s1).compareTo(getRank(s2));
if (rankCompare != 0) {
return rankCompare;
}
// we reverse the order which means services with higher service.ranking properties are called first
return -(s1.compareTo(s2));
}
private Integer getRank(ServiceReference<ConfigurationPlugin> ref) {
Object ranking = ref.getProperty(ConfigurationPlugin.CM_RANKING);
if (ranking == null || !(ranking instanceof Integer))
return ZERO;
return ((Integer) ranking);
}
});
public PluginTracker(BundleContext context) {
super(context, ConfigurationPlugin.class.getName(), null);
}
/* NOTE: this method alters the contract of the overriden method.
* Rather than returning null if no references are present, it
* returns an empty array.
*/
@Override
public ServiceReference<ConfigurationPlugin>[] getServiceReferences() {
synchronized (serviceReferences) {
return serviceReferences.toArray(new ServiceReference[serviceReferences.size()]);
}
}
@Override
public ConfigurationPlugin addingService(ServiceReference<ConfigurationPlugin> reference) {
synchronized (serviceReferences) {
serviceReferences.add(reference);
}
return context.getService(reference);
}
@Override
public void modifiedService(ServiceReference<ConfigurationPlugin> reference, ConfigurationPlugin service) {
// nothing to do
}
@Override
public void removedService(ServiceReference<ConfigurationPlugin> reference, ConfigurationPlugin service) {
synchronized (serviceReferences) {
serviceReferences.remove(reference);
}
context.ungetService(reference);
}
}
}
|