Search in sources :

Example 11 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.

the class ManifestParser method parseBreeHeader.

private static List<BundleRequirement> parseBreeHeader(String header, BundleRevision owner) {
    List<String> filters = new ArrayList<String>();
    for (String entry : parseDelimitedString(header, ",")) {
        List<String> names = parseDelimitedString(entry, "/");
        List<String> left = parseDelimitedString(names.get(0), "-");
        String lName = left.get(0);
        Version lVer;
        try {
            lVer = Version.parseVersion(left.get(1));
        } catch (Exception ex) {
            // Version doesn't parse. Make it part of the name.
            lName = names.get(0);
            lVer = null;
        }
        String rName = null;
        Version rVer = null;
        if (names.size() > 1) {
            List<String> right = parseDelimitedString(names.get(1), "-");
            rName = right.get(0);
            try {
                rVer = Version.parseVersion(right.get(1));
            } catch (Exception ex) {
                rName = names.get(1);
                rVer = null;
            }
        }
        String versionClause;
        if (lVer != null) {
            if ((rVer != null) && (!rVer.equals(lVer))) {
                // Both versions are defined, but different. Make each of them part of the name
                lName = names.get(0);
                rName = names.get(1);
                versionClause = null;
            } else {
                versionClause = getBreeVersionClause(lVer);
            }
        } else {
            versionClause = getBreeVersionClause(rVer);
        }
        if ("J2SE".equals(lName)) {
            // J2SE is not used in the Capability variant of BREE, use JavaSE here
            // This can only happen with the lName part...
            lName = "JavaSE";
        }
        String nameClause;
        if (rName != null)
            nameClause = "(" + ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE + "=" + lName + "/" + rName + ")";
        else
            nameClause = "(" + ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE + "=" + lName + ")";
        String filter;
        if (versionClause != null)
            filter = "(&" + nameClause + versionClause + ")";
        else
            filter = nameClause;
        filters.add(filter);
    }
    if (filters.size() == 0) {
        return Collections.emptyList();
    } else {
        String reqFilter;
        if (filters.size() == 1) {
            reqFilter = filters.get(0);
        } else {
            // If there are more BREE filters, we need to or them together
            StringBuilder sb = new StringBuilder("(|");
            for (String f : filters) {
                sb.append(f);
            }
            sb.append(")");
            reqFilter = sb.toString();
        }
        SimpleFilter sf = SimpleFilter.parse(reqFilter);
        return Collections.<BundleRequirement>singletonList(new BundleRequirementImpl(owner, ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE, Collections.singletonMap(ExecutionEnvironmentNamespace.REQUIREMENT_FILTER_DIRECTIVE, reqFilter), Collections.<String, Object>emptyMap(), sf));
    }
}
Also used : Version(org.osgi.framework.Version) SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl)

Example 12 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.

the class ManifestParser method convertNativeCode.

static List<BundleRequirement> convertNativeCode(BundleRevision owner, List<NativeLibraryClause> nativeLibraryClauses, boolean hasOptionalLibraryDirective) {
    List<BundleRequirement> result = new ArrayList<BundleRequirement>();
    List<SimpleFilter> nativeFilterClauseList = new ArrayList<SimpleFilter>();
    if (nativeLibraryClauses != null && !nativeLibraryClauses.isEmpty()) {
        for (NativeLibraryClause clause : nativeLibraryClauses) {
            String[] osNameArray = clause.getOSNames();
            String[] osVersionArray = clause.getOSVersions();
            String[] processorArray = clause.getProcessors();
            String[] languageArray = clause.getLanguages();
            String currentSelectionFilter = clause.getSelectionFilter();
            List<SimpleFilter> nativeFilterList = new ArrayList<SimpleFilter>();
            if (osNameArray != null && osNameArray.length > 0) {
                nativeFilterList.add(buildFilterFromArray(NativeNamespace.CAPABILITY_OSNAME_ATTRIBUTE, osNameArray, SimpleFilter.APPROX));
            }
            if (osVersionArray != null && osVersionArray.length > 0) {
                nativeFilterList.add(buildFilterFromArray(NativeNamespace.CAPABILITY_OSVERSION_ATTRIBUTE, osVersionArray, SimpleFilter.EQ));
            }
            if (processorArray != null && processorArray.length > 0) {
                nativeFilterList.add(buildFilterFromArray(NativeNamespace.CAPABILITY_PROCESSOR_ATTRIBUTE, processorArray, SimpleFilter.APPROX));
            }
            if (languageArray != null && languageArray.length > 0) {
                nativeFilterList.add(buildFilterFromArray(NativeNamespace.CAPABILITY_LANGUAGE_ATTRIBUTE, languageArray, SimpleFilter.APPROX));
            }
            if (currentSelectionFilter != null) {
                nativeFilterList.add(SimpleFilter.parse(currentSelectionFilter));
            }
            if (!nativeFilterList.isEmpty()) {
                SimpleFilter nativeClauseFilter = new SimpleFilter(null, nativeFilterList, SimpleFilter.AND);
                nativeFilterClauseList.add(nativeClauseFilter);
            }
        }
        Map<String, String> requirementDirectives = new HashMap<String, String>();
        SimpleFilter consolidatedNativeFilter = null;
        if (hasOptionalLibraryDirective) {
            requirementDirectives.put(NativeNamespace.REQUIREMENT_RESOLUTION_DIRECTIVE, NativeNamespace.RESOLUTION_OPTIONAL);
        }
        if (nativeFilterClauseList.size() > 1) {
            consolidatedNativeFilter = new SimpleFilter(null, nativeFilterClauseList, SimpleFilter.OR);
            requirementDirectives.put(NativeNamespace.REQUIREMENT_FILTER_DIRECTIVE, consolidatedNativeFilter.toString());
        } else if (nativeFilterClauseList.size() == 1) {
            consolidatedNativeFilter = nativeFilterClauseList.get(0);
            requirementDirectives.put(NativeNamespace.REQUIREMENT_FILTER_DIRECTIVE, consolidatedNativeFilter.toString());
        }
        if (requirementDirectives.size() > 0) {
            result.add(new BundleRequirementImpl(owner, NativeNamespace.NATIVE_NAMESPACE, requirementDirectives, Collections.<String, Object>emptyMap(), consolidatedNativeFilter));
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) ArrayList(java.util.ArrayList) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl)

Example 13 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.

the class ManifestParser method convertImports.

private static List<BundleRequirement> convertImports(List<ParsedHeaderClause> clauses, BundleRevision owner) {
    // Now convert generic header clauses into requirements.
    List<BundleRequirement> reqList = new ArrayList<BundleRequirement>();
    for (ParsedHeaderClause clause : clauses) {
        for (String path : clause.m_paths) {
            // Prepend the package name to the array of attributes.
            Map<String, Object> attrs = clause.m_attrs;
            // Note that we use a linked hash map here to ensure the
            // package attribute is first, which will make indexing
            // more efficient.
            // TODO: OSGi R4.3 - This is ordering is kind of hacky.
            // Prepend the package name to the array of attributes.
            Map<String, Object> newAttrs = new LinkedHashMap<String, Object>(attrs.size() + 1);
            // We want this first from an indexing perspective.
            newAttrs.put(BundleRevision.PACKAGE_NAMESPACE, path);
            newAttrs.putAll(attrs);
            // But we need to put it again to make sure it wasn't overwritten.
            newAttrs.put(BundleRevision.PACKAGE_NAMESPACE, path);
            // Create filter now so we can inject filter directive.
            SimpleFilter sf = SimpleFilter.convert(newAttrs);
            // Inject filter directive.
            // TODO: OSGi R4.3 - Can we insert this on demand somehow?
            Map<String, String> dirs = clause.m_dirs;
            Map<String, String> newDirs = new HashMap<String, String>(dirs.size() + 1);
            newDirs.putAll(dirs);
            newDirs.put(Constants.FILTER_DIRECTIVE, sf.toString());
            // Create package requirement and add to requirement list.
            reqList.add(new BundleRequirementImpl(owner, BundleRevision.PACKAGE_NAMESPACE, newDirs, Collections.EMPTY_MAP, sf));
        }
    }
    return reqList;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) ArrayList(java.util.ArrayList) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.

the class ManifestParser method convertRequireCapabilities.

private static List<BundleRequirement> convertRequireCapabilities(List<ParsedHeaderClause> clauses, BundleRevision owner) throws BundleException {
    // Now convert generic header clauses into requirements.
    List<BundleRequirement> reqList = new ArrayList<BundleRequirement>();
    for (ParsedHeaderClause clause : clauses) {
        try {
            String filterStr = clause.m_dirs.get(Constants.FILTER_DIRECTIVE);
            SimpleFilter sf = (filterStr != null) ? SimpleFilter.parse(filterStr) : new SimpleFilter(null, null, SimpleFilter.MATCH_ALL);
            for (String path : clause.m_paths) {
                if (path.startsWith("osgi.wiring.")) {
                    throw new BundleException("Manifest cannot use Require-Capability for '" + path + "' namespace.");
                }
                // Create requirement and add to requirement list.
                reqList.add(new BundleRequirementImpl(owner, path, clause.m_dirs, clause.m_attrs, sf));
            }
        } catch (Exception ex) {
            throw new BundleException("Error creating requirement: " + ex);
        }
    }
    return reqList;
}
Also used : SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl) BundleException(org.osgi.framework.BundleException)

Example 15 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project rt.equinox.framework by eclipse.

the class ClassLoadingBundleTests method testBug348805.

public void testBug348805() {
    final boolean[] endCalled = { false };
    ResolverHookFactory error = new ResolverHookFactory() {

        public ResolverHook begin(Collection triggers) {
            return new ResolverHook() {

                public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
                // Nothing
                }

                public void filterResolvable(Collection candidates) {
                    throw new RuntimeException("Error");
                }

                public void filterMatches(BundleRequirement requirement, Collection candidates) {
                // Nothing
                }

                public void end() {
                    endCalled[0] = true;
                }
            };
        }
    };
    ServiceRegistration reg = OSGiTestsActivator.getContext().registerService(ResolverHookFactory.class, error, null);
    try {
        // $NON-NLS-1$
        Bundle test = installer.installBundle("test");
        try {
            test.start();
            fail("Should not be able to start this bundle");
        } catch (BundleException e) {
            // expected
            assertEquals("Wrong exception type.", BundleException.REJECTED_BY_HOOK, e.getType());
        }
    } catch (BundleException e) {
        fail("Unexpected install fail", e);
    } finally {
        reg.unregister();
    }
    assertTrue("end is not called", endCalled[0]);
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) Bundle(org.osgi.framework.Bundle) Collection(java.util.Collection) BundleException(org.osgi.framework.BundleException) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

BundleRequirement (org.osgi.framework.wiring.BundleRequirement)55 BundleCapability (org.osgi.framework.wiring.BundleCapability)23 ArrayList (java.util.ArrayList)20 BundleWire (org.osgi.framework.wiring.BundleWire)18 BundleRevision (org.osgi.framework.wiring.BundleRevision)17 HashMap (java.util.HashMap)15 Bundle (org.osgi.framework.Bundle)15 Test (org.junit.Test)14 BundleException (org.osgi.framework.BundleException)11 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)10 BundleWiring (org.osgi.framework.wiring.BundleWiring)10 List (java.util.List)9 Collection (java.util.Collection)8 BundleRequirementImpl (org.apache.felix.framework.wiring.BundleRequirementImpl)8 ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)8 CompositeData (javax.management.openmbean.CompositeData)6 Module (org.eclipse.osgi.container.Module)6 ModuleContainer (org.eclipse.osgi.container.ModuleContainer)6 DummyContainerAdaptor (org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor)6 SimpleFilter (org.apache.felix.framework.capabilityset.SimpleFilter)5