Search in sources :

Example 6 with ScopeUpdate

use of org.apache.aries.subsystem.scope.ScopeUpdate 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 7 with ScopeUpdate

use of org.apache.aries.subsystem.scope.ScopeUpdate in project aries by apache.

the class GetScopeServiceTest method test4.

/**
	 * A new scope is created as a child of the root scope and the tb3 bundle
	 * is added to it. The tb3 bundle should receive and be in the new scope.
	 * The bundle is added directly as opposed to via an InstallInfo.
	 * @throws Exception
	 */
@Test
public void test4() throws Exception {
    Scope scope = getScope();
    Bundle bundle = installBundle("tb-3.jar");
    ScopeUpdate scopeUpdate = scope.newScopeUpdate();
    scopeUpdate.getBundles().remove(bundle);
    ScopeUpdate child = scopeUpdate.newChild("tb3");
    scopeUpdate.getChildren().add(child);
    child.getBundles().add(bundle);
    addPackageImportPolicy("org.osgi.framework", child);
    addPackageImportPolicy("org.apache.aries.subsystem.scope", child);
    addPackageImportPolicy("org.apache.aries.subsystem.scope.itests", child);
    addServiceImportPolicy(Scope.class, child);
    addServiceExportPolicy(ScopeProvider.class, child);
    scopeUpdate.commit();
    bundle.start();
    ServiceReference<ScopeProvider> scopeProviderRef = bundleContext.getServiceReference(ScopeProvider.class);
    ScopeProvider scopeProvider = bundleContext.getService(scopeProviderRef);
    scope = scopeProvider.getScope();
    assertEquals("tb3", scope.getName());
    assertTrue(scope.getBundles().contains(bundle));
    bundleContext.ungetService(scopeProviderRef);
    bundle.uninstall();
}
Also used : Scope(org.apache.aries.subsystem.scope.Scope) Bundle(org.osgi.framework.Bundle) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) Test(org.junit.Test)

Example 8 with ScopeUpdate

use of org.apache.aries.subsystem.scope.ScopeUpdate in project aries by apache.

the class GetScopeServiceTest method test3.

/**
	 * A new scope is created as a child of the root scope and the tb3 bundle
	 * is added to it. The tb3 bundle should receive and be in the new scope.
	 * @throws Exception
	 */
@Test
public void test3() throws Exception {
    Scope scope = getScope();
    ScopeUpdate scopeUpdate = scope.newScopeUpdate();
    ScopeUpdate child = scopeUpdate.newChild("tb3");
    scopeUpdate.getChildren().add(child);
    String location = getBundleLocation("tb-3.jar");
    URL url = new URL(location);
    InstallInfo installInfo = new InstallInfo(location, url.openStream());
    child.getBundlesToInstall().add(installInfo);
    addPackageImportPolicy("org.osgi.framework", child);
    addPackageImportPolicy("org.apache.aries.subsystem.scope", child);
    addPackageImportPolicy("org.apache.aries.subsystem.scope.itests", child);
    addServiceImportPolicy(Scope.class, child);
    addServiceExportPolicy(ScopeProvider.class, child);
    scopeUpdate.commit();
    Bundle bundle = bundleContext.getBundle(location);
    bundle.start();
    ServiceReference<ScopeProvider> scopeProviderRef = bundleContext.getServiceReference(ScopeProvider.class);
    ScopeProvider scopeProvider = bundleContext.getService(scopeProviderRef);
    scope = scopeProvider.getScope();
    assertEquals("tb3", scope.getName());
    assertTrue(scope.getBundles().contains(bundle));
    bundleContext.ungetService(scopeProviderRef);
    bundle.uninstall();
}
Also used : InstallInfo(org.apache.aries.subsystem.scope.InstallInfo) Scope(org.apache.aries.subsystem.scope.Scope) Bundle(org.osgi.framework.Bundle) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) URL(java.net.URL) Test(org.junit.Test)

Example 9 with ScopeUpdate

use of org.apache.aries.subsystem.scope.ScopeUpdate in project aries by apache.

the class BasicTest method testCreateChildScope.

@Test
public void testCreateChildScope() throws Exception {
    Scope scope = getScope();
    String name = "scope1";
    ScopeUpdate parent = scope.newScopeUpdate();
    ScopeUpdate child = parent.newChild(name);
    parent.getChildren().add(child);
    assertTrue(parent.commit());
    Collection<Scope> children = scope.getChildren();
    assertEquals(1, children.size());
    Scope feature1 = null;
    for (Scope s : children) {
        if (name.equals(s.getName())) {
            feature1 = s;
            break;
        }
    }
    assertNotNull(feature1);
    assertEmpty(feature1.getBundles());
    assertEmpty(feature1.getChildren());
    assertEquals(1, feature1.getId());
    assertNull(feature1.getLocation());
    assertEquals(name, feature1.getName());
    assertEquals(scope, feature1.getParent());
    assertEmpty(feature1.getSharePolicies(SharePolicy.TYPE_EXPORT));
    assertEmpty(feature1.getSharePolicies(SharePolicy.TYPE_IMPORT));
}
Also used : Scope(org.apache.aries.subsystem.scope.Scope) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) Test(org.junit.Test)

Example 10 with ScopeUpdate

use of org.apache.aries.subsystem.scope.ScopeUpdate in project aries by apache.

the class BasicTest method testInstallBundleIntoRootScope.

@Test
public void testInstallBundleIntoRootScope() throws Exception {
    Scope scope = getScope();
    int previousSize = scope.getBundles().size();
    String location = getBundleLocation("tb-2.jar");
    URL url = new URL(location);
    InstallInfo tb2Info = new InstallInfo(location, url.openStream());
    ScopeUpdate scopeUpdate = scope.newScopeUpdate();
    scopeUpdate.getBundlesToInstall().add(tb2Info);
    assertTrue(scopeUpdate.commit());
    Bundle b = bundleContext.getBundle(location);
    assertNotNull(b);
    Collection<Bundle> bundles = scope.getBundles();
    assertEquals(previousSize + 1, bundles.size());
    assertTrue(bundles.contains(b));
}
Also used : InstallInfo(org.apache.aries.subsystem.scope.InstallInfo) Scope(org.apache.aries.subsystem.scope.Scope) Bundle(org.osgi.framework.Bundle) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ScopeUpdate (org.apache.aries.subsystem.scope.ScopeUpdate)27 Bundle (org.osgi.framework.Bundle)22 Test (org.junit.Test)21 URL (java.net.URL)20 InstallInfo (org.apache.aries.subsystem.scope.InstallInfo)20 Scope (org.apache.aries.subsystem.scope.Scope)16 IOException (java.io.IOException)5 MalformedURLException (java.net.MalformedURLException)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 SharePolicy (org.apache.aries.subsystem.scope.SharePolicy)5 BundleException (org.osgi.framework.BundleException)5 Filter (org.osgi.framework.Filter)5 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)5 Ignore (org.junit.Ignore)4 FrameworkWiring (org.osgi.framework.wiring.FrameworkWiring)4 ServiceReference (org.osgi.framework.ServiceReference)3 HelloIsolation (org.apache.aries.subsystem.example.helloIsolation.HelloIsolation)2 CoreOptions.mavenBundle (org.ops4j.pax.exam.CoreOptions.mavenBundle)2 BundleContext (org.osgi.framework.BundleContext)2