FrameworkUtils { @SuppressWarnings("unchecked") public static long[] getBundleDependencies(BundleContext localBundleContext, 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"); } Set<Bundle> dependencies = new HashSet<Bundle>(); for (ExportedPackage ep : getBundleImportedPackagesRaw(localBundleContext, bundle, packageAdmin)) { dependencies.add(ep.getExportingBundle()); } Dictionary<String, String> bundleHeaders = bundle.getHeaders(); String requireBundleHeader = bundleHeaders.get(Constants.REQUIRE_BUNDLE); if (requireBundleHeader != null) { List<String> bundleSymbolicNames = extractHeaderDeclaration(requireBundleHeader); for (String bundleSymbolicName: bundleSymbolicNames) { RequiredBundle[] candidateRequiredBundles = packageAdmin.getRequiredBundles(bundleSymbolicName); if (candidateRequiredBundles != null) { for (RequiredBundle candidateRequiredBundle : candidateRequiredBundles) { Bundle[] bundlesRequiring = candidateRequiredBundle.getRequiringBundles(); if (bundlesRequiring != null && arrayContains(bundlesRequiring, bundle)) { dependencies.add(candidateRequiredBundle.getBundle()); } } } } } Bundle[] hosts = packageAdmin.getHosts(bundle); if (hosts != null) { for (Bundle host : hosts) { dependencies.add(host); } } return getBundleIds(dependencies.toArray(new Bundle[dependencies.size()])); } private  FrameworkUtils(); static Bundle resolveBundle(BundleContext bundleContext, long bundleId); static long[] getBundleIds(Bundle[] bundles); static long[] getBundleIds(List<Bundle> bundles); static ServiceReference resolveService(BundleContext bundleContext, long serviceId); static long[] getServiceIds(ServiceReference[] serviceReferences); static String[] getBundleExportedPackages(Bundle bundle, PackageAdmin packageAdmin); static long[] getFragmentIds(Bundle bundle, PackageAdmin packageAdmin); static long[] getHostIds(Bundle fragment, PackageAdmin packageAdmin); static String[] getBundleImportedPackages(BundleContext localBundleContext, Bundle bundle,
            PackageAdmin packageAdmin); static long[] getRegisteredServiceIds(Bundle bundle); static long[] getServicesInUseByBundle(Bundle bundle); static boolean isBundlePendingRemoval(Bundle bundle, PackageAdmin packageAdmin); static boolean isBundleRequiredByOthers(Bundle bundle, PackageAdmin packageAdmin); @SuppressWarnings("unchecked") static long[] getBundleDependencies(BundleContext localBundleContext, 
                                               Bundle bundle,
                                               PackageAdmin packageAdmin); static long[] getDependentBundles(Bundle bundle, PackageAdmin packageAdmin); static String getBundleState(Bundle bundle); static boolean arrayContains(Object[] array, Object value); static List<String> extractHeaderDeclaration(String headerStatement);  }
@Test public void testGetBundleDependencies() throws Exception { Bundle bundle = mock(Bundle.class); BundleContext context = mock(BundleContext.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 }); Dictionary<String, String> headers = new Hashtable<String, String>(); when(bundle.getHeaders()).thenReturn(headers); PackageAdmin admin = mock(PackageAdmin.class); assertEquals(0, getBundleDependencies(context, bundle, admin).length); 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(admin.getRequiredBundles("b1")).thenReturn(new RequiredBundle[] { rb1 }); when(admin.getRequiredBundles("b2")).thenReturn(new RequiredBundle[] { rb2 }); when(admin.getRequiredBundles("b3")).thenReturn(new RequiredBundle[] { rb3 }); assertEquals(toSet(new long[] { 44, 66 }), toSet(getBundleDependencies(context, bundle, admin))); }