Search in sources :

Example 46 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project rt.equinox.framework by eclipse.

the class PackageAdminImpl method getExportedPackage.

public ExportedPackage getExportedPackage(String name) {
    ExportedPackage[] allExports = getExportedPackages(name);
    if (allExports == null)
        return null;
    ExportedPackage result = null;
    for (int i = 0; i < allExports.length; i++) {
        if (name.equals(allExports[i].getName())) {
            if (result == null) {
                result = allExports[i];
            } else {
                Version curVersion = result.getVersion();
                Version newVersion = allExports[i].getVersion();
                if (newVersion.compareTo(curVersion) >= 0)
                    result = allExports[i];
            }
        }
    }
    return result;
}
Also used : Version(org.osgi.framework.Version) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage)

Example 47 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project rt.equinox.framework by eclipse.

the class SubstituteExportsBundleTests method testSubstituteExports13.

public void testSubstituteExports13() throws BundleException {
    // $NON-NLS-1$
    Bundle a = installer.installBundle("substitutes.a");
    // $NON-NLS-1$
    Bundle b = installer.installBundle("substitutes.b");
    // $NON-NLS-1$
    Bundle c = installer.installBundle("substitutes.c");
    // $NON-NLS-1$
    Bundle d = installer.installBundle("substitutes.d");
    Bundle[] allBundles = new Bundle[] { a, b, c, d };
    // $NON-NLS-1$
    assertTrue("Bundles are not resolved", installer.resolveBundles(allBundles));
    PackageAdmin pa = installer.getPackageAdmin();
    // $NON-NLS-1$
    ExportedPackage[] xPackages = pa.getExportedPackages("substitutes.x");
    // $NON-NLS-1$
    assertNotNull("xPackages is null", xPackages);
    // $NON-NLS-1$
    assertEquals("xPackages wrong number", 1, xPackages.length);
    // $NON-NLS-1$
    assertEquals("Wrong exporter", a, xPackages[0].getExportingBundle());
    Bundle[] xImporters = xPackages[0].getImportingBundles();
    // $NON-NLS-1$
    assertNotNull("xImporters is null", xImporters);
    // $NON-NLS-1$
    assertEquals("Wrong number of xImporters", 3, xImporters.length);
    // $NON-NLS-1$
    ExportedPackage[] yPackages = pa.getExportedPackages("substitutes.y");
    // $NON-NLS-1$
    assertNotNull("yPackages is null", yPackages);
    // $NON-NLS-1$
    assertEquals("yPackages wrong number", 1, yPackages.length);
    // $NON-NLS-1$
    assertEquals("Wrong exporter", a, yPackages[0].getExportingBundle());
    Bundle[] yImporters = yPackages[0].getImportingBundles();
    // $NON-NLS-1$
    assertNotNull("yImporters is null", yImporters);
    // $NON-NLS-1$
    assertEquals("Wrong number of yImporters", 3, yImporters.length);
    Bundle[] expectedImporters = new Bundle[] { b, c, d };
    for (int i = 0; i < expectedImporters.length; i++) {
        // $NON-NLS-1$
        contains("xPackages importers does not contain", xImporters, expectedImporters[i]);
        // $NON-NLS-1$
        contains("yPackages importers does not contain", yImporters, expectedImporters[i]);
    }
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage)

Example 48 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project rt.equinox.framework by eclipse.

the class SubstituteExportsBundleTests method testSubstituteExports14.

public void testSubstituteExports14() throws BundleException {
    // $NON-NLS-1$
    Bundle iBundle = installer.installBundle("substitutes.i");
    // $NON-NLS-1$
    Bundle jBundle = installer.installBundle("substitutes.j");
    // $NON-NLS-1$
    Bundle kBundle = installer.installBundle("substitutes.k");
    // $NON-NLS-1$
    Bundle lBundle = installer.installBundle("substitutes.l");
    // $NON-NLS-1$
    Bundle mBundle = installer.installBundle("substitutes.m");
    // $NON-NLS-1$
    Bundle nBundle = installer.installBundle("substitutes.n");
    // $NON-NLS-1$
    Bundle pBundle = installer.installBundle("substitutes.p");
    // $NON-NLS-1$
    Bundle qBundle = installer.installBundle("substitutes.q");
    // k and l do not depend on i
    Bundle[] allBundles = new Bundle[] { iBundle, jBundle, kBundle, lBundle, mBundle, nBundle, pBundle, qBundle };
    // $NON-NLS-1$
    assertTrue("Bundles are not resolved", installer.resolveBundles(allBundles));
    PackageAdmin pa = installer.getPackageAdmin();
    // $NON-NLS-1$
    ExportedPackage[] xPackages = pa.getExportedPackages("substitutes.x");
    // $NON-NLS-1$
    assertNotNull("xPackages is null", xPackages);
    // $NON-NLS-1$
    assertEquals("xPackages wrong number", 3, xPackages.length);
    // $NON-NLS-1$
    ExportedPackage[] yPackages = pa.getExportedPackages("substitutes.y");
    // $NON-NLS-1$
    assertNotNull("yPackages is null", yPackages);
    // $NON-NLS-1$
    assertEquals("yPackages wrong number", 3, yPackages.length);
    Bundle[] expectedExporters = new Bundle[] { iBundle, kBundle, mBundle };
    for (int i = 0; i < expectedExporters.length; i++) {
        boolean found = false;
        for (int j = 0; j < xPackages.length && !found; j++) {
            found = expectedExporters[i] == xPackages[j].getExportingBundle();
            if (found) {
                Bundle[] importingBundles = xPackages[j].getImportingBundles();
                Bundle[] expectedImporters = null;
                String message = null;
                if (expectedExporters[i] == iBundle) {
                    expectedImporters = new Bundle[] { jBundle, mBundle, nBundle, pBundle, qBundle };
                    // $NON-NLS-1$
                    message = "iBundle ";
                } else if (expectedExporters[i] == kBundle) {
                    expectedImporters = new Bundle[] { lBundle, mBundle, nBundle, pBundle, qBundle };
                    // $NON-NLS-1$
                    message = "kBundle ";
                } else if (expectedExporters[i] == mBundle) {
                    expectedImporters = new Bundle[] { nBundle, pBundle, qBundle };
                    // $NON-NLS-1$
                    message = "mBundle ";
                }
                assertEquals(message, expectedImporters.length, importingBundles.length);
                for (int k = 0; k < expectedImporters.length; k++) contains(message, importingBundles, expectedImporters[k]);
            }
        }
    }
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage)

Example 49 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testSystemBundle11.

public void testSystemBundle11() {
    // test extra packages property
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile("testSystemBundle11");
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    // $NON-NLS-1$
    configuration.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "test.pkg1, test.pkg2");
    Equinox equinox = new Equinox(configuration);
    try {
        equinox.init();
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Failed to start the framework", e);
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.ACTIVE, equinox.getState());
    BundleContext systemContext = equinox.getBundleContext();
    // $NON-NLS-1$
    assertNotNull("SystemBundle context is null", systemContext);
    PackageAdmin pa = (PackageAdmin) equinox.getBundleContext().getService(equinox.getBundleContext().getServiceReference(PackageAdmin.class.getName()));
    // $NON-NLS-1$
    ExportedPackage[] pkg1 = pa.getExportedPackages("test.pkg1");
    assertNotNull(pkg1);
    // $NON-NLS-1$
    assertEquals("Wrong number of exports", 1, pkg1.length);
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("Wrong package name", "test.pkg1", pkg1[0].getName());
    // $NON-NLS-1$
    ExportedPackage[] pkg2 = pa.getExportedPackages("test.pkg2");
    assertNotNull(pkg2);
    // $NON-NLS-1$
    assertEquals("Wrong number of exports", 1, pkg2.length);
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("Wrong package name", "test.pkg2", pkg2[0].getName());
    try {
        equinox.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected erorr stopping framework", e);
    }
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) BundleException(org.osgi.framework.BundleException) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 50 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project aries by apache.

the class BundleDataTest method testToCompositeData.

@Test
public void testToCompositeData() throws Exception {
    Bundle bundle = mock(Bundle.class);
    BundleContext context = mock(BundleContext.class);
    PackageAdmin packageAdmin = mock(PackageAdmin.class);
    StartLevel startLevel = mock(StartLevel.class);
    Bundle b1 = mock(Bundle.class);
    when(b1.getSymbolicName()).thenReturn("b1");
    when(b1.getBundleId()).thenReturn(new Long(44));
    Bundle b2 = mock(Bundle.class);
    when(b2.getSymbolicName()).thenReturn("b2");
    when(b2.getBundleId()).thenReturn(new Long(55));
    Bundle b3 = mock(Bundle.class);
    when(b3.getSymbolicName()).thenReturn("b3");
    when(b3.getBundleId()).thenReturn(new Long(66));
    when(context.getBundles()).thenReturn(new Bundle[] { bundle, b1, b2, b3 });
    when(bundle.getSymbolicName()).thenReturn("test");
    when(bundle.getVersion()).thenReturn(Version.emptyVersion);
    when(bundle.getBundleId()).thenReturn(new Long(1));
    when(bundle.getLastModified()).thenReturn(new Long(12345));
    when(bundle.getLocation()).thenReturn("location");
    // headers
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(Constants.BUNDLE_SYMBOLICNAME, "test");
    headers.put(Constants.BUNDLE_VERSION, "0.0.0");
    when(bundle.getHeaders()).thenReturn(headers);
    // exported packages
    ExportedPackage exported = mock(ExportedPackage.class);
    when(exported.getName()).thenReturn("org.apache.aries.jmx");
    when(exported.getVersion()).thenReturn(new Version("1.0.0"));
    when(exported.getExportingBundle()).thenReturn(bundle);
    when(packageAdmin.getExportedPackages(bundle)).thenReturn(new ExportedPackage[] { exported });
    // imported packages
    ExportedPackage ep1 = mock(ExportedPackage.class);
    when(ep1.getImportingBundles()).thenReturn(new Bundle[] { bundle, b2, b3 });
    when(ep1.getName()).thenReturn("org.apache.aries.jmx.b1");
    when(ep1.getVersion()).thenReturn(Version.emptyVersion);
    when(ep1.getExportingBundle()).thenReturn(b1);
    ExportedPackage ep2 = mock(ExportedPackage.class);
    when(ep2.getImportingBundles()).thenReturn(new Bundle[] { bundle, b3 });
    when(ep2.getName()).thenReturn("org.apache.aries.jmx.b2");
    when(ep2.getVersion()).thenReturn(Version.parseVersion("2.0.1"));
    when(ep2.getExportingBundle()).thenReturn(b2);
    headers.put(Constants.DYNAMICIMPORT_PACKAGE, "*");
    when(packageAdmin.getExportedPackages(b1)).thenReturn(new ExportedPackage[] { ep1 });
    when(packageAdmin.getExportedPackages(b2)).thenReturn(new ExportedPackage[] { ep2 });
    when(packageAdmin.getExportedPackages(b3)).thenReturn(null);
    // required bundles
    RequiredBundle rb1 = mock(RequiredBundle.class);
    when(rb1.getBundle()).thenReturn(b1);
    when(rb1.getRequiringBundles()).thenReturn(new Bundle[] { bundle, b2 });
    RequiredBundle rb2 = mock(RequiredBundle.class);
    when(rb2.getBundle()).thenReturn(b2);
    when(rb2.getRequiringBundles()).thenReturn(new Bundle[] { b1 });
    RequiredBundle rb3 = mock(RequiredBundle.class);
    when(rb3.getBundle()).thenReturn(b3);
    when(rb3.getRequiringBundles()).thenReturn(new Bundle[] { bundle, b1, b2 });
    headers.put(Constants.REQUIRE_BUNDLE, "b1;bundle-version=\"1.0.0\",b3;bundle-version=\"2.0.0\"");
    when(packageAdmin.getRequiredBundles("b1")).thenReturn(new RequiredBundle[] { rb1 });
    when(packageAdmin.getRequiredBundles("b2")).thenReturn(new RequiredBundle[] { rb2 });
    when(packageAdmin.getRequiredBundles("b3")).thenReturn(new RequiredBundle[] { rb3 });
    // services in use
    ServiceReference s1 = mock(ServiceReference.class);
    when(s1.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(15));
    ServiceReference s2 = mock(ServiceReference.class);
    when(s2.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(16));
    ServiceReference s3 = mock(ServiceReference.class);
    when(s3.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(17));
    when(bundle.getServicesInUse()).thenReturn(new ServiceReference[] { s1, s2, s3 });
    BundleData b = new BundleData(context, bundle, packageAdmin, startLevel);
    CompositeData compositeData = b.toCompositeData();
    assertEquals("test", compositeData.get(SYMBOLIC_NAME));
    assertEquals("0.0.0", compositeData.get(VERSION));
    TabularData headerTable = (TabularData) compositeData.get(HEADERS);
    assertEquals(4, headerTable.values().size());
    CompositeData header = headerTable.get(new Object[] { Constants.BUNDLE_SYMBOLICNAME });
    assertNotNull(header);
    String value = (String) header.get(VALUE);
    assertEquals("test", value);
    String key = (String) header.get(KEY);
    assertEquals(Constants.BUNDLE_SYMBOLICNAME, key);
    TabularData bundleTable = new TabularDataSupport(BUNDLES_TYPE);
    bundleTable.put(b.toCompositeData());
    CompositeData bundleData = bundleTable.get(new Object[] { Long.valueOf(1) });
    assertNotNull(bundleData);
    String location = (String) bundleData.get(LOCATION);
    assertEquals("location", location);
    assertArrayEquals(new String[] { "org.apache.aries.jmx;1.0.0" }, (String[]) compositeData.get(EXPORTED_PACKAGES));
    assertArrayEquals(new String[] { "org.apache.aries.jmx.b1;0.0.0", "org.apache.aries.jmx.b2;2.0.1" }, (String[]) compositeData.get(IMPORTED_PACKAGES));
    assertEquals(toSet(new long[] { 44, 55, 66 }), toSet((Long[]) compositeData.get(REQUIRED_BUNDLES)));
    assertArrayEquals(new Long[] { new Long(15), new Long(16), new Long(17) }, (Long[]) compositeData.get(SERVICES_IN_USE));
    // default no return stub
    assertEquals("UNKNOWN", compositeData.get(STATE));
    assertEquals(0, ((Long[]) compositeData.get(HOSTS)).length);
    assertEquals(0, ((Long[]) compositeData.get(FRAGMENTS)).length);
}
Also used : Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) Hashtable(java.util.Hashtable) CompositeData(javax.management.openmbean.CompositeData) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) ServiceReference(org.osgi.framework.ServiceReference) TabularData(javax.management.openmbean.TabularData) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Version(org.osgi.framework.Version) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) TabularDataSupport(javax.management.openmbean.TabularDataSupport) StartLevel(org.osgi.service.startlevel.StartLevel) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

ExportedPackage (org.osgi.service.packageadmin.ExportedPackage)54 Bundle (org.osgi.framework.Bundle)40 PackageAdmin (org.osgi.service.packageadmin.PackageAdmin)21 ArrayList (java.util.ArrayList)19 RequiredBundle (org.osgi.service.packageadmin.RequiredBundle)14 Test (org.junit.Test)9 HashMap (java.util.HashMap)7 List (java.util.List)7 Version (org.osgi.framework.Version)6 LinkedHashSet (java.util.LinkedHashSet)4 TreeMap (java.util.TreeMap)4 Expectations (org.jmock.Expectations)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 File (java.io.File)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 BundleCapability (org.osgi.framework.wiring.BundleCapability)3