Search in sources :

Example 51 with ExportedPackage

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

the class PackageState method getExportingBundles.

/**
 * @see org.osgi.jmx.framework.PackageStateMBean#getExportingBundles(String, String)
 */
public long[] getExportingBundles(String packageName, String version) throws IOException {
    if (packageName == null || packageName.length() < 1) {
        throw new IOException("Package name cannot be null or empty");
    }
    ExportedPackage[] exportedPackages = packageAdmin.getExportedPackages(packageName);
    if (exportedPackages != null) {
        Version ver = Version.parseVersion(version);
        List<Bundle> exportingBundles = new ArrayList<Bundle>();
        for (ExportedPackage exportedPackage : exportedPackages) {
            if (exportedPackage.getVersion().equals(ver)) {
                Bundle bundle = exportedPackage.getExportingBundle();
                exportingBundles.add(bundle);
            }
        }
        return FrameworkUtils.getBundleIds(exportingBundles);
    }
    return null;
}
Also used : Version(org.osgi.framework.Version) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 52 with ExportedPackage

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

the class FrameworkUtilsTest method testGetBundleExportedPackages.

@Test
public void testGetBundleExportedPackages() throws Exception {
    Bundle bundle = mock(Bundle.class);
    PackageAdmin admin = mock(PackageAdmin.class);
    assertEquals(0, getBundleExportedPackages(bundle, admin).length);
    ExportedPackage exported = mock(ExportedPackage.class);
    when(exported.getName()).thenReturn("org.apache.aries.jmx");
    when(exported.getVersion()).thenReturn(new Version("1.0.0"));
    when(admin.getExportedPackages(bundle)).thenReturn(new ExportedPackage[] { exported });
    assertArrayEquals(new String[] { "org.apache.aries.jmx;1.0.0" }, getBundleExportedPackages(bundle, admin));
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Version(org.osgi.framework.Version) FrameworkUtils.getServicesInUseByBundle(org.apache.aries.jmx.util.FrameworkUtils.getServicesInUseByBundle) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Test(org.junit.Test)

Example 53 with ExportedPackage

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

the class FrameworkUtilsTest method testGetBundleImportedPackages.

@Test
public void testGetBundleImportedPackages() throws Exception {
    Bundle bundle = mock(Bundle.class);
    BundleContext context = mock(BundleContext.class);
    Bundle b1 = mock(Bundle.class);
    Bundle b2 = mock(Bundle.class);
    Bundle b3 = mock(Bundle.class);
    when(context.getBundles()).thenReturn(new Bundle[] { bundle, b1, b2, b3 });
    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);
    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"));
    PackageAdmin admin = mock(PackageAdmin.class);
    when(admin.getExportedPackages(b1)).thenReturn(new ExportedPackage[] { ep1 });
    when(admin.getExportedPackages(b2)).thenReturn(new ExportedPackage[] { ep2 });
    when(admin.getExportedPackages(b3)).thenReturn(null);
    // check first with DynamicImport
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(Constants.DYNAMICIMPORT_PACKAGE, "*");
    when(bundle.getHeaders()).thenReturn(headers);
    assertArrayEquals(new String[] { "org.apache.aries.jmx.b1;0.0.0", "org.apache.aries.jmx.b2;2.0.1" }, getBundleImportedPackages(context, bundle, admin));
    // check with ImportPackage statement
    headers.remove(Constants.DYNAMICIMPORT_PACKAGE);
    String importPackageStatement = "org.apache.aries.jmx.b1;version=0.0.0;resolution:=optional,org.apache.aries.jmx.b2;attribute:=value;version=\"[2.0, 3.0)\"";
    headers.put(Constants.IMPORT_PACKAGE, importPackageStatement);
    when(admin.getExportedPackages("org.apache.aries.jmx.b1")).thenReturn(new ExportedPackage[] { ep1 });
    when(admin.getExportedPackages("org.apache.aries.jmx.b2")).thenReturn(new ExportedPackage[] { ep2 });
    assertArrayEquals(new String[] { "org.apache.aries.jmx.b1;0.0.0", "org.apache.aries.jmx.b2;2.0.1" }, getBundleImportedPackages(context, bundle, admin));
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) FrameworkUtils.getServicesInUseByBundle(org.apache.aries.jmx.util.FrameworkUtils.getServicesInUseByBundle) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Hashtable(java.util.Hashtable) Matchers.anyString(org.mockito.Matchers.anyString) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 54 with ExportedPackage

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

the class FrameworkUtils method isBundlePendingRemoval.

/**
 * Returns the status of pending removal
 *
 * @param bundle
 * @return true if the bundle is pending removal
 * @throws IllegalArgumentException
 *             if bundle or packageAdmin are null
 */
public static boolean isBundlePendingRemoval(Bundle bundle, PackageAdmin packageAdmin) throws IllegalArgumentException {
    if (bundle == null) {
        throw new IllegalArgumentException("Argument bundle cannot be null");
    }
    if (packageAdmin == null) {
        throw new IllegalArgumentException("Argument packageAdmin cannot be null");
    }
    boolean result = false;
    ExportedPackage[] exportedPackages = packageAdmin.getExportedPackages(bundle);
    if (exportedPackages != null) {
        for (ExportedPackage exportedPackage : exportedPackages) {
            if (exportedPackage.isRemovalPending()) {
                result = true;
                break;
            }
        }
    }
    if (!result) {
        RequiredBundle[] requiredBundles = packageAdmin.getRequiredBundles(bundle.getSymbolicName());
        if (requiredBundles != null) {
            for (RequiredBundle requiredBundle : requiredBundles) {
                Bundle required = requiredBundle.getBundle();
                if (required == bundle) {
                    result = requiredBundle.isRemovalPending();
                    break;
                }
            }
        }
    }
    return result;
}
Also used : ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle)

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