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;
}
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));
}
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));
}
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;
}
Aggregations