Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9732b87c38f529dea91206fcea11235b664192fe (plain) (blame)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*******************************************************************************
 * Copyright (c) 2012, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.internal.container;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osgi.container.ModuleCapability;
import org.eclipse.osgi.container.ModuleRevision;
import org.eclipse.osgi.internal.framework.FilterImpl;
import org.eclipse.osgi.util.ManifestElement;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.namespace.*;
import org.osgi.resource.*;

public class Capabilities {
	static class NamespaceSet {
		private final String name;
		private final Map<String, Set<ModuleCapability>> indexes = new HashMap<>();
		private final Set<ModuleCapability> all = new HashSet<>();
		private final Set<ModuleCapability> nonStringIndexes = new HashSet<>(0);
		private final boolean matchMandatory;

		NamespaceSet(String name) {
			this.name = name;
			this.matchMandatory = PackageNamespace.PACKAGE_NAMESPACE.equals(name) || BundleNamespace.BUNDLE_NAMESPACE.equals(name) || HostNamespace.HOST_NAMESPACE.equals(name);
		}

		void addCapability(ModuleCapability capability) {
			if (!name.equals(capability.getNamespace())) {
				throw new IllegalArgumentException("Invalid namespace: " + capability.getNamespace() + ": expecting: " + name); //$NON-NLS-1$ //$NON-NLS-2$
			}
			all.add(capability);
			// by convention we index by the namespace attribute
			Object index = capability.getAttributes().get(name);
			if (index == null) {
				return;
			}
			Collection<?> indexCollection = null;
			if (index instanceof Collection) {
				indexCollection = (Collection<?>) index;
			} else if (index.getClass().isArray()) {
				indexCollection = Arrays.asList((Object[]) index);
			}
			if (indexCollection == null) {
				addIndex(index, capability);
			} else {
				for (Object indexKey : indexCollection) {
					addIndex(indexKey, capability);
				}
			}
		}

		private void addIndex(Object indexKey, ModuleCapability capability) {
			if (!(indexKey instanceof String)) {
				nonStringIndexes.add(capability);
			} else {
				Set<ModuleCapability> capabilities = indexes.get(indexKey);
				if (capabilities == null) {
					capabilities = new HashSet<>(1);
					indexes.put((String) indexKey, capabilities);
				}
				capabilities.add(capability);
			}
		}

		void removeCapability(ModuleCapability capability) {
			if (!name.equals(capability.getNamespace())) {
				throw new IllegalArgumentException("Invalid namespace: " + capability.getNamespace() + ": expecting: " + name); //$NON-NLS-1$//$NON-NLS-2$
			}
			all.remove(capability);
			// by convention we index by the namespace attribute
			Object index = capability.getAttributes().get(name);
			if (index == null) {
				return;
			}
			Collection<?> indexCollection = null;
			if (index instanceof Collection) {
				indexCollection = (Collection<?>) index;
			} else if (index.getClass().isArray()) {
				indexCollection = Arrays.asList((Object[]) index);
			}
			if (indexCollection == null) {
				removeIndex(index, capability);
			} else {
				for (Object indexKey : indexCollection) {
					removeIndex(indexKey, capability);
				}
			}
		}

		private void removeIndex(Object indexKey, ModuleCapability capability) {
			if (!(indexKey instanceof String)) {
				nonStringIndexes.remove(capability);
			} else {
				Set<ModuleCapability> capabilities = indexes.get(indexKey);
				if (capabilities != null) {
					capabilities.remove(capability);
				}
			}
		}

		List<ModuleCapability> findCapabilities(Requirement requirement) {
			if (!name.equals(requirement.getNamespace())) {
				throw new IllegalArgumentException("Invalid namespace: " + requirement.getNamespace() + ": expecting: " + name); //$NON-NLS-1$//$NON-NLS-2$
			}
			FilterImpl f = null;
			String filterSpec = requirement.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
			if (filterSpec != null) {
				try {
					f = FilterImpl.newInstance(filterSpec);
				} catch (InvalidSyntaxException e) {
					return Collections.emptyList();
				}
			}
			Object syntheticAttr = requirement.getAttributes().get(SYNTHETIC_REQUIREMENT);
			boolean synthetic = syntheticAttr instanceof Boolean ? ((Boolean) syntheticAttr).booleanValue() : false;

			List<ModuleCapability> result;
			if (filterSpec == null) {
				result = match(null, all, synthetic);
			} else {
				String indexKey = f.getPrimaryKeyValue(name);
				if (indexKey == null) {
					result = match(f, all, synthetic);
				} else {
					Set<ModuleCapability> indexed = indexes.get(indexKey);
					if (indexed == null) {
						result = new ArrayList<>(0);
					} else {
						result = match(f, indexed, synthetic);
					}
					if (!nonStringIndexes.isEmpty()) {
						List<ModuleCapability> nonStringResult = match(f, nonStringIndexes, synthetic);
						for (ModuleCapability capability : nonStringResult) {
							if (!result.contains(capability)) {
								result.add(capability);
							}
						}
					}
				}
			}
			return result;
		}

		private List<ModuleCapability> match(Filter f, Set<ModuleCapability> candidates, boolean synthetic) {
			List<ModuleCapability> result = new ArrayList<>(1);
			for (ModuleCapability candidate : candidates) {
				if (matches(f, candidate, !synthetic && matchMandatory)) {
					result.add(candidate);
				}
			}
			return result;
		}
	}

	public static final Pattern MANDATORY_ATTR = Pattern.compile("\\(([^(=<>]+)\\s*[=<>]\\s*[^)]+\\)"); //$NON-NLS-1$
	public static final String SYNTHETIC_REQUIREMENT = "org.eclipse.osgi.container.synthetic"; //$NON-NLS-1$

	public static boolean matches(Filter f, Capability candidate, boolean matchMandatory) {
		if (f != null && !f.matches(candidate.getAttributes())) {
			return false;
		}
		if (matchMandatory) {
			// check for mandatory directive
			String mandatory = candidate.getDirectives().get(AbstractWiringNamespace.CAPABILITY_MANDATORY_DIRECTIVE);
			if (mandatory == null) {
				return true;
			}
			if (f == null) {
				return false;
			}
			Matcher matcher = MANDATORY_ATTR.matcher(f.toString());
			String[] mandatoryAttrs = ManifestElement.getArrayFromList(mandatory, ","); //$NON-NLS-1$
			boolean allPresent = true;
			for (String mandatoryAttr : mandatoryAttrs) {
				matcher.reset();
				boolean found = false;
				while (matcher.find()) {
					int numGroups = matcher.groupCount();
					for (int i = 1; i <= numGroups; i++) {
						if (mandatoryAttr.equals(matcher.group(i))) {
							found = true;
						}
					}
				}
				allPresent &= found;
			}
			return allPresent;
		}
		return true;
	}

	Map<String, NamespaceSet> namespaceSets = new HashMap<>();

	/**
	 * Adds the {@link ModuleRevision#getModuleCapabilities(String) capabilities}
	 * provided by the specified revision to this database.  These capabilities must 
	 * become available for lookup with the {@link #findCapabilities(Requirement)}
	 * method.
	 * @param revision the revision which has capabilities to add
	 * @return a collection of package names added for the osgi.wiring.package namespace
	 */
	public Collection<String> addCapabilities(ModuleRevision revision) {
		Collection<String> packageNames = null;
		for (ModuleCapability capability : revision.getModuleCapabilities(null)) {
			NamespaceSet namespaceSet = namespaceSets.get(capability.getNamespace());
			if (namespaceSet == null) {
				namespaceSet = new NamespaceSet(capability.getNamespace());
				namespaceSets.put(capability.getNamespace(), namespaceSet);
			}
			namespaceSet.addCapability(capability);
			// For the package namespace we return a list of package names.
			// This is used to clear the dynamic package miss caches.
			if (PackageNamespace.PACKAGE_NAMESPACE.equals(capability.getNamespace())) {
				Object packageName = capability.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE);
				if (packageName instanceof String) {
					if (packageNames == null) {
						packageNames = new ArrayList<>();
					}
					packageNames.add((String) packageName);
				}
			}
		}
		return packageNames == null ? Collections.<String> emptyList() : packageNames;
	}

	/**
	 * Removes the {@link ModuleRevision#getModuleCapabilities(String) capabilities}
	 * provided by the specified revision from this database.  These capabilities
	 * must no longer be available for lookup with the 
	 * {@link #findCapabilities(Requirement)} method.
	 * @param revision
	 */
	public void removeCapabilities(ModuleRevision revision) {
		for (ModuleCapability capability : revision.getModuleCapabilities(null)) {
			NamespaceSet namespaceSet = namespaceSets.get(capability.getNamespace());
			if (namespaceSet != null) {
				namespaceSet.removeCapability(capability);
			}
		}
	}

	/**
	 * Returns a mutable snapshot of capabilities that are candidates for 
	 * satisfying the specified requirement.
	 * @param requirement the requirement
	 * @return the candidates for the requirement
	 */
	public List<ModuleCapability> findCapabilities(Requirement requirement) {
		NamespaceSet namespaceSet = namespaceSets.get(requirement.getNamespace());
		if (namespaceSet == null) {
			return Collections.emptyList();
		}
		return namespaceSet.findCapabilities(requirement);
	}
}

Back to the top