Search in sources :

Example 1 with HelloIsolation

use of org.apache.aries.subsystem.example.helloIsolation.HelloIsolation 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);
}
Also used : InstallInfo(org.apache.aries.subsystem.scope.InstallInfo) Bundle(org.osgi.framework.Bundle) BundleTracker(org.osgi.util.tracker.BundleTracker) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) BundleTrackerCustomizer(org.osgi.util.tracker.BundleTrackerCustomizer) URL(java.net.URL) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) HelloIsolation(org.apache.aries.subsystem.example.helloIsolation.HelloIsolation) Scope(org.apache.aries.subsystem.scope.Scope) BundleEvent(org.osgi.framework.BundleEvent) Test(org.junit.Test)

Example 2 with HelloIsolation

use of org.apache.aries.subsystem.example.helloIsolation.HelloIsolation in project aries by apache.

the class ScopeSecurityTest method testScopeSecurityWithServiceShared.

// @Test
public void testScopeSecurityWithServiceShared() throws Exception {
    SecurityManager security = System.getSecurityManager();
    assertNotNull("Security manager should not be null", security);
    Bundle[] bundles = bundleContext.getBundles();
    for (Bundle b : bundles) {
        // set up condition permission for scope
        if (b.getSymbolicName().indexOf("subsystem.scope.impl") > -1) {
            ServiceReference permRef = bundleContext.getServiceReference(ConditionalPermissionAdmin.class.getName());
            ConditionalPermissionAdmin permAdmin = (ConditionalPermissionAdmin) bundleContext.getService(permRef);
            ConditionalPermissionUpdate update = permAdmin.newConditionalPermissionUpdate();
            List<ConditionalPermissionInfo> infos = update.getConditionalPermissionInfos();
            // infos.clear();
            // set up the conditionInfo
            ConditionInfo[] conditionInfo = new ConditionInfo[] { new ConditionInfo("org.osgi.service.condpermadmin.BundleLocationCondition", new String[] { b.getLocation() }) };
            // Set up permissions which are common to all applications
            infos.add(permAdmin.newConditionalPermissionInfo(null, conditionInfo, adminAllowInfo, "allow"));
            update.commit();
        }
    }
    ScopeUpdate su = scope.newScopeUpdate();
    ScopeUpdate childScopeUpdate = su.newChild("scope_test1");
    Map<String, List<SharePolicy>> sharePolicies = childScopeUpdate.getSharePolicies(SharePolicy.TYPE_EXPORT);
    final Filter filter1 = FrameworkUtil.createFilter("(&" + "(osgi.package=org.apache.aries.subsystem.example.helloIsolation)" + ")");
    final Filter filter2 = FrameworkUtil.createFilter("(&" + "(osgi.service=org.apache.aries.subsystem.example.helloIsolation.HelloIsolation)" + ")");
    List<SharePolicy> packagePolicies = sharePolicies.get(BundleRevision.PACKAGE_NAMESPACE);
    if (packagePolicies == null) {
        packagePolicies = new ArrayList<SharePolicy>();
        sharePolicies.put(BundleRevision.PACKAGE_NAMESPACE, packagePolicies);
    }
    packagePolicies.add(new SharePolicy(SharePolicy.TYPE_EXPORT, BundleRevision.PACKAGE_NAMESPACE, filter1));
    List<SharePolicy> servicePolicies = sharePolicies.get("scope.share.service");
    if (servicePolicies == null) {
        servicePolicies = new ArrayList<SharePolicy>();
        sharePolicies.put("scope.share.service", servicePolicies);
    }
    servicePolicies.add(new SharePolicy(SharePolicy.TYPE_EXPORT, "scope.share.service", filter2));
    // 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
    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");
            HelloIsolation hi = (HelloIsolation) childScopebundleContext.getService(sr);
            hi.hello();
            Permission permission = new PackagePermission("*", PackagePermission.IMPORT);
            hi.checkPermission(permission);
        }
    }
    // 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 : SharePolicy(org.apache.aries.subsystem.scope.SharePolicy) URL(java.net.URL) HelloIsolation(org.apache.aries.subsystem.example.helloIsolation.HelloIsolation) ConditionalPermissionInfo(org.osgi.service.condpermadmin.ConditionalPermissionInfo) PackagePermission(org.osgi.framework.PackagePermission) Permission(java.security.Permission) ArrayList(java.util.ArrayList) List(java.util.List) ConditionalPermissionUpdate(org.osgi.service.condpermadmin.ConditionalPermissionUpdate) ConditionInfo(org.osgi.service.condpermadmin.ConditionInfo) ConditionalPermissionAdmin(org.osgi.service.condpermadmin.ConditionalPermissionAdmin) 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) PackagePermission(org.osgi.framework.PackagePermission) ServiceReference(org.osgi.framework.ServiceReference) Scope(org.apache.aries.subsystem.scope.Scope) Filter(org.osgi.framework.Filter) BundleContext(org.osgi.framework.BundleContext)

Example 3 with HelloIsolation

use of org.apache.aries.subsystem.example.helloIsolation.HelloIsolation in project aries by apache.

the class Activator method start.

/*
   * (non-Javadoc)
   * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
   */
public void start(BundleContext context) throws Exception {
    System.out.println("bundle helloIsolationRef start");
    // check to see if we can see helloIsolation service from service registry
    ServiceReference sr = context.getServiceReference(HelloIsolation.class.getName());
    if (sr != null) {
        System.out.println("Able to obtain service reference from bundle " + sr.getBundle().getSymbolicName() + "_" + sr.getBundle().getVersion().toString());
        HelloIsolation hi = (HelloIsolation) context.getService(sr);
        hi.hello();
    }
}
Also used : HelloIsolation(org.apache.aries.subsystem.example.helloIsolation.HelloIsolation) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

HelloIsolation (org.apache.aries.subsystem.example.helloIsolation.HelloIsolation)3 ServiceReference (org.osgi.framework.ServiceReference)3 URL (java.net.URL)2 InstallInfo (org.apache.aries.subsystem.scope.InstallInfo)2 Scope (org.apache.aries.subsystem.scope.Scope)2 ScopeUpdate (org.apache.aries.subsystem.scope.ScopeUpdate)2 Bundle (org.osgi.framework.Bundle)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 Permission (java.security.Permission)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 SharePolicy (org.apache.aries.subsystem.scope.SharePolicy)1 Test (org.junit.Test)1 CoreOptions.mavenBundle (org.ops4j.pax.exam.CoreOptions.mavenBundle)1 BundleContext (org.osgi.framework.BundleContext)1 BundleEvent (org.osgi.framework.BundleEvent)1 BundleException (org.osgi.framework.BundleException)1 Filter (org.osgi.framework.Filter)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1