use of org.apache.aries.subsystem.scope.InstallInfo in project aries by apache.
the class ServiceVisibilityTest method test1.
/**
* Install a bundle registering a service into the same scope as this one.
* This bundle should be able to see the service.
* @throws Exception
*/
@Test
public void test1() throws Exception {
assertTrue(getScope().getBundles().contains(bundleContext.getBundle()));
ScopeUpdate scopeUpdate = getScope().newScopeUpdate();
String location = getBundleLocation("tb-7.jar");
assertNull(bundleContext.getBundle(location));
URL url = new URL(location);
InstallInfo installInfo = new InstallInfo(location, url.openStream());
scopeUpdate.getBundlesToInstall().add(installInfo);
scopeUpdate.commit();
Bundle bundle = bundleContext.getBundle(location);
assertNotNull(bundle);
assertTrue(getScope().getBundles().contains(bundle));
bundle.start();
ServiceReference<Service> serviceRef = bundleContext.getServiceReference(Service.class);
assertNotNull(serviceRef);
Service service = bundleContext.getService(serviceRef);
assertNotNull(service);
bundleContext.ungetService(serviceRef);
bundle.uninstall();
}
use of org.apache.aries.subsystem.scope.InstallInfo in project aries by apache.
the class ServiceVisibilityTest method test2.
/**
* Install a bundle registering a service into a different scope than this
* one. This bundle should not be able to see the service.
* @throws Exception
*/
@Test
public void test2() throws Exception {
assertTrue(getScope().getBundles().contains(bundleContext.getBundle()));
String location = getBundleLocation("tb-7.jar");
assertNull(bundleContext.getBundle(location));
URL url = new URL(location);
InstallInfo installInfo = new InstallInfo(location, url.openStream());
ScopeUpdate scopeUpdate = getScope().newScopeUpdate();
ScopeUpdate child = scopeUpdate.newChild("tb7");
scopeUpdate.getChildren().add(child);
child.getBundlesToInstall().add(installInfo);
addPackageImportPolicy("org.osgi.framework", child);
addPackageImportPolicy("org.apache.aries.subsystem.scope.itests", child);
scopeUpdate.commit();
Bundle bundle = bundleContext.getBundle(location);
assertNotNull(bundle);
assertTrue(child.getScope().getBundles().contains(bundle));
bundle.start();
ServiceReference<Service> serviceRef = bundleContext.getServiceReference(Service.class);
assertNull(serviceRef);
bundle.uninstall();
}
use of org.apache.aries.subsystem.scope.InstallInfo in project aries by apache.
the class SharePolicyTest method test1.
/**
* Bundle tb5
* Bundle tb6
* tb5 imports package exported by tb6
* tb5 and tb6 in same scope
* tb5 should resolve
*
* Share policies have no effect within the same scope.
*
* @throws Exception
*/
@Test
public void test1() throws Exception {
Bundle tb5 = null;
Bundle tb6 = null;
try {
String tb5Location = getBundleLocation("tb-5.jar");
String tb6Location = getBundleLocation("tb-6.jar");
InstallInfo tb5Info = new InstallInfo(tb5Location, new URL(tb5Location));
InstallInfo tb6Info = new InstallInfo(tb6Location, new URL(tb6Location));
ScopeUpdate scopeUpdate = getScope().newScopeUpdate();
scopeUpdate.getBundlesToInstall().add(tb5Info);
scopeUpdate.commit();
tb5 = findBundleInRootScope("org.apache.aries.subsystem.scope.itests.tb5");
assertNotNull(tb5);
FrameworkWiring frameworkWiring = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
assertFalse(frameworkWiring.resolveBundles(Arrays.asList(new Bundle[] { tb5 })));
scopeUpdate = getScope().newScopeUpdate();
scopeUpdate.getBundlesToInstall().add(tb6Info);
scopeUpdate.commit();
tb6 = findBundleInRootScope("org.apache.aries.subsystem.scope.itests.tb6");
assertNotNull(tb6);
assertTrue(frameworkWiring.resolveBundles(Arrays.asList(new Bundle[] { tb5, tb6 })));
} finally {
uninstallQuietly(tb6);
uninstallQuietly(tb5);
}
}
use of org.apache.aries.subsystem.scope.InstallInfo in project aries by apache.
the class ScopeAdminTest method testBundleServiceIsolation.
@Test
public void testBundleServiceIsolation() throws Exception {
// make sure we are using a framework that provides composite admin service
assertNotNull("scope admin should not be null", scope);
System.out.println("able to get scope admin service");
bt = new BundleTracker(bundleContext, Bundle.INSTALLED | Bundle.UNINSTALLED | Bundle.ACTIVE, new BundleTrackerCustomizer() {
public synchronized Object addingBundle(Bundle bundle, BundleEvent event) {
if (event == null) {
System.out.println("ScopeAdminTest - adding Bundle: " + bundle.getSymbolicName() + " event: null");
} else {
System.out.println("ScopeAdminTest - adding Bundle: " + bundle.getSymbolicName() + " event: " + event.getType());
addEventCount++;
}
return bundle;
}
public synchronized void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
if (event == null) {
System.out.println("ScopeAdminTest - modifying Bundle: " + bundle.getSymbolicName() + " event: null");
} else {
System.out.println("ScopeAdminTest - modifying Bundle: " + bundle.getSymbolicName() + " event: " + event.getType());
modifyEventCount++;
}
}
public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
if (event == null) {
System.out.println("ScopeAdminTest - removing Bundle: " + bundle.getSymbolicName() + " event: null");
} else {
System.out.println("ScopeAdminTest - removing Bundle: " + bundle.getSymbolicName() + " event: " + event.getType());
removeEventCount++;
}
}
});
bt.open();
ScopeUpdate su = scope.newScopeUpdate();
ScopeUpdate childScopeUpdate = su.newChild("scope_test1");
su.getChildren().add(childScopeUpdate);
addPackageImportPolicy("org.osgi.framework", childScopeUpdate);
addPackageImportPolicy("org.osgi.util.tracker", childScopeUpdate);
// build up installInfo object for the scope
InstallInfo info1 = new InstallInfo("helloIsolation", new URL("mvn:org.apache.aries.subsystem/org.apache.aries.subsystem.example.helloIsolation/0.1-SNAPSHOT"));
InstallInfo info2 = new InstallInfo("helloIsolationRef", new URL("mvn:org.apache.aries.subsystem/org.apache.aries.subsystem.example.helloIsolationRef/0.1-SNAPSHOT"));
List<InstallInfo> bundlesToInstall = childScopeUpdate.getBundlesToInstall();
bundlesToInstall.add(info1);
bundlesToInstall.add(info2);
// add bundles to be installed, based on subsystem content
su.commit();
assertEquals("add event count should be 0 since 0 bundles are installed in root scope", 0, addEventCount);
assertEquals("modify event count should be 0", 0, modifyEventCount);
assertEquals("remove event count should be 0", 0, removeEventCount);
// start all bundles in the scope scope_test1
Collection<Bundle> bundlesToStart = childScopeUpdate.getScope().getBundles();
for (Bundle b : bundlesToStart) {
b.start();
}
assertEquals("add event count should be 0 since 0 bundles are installed in root scope", 0, addEventCount);
assertEquals("modify event count should be 0", 0, modifyEventCount);
assertEquals("remove event count should be 0", 0, removeEventCount);
try {
ServiceReference sr = bundleContext.getServiceReference(HelloIsolation.class.getName());
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());
for (Bundle b : buns) {
assertTrue(b.getSymbolicName().indexOf("org.apache.aries.subsystem.example.helloIsolation") > -1);
}
}
}
// 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());
assertEquals("add event count should be 1 since 1 bundles are installed", 1, addEventCount);
assertEquals("modify event count should be 0", 0, modifyEventCount);
assertEquals("remove event count should be 0", 0, removeEventCount);
// remove child scope
su = scope.newScopeUpdate();
// Collection<Scope> scopes = su.getToBeRemovedChildren();
Collection<ScopeUpdate> scopes = su.getChildren();
childScopeUpdate = scopes.iterator().next();
// obtain child scope admin from service registry
// String filter = "ScopeName=scope_test1";
// Scope childscope = getOsgiService(Scope.class, filter, DEFAULT_TIMEOUT);
Scope childScopeAdmin = childScopeUpdate.getScope();
assertEquals(scope, childScopeAdmin.getParent());
// scopes.add(childScopeAdmin);
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.apache.aries.subsystem.scope.InstallInfo 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);
}
Aggregations