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));
}
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);
}
}
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);
}
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();
}
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();
}
Aggregations