Search in sources :

Example 6 with BundleRequirement

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

the class WovenClassImpl method add.

public synchronized void add(int i, String s) {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
    }
    try {
        List<BundleRequirement> reqs = ManifestParser.parseDynamicImportHeader(null, null, s);
    } catch (Exception ex) {
        RuntimeException re = new IllegalArgumentException("Unable to parse dynamic import.");
        re.initCause(ex);
        throw re;
    }
    checkImport(s);
    m_imports.add(i, s);
}
Also used : AdminPermission(org.osgi.framework.AdminPermission) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 7 with BundleRequirement

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

the class WovenClassImpl method set.

public synchronized String set(int i, String s) {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
    }
    try {
        List<BundleRequirement> reqs = ManifestParser.parseDynamicImportHeader(null, null, s);
    } catch (Exception ex) {
        RuntimeException re = new IllegalArgumentException("Unable to parse dynamic import.");
        re.initCause(ex);
        throw re;
    }
    checkImport(s);
    return m_imports.set(i, s);
}
Also used : AdminPermission(org.osgi.framework.AdminPermission) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 8 with BundleRequirement

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

the class Util method isConfigurerBundle.

/**
 * Check if the bundle contains configurations for the configurator
 * @param bundle The bundle
 * @param configuratorBundleId The bundle id of the configurator bundle to check the wiring
 * @return Set of locations or {@code null}
 */
@SuppressWarnings("unchecked")
public static Set<String> isConfigurerBundle(final Bundle bundle, final long configuratorBundleId) {
    // check for bundle wiring
    final BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
    if (bundleWiring == null) {
        return null;
    }
    // check for bundle requirement to implementation namespace
    final List<BundleRequirement> requirements = bundleWiring.getRequirements(NS_OSGI_EXTENDER);
    if (requirements == null || requirements.isEmpty()) {
        return null;
    }
    // get all wires for the implementation namespace
    final List<BundleWire> wires = bundleWiring.getRequiredWires(NS_OSGI_EXTENDER);
    for (final BundleWire wire : wires) {
        // requirement (no need to do additional checks like version etc.)
        if (wire.getProviderWiring() != null && wire.getProviderWiring().getBundle().getBundleId() == configuratorBundleId) {
            final Object val = wire.getRequirement().getAttributes().get(PROP_CONFIGURATIONS);
            if (val != null) {
                if (val instanceof String) {
                    return Collections.singleton((String) val);
                }
                if (val instanceof List) {
                    final List<String> paths = (List<String>) val;
                    final Set<String> result = new HashSet<>();
                    for (final String p : paths) {
                        result.add(p);
                    }
                    return result;
                }
                SystemLogger.error("Attribute " + PROP_CONFIGURATIONS + " for configurator requirement has an invalid type: " + val + ". Using default configuration.");
            }
            return Collections.singleton(DEFAULT_PATH);
        }
    }
    return null;
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) List(java.util.List) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) HashSet(java.util.HashSet)

Example 9 with BundleRequirement

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

the class ConfiguratorTest method setupBundle.

private Bundle setupBundle(final long id) throws Exception {
    final Bundle b = mock(Bundle.class);
    when(b.getBundleId()).thenReturn(id);
    when(b.getLastModified()).thenReturn(5L);
    when(b.getState()).thenReturn(Bundle.ACTIVE);
    final BundleWiring wiring = mock(BundleWiring.class);
    when(b.adapt(BundleWiring.class)).thenReturn(wiring);
    final BundleRequirement req = mock(BundleRequirement.class);
    when(wiring.getRequirements(Util.NS_OSGI_EXTENDER)).thenReturn(Collections.singletonList(req));
    final BundleWire wire = mock(BundleWire.class);
    when(wire.getProviderWiring()).thenReturn(wiring);
    when(wire.getRequirement()).thenReturn(req);
    when(wiring.getBundle()).thenReturn(bundle);
    when(wiring.getRequiredWires(Util.NS_OSGI_EXTENDER)).thenReturn(Collections.singletonList(wire));
    final Vector<URL> urls = new Vector<>();
    urls.add(this.getClass().getResource("/bundles/" + id + ".json"));
    when(b.findEntries("OSGI-INF/configurator", "*.json", false)).thenReturn(urls.elements());
    final BundleContext bContext = mock(BundleContext.class);
    when(b.getBundleContext()).thenReturn(bContext);
    when(bContext.getServiceReferences(ConfigurationAdmin.class, null)).thenReturn(Collections.singleton(caRef));
    when(bundleContext.getBundle(id)).thenReturn(b);
    return b;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWire(org.osgi.framework.wiring.BundleWire) Vector(java.util.Vector) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext)

Example 10 with BundleRequirement

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

the class ManifestParserTest method testAttributes.

@SuppressWarnings("unchecked")
public void testAttributes() throws BundleException {
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    headers.put(Constants.BUNDLE_SYMBOLICNAME, "com.example.test.sample");
    headers.put(Constants.PROVIDE_CAPABILITY, "com.example;theList:List<String>=\"red,green,blue\";theLong:Long=111");
    headers.put(Constants.REQUIRE_CAPABILITY, "com.example.other;theList:List<String>=\"one,two,three\";theLong:Long=999");
    BundleRevision mockBundleRevision = mock(BundleRevision.class);
    when(mockBundleRevision.getSymbolicName()).thenReturn("com.example.test.sample");
    ManifestParser mp = new ManifestParser(null, null, mockBundleRevision, headers);
    BundleCapability bc = findCapability(mp.getCapabilities(), "com.example");
    Long cLong = (Long) bc.getAttributes().get("theLong");
    assertEquals(Long.valueOf(111), cLong);
    List<String> cList = (List<String>) bc.getAttributes().get("theList");
    assertEquals(3, cList.size());
    assertTrue(cList.contains("red"));
    BundleRequirement br = findRequirement(mp.getRequirements(), "com.example.other");
    Long rLong = (Long) br.getAttributes().get("theLong");
    assertEquals(Long.valueOf(999), rLong);
    List<String> rList = (List<String>) br.getAttributes().get("theList");
    assertEquals(3, rList.size());
}
Also used : HashMap(java.util.HashMap) BundleRevision(org.osgi.framework.wiring.BundleRevision) ArrayList(java.util.ArrayList) List(java.util.List) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

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