use of org.osgi.framework.wiring.BundleRequirement in project karaf by apache.
the class BundleWiresTest method packageWire.
private BundleWire packageWire(String packageFilter, BundleCapability bundleRef) {
BundleWire wire = c.createMock(BundleWire.class);
BundleRequirement req = packageRequirement(packageFilter);
expect(wire.getRequirement()).andReturn(req);
expect(wire.getCapability()).andReturn(bundleRef);
return wire;
}
use of org.osgi.framework.wiring.BundleRequirement in project karaf by apache.
the class BundleWiresTest method testFilterMatches.
@Test
public void testFilterMatches() throws IOException {
BundleWires wires = readFromFile();
BundleRequirement req = packageRequirement(packageFilter);
BundleCapability candidate1 = bundleCap(targetBundleId, targetBundleVersion);
List<BundleCapability> candidates = new ArrayList<>();
candidates.add(candidate1);
BundleCapability matchingCandidate = bundleCap(targetBundleId, "1.1.0");
candidates.add(matchingCandidate);
c.replay();
wires.filterMatches(req, candidates);
assertEquals(1, candidates.size());
assertEquals(candidate1, candidates.iterator().next());
c.verify();
}
use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.
the class StatefulResolver method isAllowedDynamicImport.
// This method duplicates a lot of logic from:
// ResolverImpl.getDynamicImportCandidates()
// This is only a rough check since it doesn't include resolver hooks.
boolean isAllowedDynamicImport(BundleRevision revision, String pkgName) {
// package be dynamically imported.
if ((revision.getWiring() == null) || pkgName.length() == 0) {
return false;
}
// If the revision doesn't have dynamic imports, then just return
// immediately.
List<BundleRequirement> dynamics = Util.getDynamicRequirements(revision.getWiring().getRequirements(null));
if ((dynamics == null) || dynamics.isEmpty()) {
return false;
}
// attempt to dynamically import it.
for (BundleCapability cap : revision.getWiring().getCapabilities(null)) {
if (cap.getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE) && cap.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).equals(pkgName)) {
return false;
}
}
// we cannot dynamically import it.
if (((BundleWiringImpl) revision.getWiring()).hasPackageSource(pkgName)) {
return false;
}
// Loop through the importer's dynamic requirements to determine if
// there is a matching one for the package from which we want to
// load a class.
Map<String, Object> attrs = Collections.singletonMap(BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
BundleRequirementImpl req = new BundleRequirementImpl(revision, BundleRevision.PACKAGE_NAMESPACE, Collections.EMPTY_MAP, attrs);
List<BundleCapability> candidates = findProviders(req, false);
// Try to find a dynamic requirement that matches the capabilities.
BundleRequirementImpl dynReq = null;
for (int dynIdx = 0; (candidates.size() > 0) && (dynReq == null) && (dynIdx < dynamics.size()); dynIdx++) {
for (Iterator<BundleCapability> itCand = candidates.iterator(); (dynReq == null) && itCand.hasNext(); ) {
Capability cap = itCand.next();
if (CapabilitySet.matches(cap, ((BundleRequirementImpl) dynamics.get(dynIdx)).getFilter())) {
dynReq = (BundleRequirementImpl) dynamics.get(dynIdx);
}
}
}
// any candidates that do not match it.
if (dynReq != null) {
for (Iterator<BundleCapability> itCand = candidates.iterator(); itCand.hasNext(); ) {
Capability cap = itCand.next();
if (!CapabilitySet.matches(cap, dynReq.getFilter())) {
itCand.remove();
}
}
} else {
candidates.clear();
}
return !candidates.isEmpty();
}
use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.
the class ManifestParserTest method testConvertNativeCode.
public void testConvertNativeCode() throws InvalidSyntaxException {
List<NativeLibraryClause> nativeLibraryClauses = new ArrayList<NativeLibraryClause>();
String[] libraryFiles = { "lib/http.dll", "lib/zlib.dll" };
String[] osNames = { "Windows95", "Windows98", "WindowsNT" };
String[] processors = { "x86" };
String[] osVersions = null;
String[] languages = { "en", "se" };
String selectionFilter = "(com.acme.windowing=win32)";
NativeLibraryClause clause = new NativeLibraryClause(libraryFiles, osNames, processors, osVersions, languages, selectionFilter);
BundleRevision owner = mock(BundleRevision.class);
nativeLibraryClauses.add(clause);
List<BundleRequirement> nativeBundleReq = ManifestParser.convertNativeCode(owner, nativeLibraryClauses, false);
BundleRequirement ir = findRequirement(nativeBundleReq, NativeNamespace.NATIVE_NAMESPACE);
String filterStr = (String) ir.getDirectives().get(NativeNamespace.REQUIREMENT_FILTER_DIRECTIVE);
Filter actualFilter = FrameworkUtil.createFilter(filterStr);
Filter expectedFilter = FrameworkUtil.createFilter("(&(|" + "(osgi.native.osname~=windows95)(osgi.native.osname~=windows98)(osgi.native.osname~=windowsnt)" + ")" + "(osgi.native.processor~=x86)" + "(|(osgi.native.language~=en)" + "(osgi.native.language~=se)" + ")" + "(com.acme.windowing=win32))");
assertEquals("Filter Should contain native requirements", expectedFilter, actualFilter);
}
use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.
the class Inspect method aggregateRequirements.
private static Map<BundleRequirement, List<BundleWire>> aggregateRequirements(List<String> namespace, List<BundleWire> wires) {
// Aggregate matching capabilities.
Map<BundleRequirement, List<BundleWire>> map = new HashMap<BundleRequirement, List<BundleWire>>();
for (BundleWire wire : wires) {
if (matchNamespace(namespace, wire.getRequirement().getNamespace())) {
List<BundleWire> providers = map.get(wire.getRequirement());
if (providers == null) {
providers = new ArrayList<BundleWire>();
map.put(wire.getRequirement(), providers);
}
providers.add(wire);
}
}
return map;
}
Aggregations