Search in sources :

Example 46 with BundleContext

use of org.osgi.framework.BundleContext in project aries by apache.

the class ProviderBundleTrackerCustomizerTest method testAddingBundleSPIBundle.

@Test
public void testAddingBundleSPIBundle() throws Exception {
    BundleContext implBC = mockSPIBundleContext(EasyMock.createNiceMock(ServiceRegistration.class));
    Bundle spiBundle = mockSPIBundle(implBC);
    ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(EasyMock.createNiceMock(BaseActivator.class), spiBundle);
    assertNull("The SpiFly bundle itself should be ignored", customizer.addingBundle(spiBundle, null));
}
Also used : Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 47 with BundleContext

use of org.osgi.framework.BundleContext in project aries by apache.

the class ScopeManager method event.

public void event(BundleEvent event, Collection<BundleContext> contexts) {
    int type = event.getType();
    Bundle source = event.getBundle();
    if (type == BundleEvent.INSTALLED) {
        // For bundle installed events, the origin is the bundle whose context
        // was used to install the source bundle. In this case, we need to be
        // sure the origin bundle is assigned to a scope. This is necessary to
        // ensure the next step will succeed. This condition may occur, for
        // example, during Scope Admin initialization.
        Bundle origin = event.getOrigin();
        synchronized (scopes) {
            if (!scopes.contains(origin)) {
                scopes.addBundle(origin);
            }
            // installed via Scope Admin.
            if (origin.getBundleId() != bundleContext.getBundle().getBundleId()) {
                scopes.addBundle(source, scopes.getScope(origin));
            } else // Otherwise, assign the installed bundle to the scope designated by the scope update.
            {
                ScopeImpl scope = installingBundleToScope.remove(source.getLocation());
                scopes.addBundle(source, scope);
            }
        }
    }
    // system bundle, which receives all events and sends events to all listeners.
    if (source.getBundleId() == 0)
        return;
    ScopeImpl scope = scopes.getScope(source);
    Collection<Bundle> bundles = scope.getBundles();
    for (Iterator<BundleContext> i = contexts.iterator(); i.hasNext(); ) {
        BundleContext bc = i.next();
        Bundle b = bc.getBundle();
        if (b.getBundleId() != 0 && !bundles.contains(b)) {
            i.remove();
        }
    }
    if (type == BundleEvent.UNINSTALLED) {
        // For bundle uninstalled events, remove the bundle from Scope Admin.
        // Note this must be done after filtering the event listeners or the
        // bundle will get added back.
        scopes.removeBundle(source);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext)

Example 48 with BundleContext

use of org.osgi.framework.BundleContext in project aries by apache.

the class ScopeSecurityTest method testScopeSecurityWithServiceIsolation.

//@Test
public void testScopeSecurityWithServiceIsolation() throws Exception {
    ScopeUpdate su = scope.newScopeUpdate();
    ScopeUpdate childScopeUpdate = su.newChild("scope_test1");
    // build up installInfo object for the scope
    InstallInfo info1 = new InstallInfo("helloIsolation", new URL("mvn:org.apache.aries.subsystem.example/org.apache.aries.subsystem.example.helloIsolation/0.4-SNAPSHOT"));
    InstallInfo info2 = new InstallInfo("helloIsolationRef", new URL("mvn:org.apache.aries.subsystem.example/org.apache.aries.subsystem.example.helloIsolationRef/0.4-SNAPSHOT"));
    List<InstallInfo> bundlesToInstall = childScopeUpdate.getBundlesToInstall();
    bundlesToInstall.add(info1);
    bundlesToInstall.add(info2);
    // add bundles to be installed, based on subsystem content
    su.commit();
    // start all bundles in the scope scope_test1
    Collection<Bundle> bundlesToStart = childScopeUpdate.getBundles();
    for (Bundle b : bundlesToStart) {
        b.start();
    }
    try {
        ServiceReference sr = bundleContext.getServiceReference("org.apache.aries.subsystem.example.helloIsolation.HelloIsolation");
        fail("should not be able to get the sr for HelloIsolation service");
    } catch (Exception ex) {
    // expected 
    } catch (Error er) {
    // expected
    }
    // test bundle find hooks
    Bundle[] bundles = bundleContext.getBundles();
    for (Bundle b : bundles) {
        System.out.println("Bundle is " + b.getBundleId() + ": " + b.getSymbolicName());
        if (b.getSymbolicName().indexOf("org.apache.aries.subsystem.example.helloIsolation") > -1) {
            fail("bundles with name starts with org.apache.aries.subsystem.example.helloIsolation should be in a different scope");
        }
    }
    // test bundle service find hook
    //ServiceReference sr = bundleContext.getServiceReference(HelloIsolation.class.getName());
    //assertNull("sr should be null", sr);
    Collection<Scope> children = scope.getChildren();
    assertEquals(1, children.size());
    for (Scope child : children) {
        if (child.getName().equals("scope_test1")) {
            Collection<Bundle> buns = child.getBundles();
            assertEquals(2, buns.size());
            assertEquals(0, child.getChildren().size());
            BundleContext childScopebundleContext = null;
            for (Bundle b : buns) {
                assertTrue(b.getSymbolicName().indexOf("org.apache.aries.subsystem.example.helloIsolation") > -1);
                if (b.getSymbolicName().indexOf("org.apache.aries.subsystem.example.helloIsolationRef") > -1) {
                    childScopebundleContext = b.getBundleContext();
                }
            }
            assertNotNull(childScopebundleContext);
            ServiceReference sr = childScopebundleContext.getServiceReference("org.apache.aries.subsystem.example.helloIsolation.HelloIsolation");
            assertNotNull("sr is not null", sr);
            System.out.println("got the sr, go get service next");
            Object obj = childScopebundleContext.getService(sr);
        //HelloIsolation hi = (HelloIsolation)childScopebundleContext.getService(sr);
        }
    }
    // install a test bundle in the root scope
    URL url = new URL("mvn:org.apache.felix/org.apache.felix.fileinstall/2.0.8");
    bundleContext.installBundle("org.apache.felix.fileinstall-rootScope", url.openStream());
    // remove child scope
    su = scope.newScopeUpdate();
    Collection<ScopeUpdate> scopes = su.getChildren();
    // obtain child scope admin from service registry
    //        String filter = "ScopeName=scope_test1";
    Scope childScopeAdmin = childScopeUpdate.getScope();
    assertEquals(scope, childScopeAdmin.getParent());
    scopes.remove(childScopeUpdate);
    su.commit();
    assertFalse(scope.getChildren().contains(childScopeAdmin));
    su = scope.newScopeUpdate();
    assertFalse(su.getChildren().contains(childScopeUpdate));
//        childScopeAdmin = null;
//        try {
//            childScopeAdmin = getOsgiService(Scope.class, filter, DEFAULT_TIMEOUT);
//        } catch (Exception ex) {
//            // ignore
//        }
//        assertNull("scope admin service for the scope should be unregistered", childScopeAdmin);
}
Also used : InstallInfo(org.apache.aries.subsystem.scope.InstallInfo) Bundle(org.osgi.framework.Bundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) URL(java.net.URL) ServiceReference(org.osgi.framework.ServiceReference) Scope(org.apache.aries.subsystem.scope.Scope) BundleContext(org.osgi.framework.BundleContext)

Example 49 with BundleContext

use of org.osgi.framework.BundleContext in project aries by apache.

the class SubsystemTest method getRegionContextBundle.

protected Bundle getRegionContextBundle(Subsystem subsystem) {
    BundleContext bc = subsystem.getBundleContext();
    assertNotNull("No region context bundle", bc);
    return bc.getBundle();
}
Also used : BundleContext(org.osgi.framework.BundleContext) RichBundleContext(org.apache.aries.itest.RichBundleContext)

Example 50 with BundleContext

use of org.osgi.framework.BundleContext in project aries by apache.

the class Activator method start.

@Override
public void start(BundleContext context) throws Exception {
    this.context = context;
    synchronized (this) {
        open = true;
    }
    Dictionary<String, Object> properties = getMSProperties();
    logger.info("Registering for configuration updates {}", properties);
    ManagedService service = c -> configurationUpdated(c, false);
    msReg = context.registerService(ManagedService.class, service, properties);
    new Thread(() -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        configurationUpdated(null, true);
    }).start();
}
Also used : ManagedService(org.osgi.service.cm.ManagedService) Logger(org.slf4j.Logger) Constants(org.osgi.framework.Constants) Enumeration(java.util.Enumeration) BundleActivator(org.osgi.framework.BundleActivator) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) TransactionControl(org.osgi.service.transaction.control.TransactionControl) BundleContext(org.osgi.framework.BundleContext) Map(java.util.Map) RECREATE(org.apache.aries.tx.control.service.xa.impl.Activator.ChangeType.RECREATE) ManagedService(org.osgi.service.cm.ManagedService) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

BundleContext (org.osgi.framework.BundleContext)524 Test (org.junit.Test)186 Bundle (org.osgi.framework.Bundle)175 ServiceReference (org.osgi.framework.ServiceReference)126 File (java.io.File)82 Hashtable (java.util.Hashtable)75 HashMap (java.util.HashMap)70 Equinox (org.eclipse.osgi.launch.Equinox)51 BundleException (org.osgi.framework.BundleException)51 ArrayList (java.util.ArrayList)50 LinkedHashMap (java.util.LinkedHashMap)45 ServiceRegistration (org.osgi.framework.ServiceRegistration)41 IOException (java.io.IOException)40 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)38 URL (java.net.URL)33 Dictionary (java.util.Dictionary)32 Matchers.anyString (org.mockito.Matchers.anyString)28 Before (org.junit.Before)26 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)26 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)18