Search in sources :

Example 36 with FrameworkWiring

use of org.osgi.framework.wiring.FrameworkWiring in project aries by apache.

the class FrameworkMBeanTest method testRefreshBundleAndWait.

@Test
public void testRefreshBundleAndWait() throws Exception {
    FrameworkWiring frameworkWiring = (FrameworkWiring) context().getBundle(0).adapt(FrameworkWiring.class);
    Bundle bundleA = getBundleByName("org.apache.aries.jmx.test.bundlea");
    Bundle bundleB = getBundleByName("org.apache.aries.jmx.test.bundleb");
    BundleWiring bw = (BundleWiring) bundleB.adapt(BundleWiring.class);
    List<BundleWire> initialRequiredWires = bw.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    assertEquals(1, initialRequiredWires.size());
    BundleWire wire = initialRequiredWires.get(0);
    Map<String, Object> capabilityAttributes = wire.getCapability().getAttributes();
    assertEquals("Precondition", bundleA.getSymbolicName(), capabilityAttributes.get(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE));
    assertEquals("Precondition", new Version("1.0"), capabilityAttributes.get(Constants.BUNDLE_VERSION_ATTRIBUTE));
    assertEquals("Precondition", "org.apache.aries.jmx.test.bundlea.api", capabilityAttributes.get(BundleRevision.PACKAGE_NAMESPACE));
    // Create an updated version of Bundle A, which an extra export and version 1.1
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    manifest.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, "org.apache.aries.jmx.test.bundlea");
    manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.1");
    manifest.getMainAttributes().putValue(Constants.EXPORT_PACKAGE, "org.apache.aries.jmx.test.bundlea.api,org.apache.aries.jmx.test.bundlea.impl");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JarOutputStream jos = new JarOutputStream(baos, manifest);
    addResourceToJar("org/apache/aries/jmx/test/bundlea/api/InterfaceA.class", jos, bundleA);
    addResourceToJar("org/apache/aries/jmx/test/bundlea/impl/A2.class", jos, bundleA);
    jos.close();
    assertEquals("Precondition", 0, frameworkWiring.getRemovalPendingBundles().size());
    assertEquals(0, framework.getRemovalPendingBundles().length);
    assertEquals("Precondition", 1, ((BundleRevisions) bundleA.adapt(BundleRevisions.class)).getRevisions().size());
    bundleA.update(new ByteArrayInputStream(baos.toByteArray()));
    assertEquals("There should be 2 revisions now", 2, ((BundleRevisions) bundleA.adapt(BundleRevisions.class)).getRevisions().size());
    assertEquals("No refresh called, the bundle wiring for B should still be the old one", bw, bundleB.adapt(BundleWiring.class));
    assertEquals("Precondition", 1, frameworkWiring.getRemovalPendingBundles().size());
    assertEquals(1, framework.getRemovalPendingBundles().length);
    assertEquals(((Bundle) frameworkWiring.getRemovalPendingBundles().iterator().next()).getBundleId(), framework.getRemovalPendingBundles()[0]);
    assertTrue(framework.refreshBundleAndWait(bundleB.getBundleId()));
    List<BundleWire> requiredWires = ((BundleWiring) bundleB.adapt(BundleWiring.class)).getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    assertEquals(2, requiredWires.size());
    List<String> imported = new ArrayList<String>();
    for (BundleWire w : requiredWires) {
        Map<String, Object> ca = w.getCapability().getAttributes();
        assertEquals(bundleA.getSymbolicName(), ca.get(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE));
        imported.add(ca.get(BundleRevision.PACKAGE_NAMESPACE).toString());
        if ("org.apache.aries.jmx.test.bundlea.impl".equals(ca.get(BundleRevision.PACKAGE_NAMESPACE))) {
            // Came across an issue where equinox was reporting the other package as still coming from from the 1.0 bundle
            // not sure if this is a bug or not...
            assertEquals(new Version("1.1"), ca.get(Constants.BUNDLE_VERSION_ATTRIBUTE));
        }
    }
    assertEquals(Arrays.asList("org.apache.aries.jmx.test.bundlea.api", "org.apache.aries.jmx.test.bundlea.impl"), imported);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) BundleWire(org.osgi.framework.wiring.BundleWire) Version(org.osgi.framework.Version) ByteArrayInputStream(java.io.ByteArrayInputStream) BundleRevisions(org.osgi.framework.wiring.BundleRevisions) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 37 with FrameworkWiring

use of org.osgi.framework.wiring.FrameworkWiring in project aries by apache.

the class SharePolicyTest method test4.

/**
 * Bundle tb5
 * Bundle tb6
 * tb5 imports package exported by tb6
 * tb5 in child scope of root
 * tb6 in different child scope of root
 * tb6 scope exports tb6 package
 * root scope exports tb6 package
 * tb5 scope imports tb6 package
 * tb5 should resolve
 *
 * @throws Exception
 */
@Test
public void test4() 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 rootUpdate = getScope().newScopeUpdate();
        addPackageExportPolicy("org.apache.aries.subsystem.scope.itests.tb6", rootUpdate);
        ScopeUpdate tb5Update = rootUpdate.newChild("tb5");
        rootUpdate.getChildren().add(tb5Update);
        tb5Update.getBundlesToInstall().add(tb5Info);
        addPackageImportPolicy("org.apache.aries.subsystem.scope.itests.tb6", tb5Update);
        rootUpdate.commit();
        tb5 = findBundle("org.apache.aries.subsystem.scope.itests.tb5", tb5Update.getScope());
        assertNotNull(tb5);
        FrameworkWiring frameworkWiring = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
        assertFalse(frameworkWiring.resolveBundles(Arrays.asList(new Bundle[] { tb5 })));
        rootUpdate = getScope().newScopeUpdate();
        ScopeUpdate tb6Update = rootUpdate.newChild("tb6");
        rootUpdate.getChildren().add(tb6Update);
        tb6Update.getBundlesToInstall().add(tb6Info);
        addPackageExportPolicy("org.apache.aries.subsystem.scope.itests.tb6", tb6Update);
        rootUpdate.commit();
        tb6 = findBundle("org.apache.aries.subsystem.scope.itests.tb6", tb6Update.getScope());
        assertNotNull(tb6);
        assertTrue(frameworkWiring.resolveBundles(Arrays.asList(new Bundle[] { tb5, tb6 })));
    } finally {
        uninstallQuietly(tb6);
        uninstallQuietly(tb5);
    }
}
Also used : InstallInfo(org.apache.aries.subsystem.scope.InstallInfo) Bundle(org.osgi.framework.Bundle) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) URL(java.net.URL) Test(org.junit.Test)

Example 38 with FrameworkWiring

use of org.osgi.framework.wiring.FrameworkWiring in project aries by apache.

the class SharePolicyTest method test2.

/**
 * Bundle tb5
 * Bundle tb6
 * tb5 imports package exported by tb6
 * tb5 in root scope
 * tb6 in child scope of root
 * tb6 scope does not export tb6 package
 * tb5 should not resolve
 * @throws Exception
 */
@Test
public void test2() 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 tb6ScopeUpdate = scopeUpdate.newChild("tb6");
        scopeUpdate.getChildren().add(tb6ScopeUpdate);
        tb6ScopeUpdate.getBundlesToInstall().add(tb6Info);
        scopeUpdate.commit();
        tb6 = findBundle("org.apache.aries.subsystem.scope.itests.tb6", tb6ScopeUpdate.getScope());
        assertNotNull(tb6);
        assertFalse(frameworkWiring.resolveBundles(Arrays.asList(new Bundle[] { tb5, tb6 })));
    } finally {
        uninstallQuietly(tb6);
        uninstallQuietly(tb5);
    }
}
Also used : InstallInfo(org.apache.aries.subsystem.scope.InstallInfo) Bundle(org.osgi.framework.Bundle) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) URL(java.net.URL) Test(org.junit.Test)

Example 39 with FrameworkWiring

use of org.osgi.framework.wiring.FrameworkWiring in project aries by apache.

the class SharePolicyTest method test3.

/**
 * Bundle tb5
 * Bundle tb6
 * tb5 imports package exported by tb6
 * tb5 in root scope
 * tb6 in child scope of root
 * tb6 scope exports tb6 package
 * tb5 should resolve
 *
 * There is an implicit import between parent and child. In other words,
 * anything exported by a child is automatically available without the
 * parent explicitly importing it.
 *
 * @throws Exception
 */
@Test
public void test3() 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 tb6ScopeUpdate = scopeUpdate.newChild("tb6");
        scopeUpdate.getChildren().add(tb6ScopeUpdate);
        tb6ScopeUpdate.getBundlesToInstall().add(tb6Info);
        addPackageExportPolicy("org.apache.aries.subsystem.scope.itests.tb6", tb6ScopeUpdate);
        scopeUpdate.commit();
        tb6 = findBundle("org.apache.aries.subsystem.scope.itests.tb6", tb6ScopeUpdate.getScope());
        assertNotNull(tb6);
        tb5.start();
        assertTrue(frameworkWiring.resolveBundles(Arrays.asList(new Bundle[] { tb5, tb6 })));
    } finally {
        uninstallQuietly(tb6);
        uninstallQuietly(tb5);
    }
}
Also used : InstallInfo(org.apache.aries.subsystem.scope.InstallInfo) Bundle(org.osgi.framework.Bundle) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) URL(java.net.URL) Test(org.junit.Test)

Aggregations

FrameworkWiring (org.osgi.framework.wiring.FrameworkWiring)39 Bundle (org.osgi.framework.Bundle)29 Test (org.junit.Test)12 URL (java.net.URL)10 FrameworkListener (org.osgi.framework.FrameworkListener)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 BundleContext (org.osgi.framework.BundleContext)7 HashSet (java.util.HashSet)6 FrameworkEvent (org.osgi.framework.FrameworkEvent)6 Version (org.osgi.framework.Version)6 HashMap (java.util.HashMap)5 ComponentContext (org.osgi.service.component.ComponentContext)5 InputStream (java.io.InputStream)4 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)4 PatchManagement (io.fabric8.patch.management.PatchManagement)3 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)3 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)3 FileInputStream (java.io.FileInputStream)3