use of org.osgi.service.packageadmin.ExportedPackage in project aries by apache.
the class BundleContextInfoProvider method getRelationships.
@Override
public List<RelationshipInfo> getRelationships() {
ArrayList<RelationshipInfo> r = new ArrayList<RelationshipInfo>();
Bundle[] bundles = ctx.getBundles();
PackageAdmin pa = (PackageAdmin) ctx.getService(ctx.getServiceReference(PackageAdmin.class.getName().toString()));
if (bundles != null && bundles.length != 0) {
for (Bundle b : bundles) {
String bkey = getKeyForBundle(b);
ComponentInfo ci = getComponentForId(bkey);
// add all the packages..
// we only add exports, as imports are implied in the reverse
ExportedPackage[] eps = pa.getExportedPackages(b);
if (eps != null && eps.length != 0) {
for (ExportedPackage ep : eps) {
RelationshipInfoImpl ri = new RelationshipInfoImpl();
ri.setProvidedBy(ci);
ri.setType("Package");
ri.setName(ep.getName());
ri.setRelationshipAspects(new ArrayList<RelationshipAspect>());
ri.setConsumedBy(new ArrayList<ComponentInfo>());
// TODO: add versioning aspect.
Bundle[] imps = ep.getImportingBundles();
if (imps != null && imps.length != 0) {
for (Bundle imp : imps) {
ri.getConsumedBy().add(getComponentForId(getKeyForBundle(imp)));
}
}
r.add(ri);
}
}
// add all the services..
// we only add registered services, as ones in use are handled in the reverse
ServiceReference[] srs = b.getRegisteredServices();
if (srs != null && srs.length != 0) {
for (ServiceReference sr : srs) {
RelationshipInfoImpl ri = getRIforSR(sr);
ri.setProvidedBy(ci);
r.add(ri);
}
}
}
}
return r;
}
use of org.osgi.service.packageadmin.ExportedPackage in project aries by apache.
the class FrameworkUtils method getBundleImportedPackagesRaw.
@SuppressWarnings("unchecked")
private static Collection<ExportedPackage> getBundleImportedPackagesRaw(BundleContext localBundleContext, Bundle bundle, PackageAdmin packageAdmin) throws IllegalArgumentException {
List<ExportedPackage> result = new ArrayList<ExportedPackage>();
Dictionary<String, String> bundleHeaders = bundle.getHeaders();
String dynamicImportHeader = bundleHeaders.get(Constants.DYNAMICIMPORT_PACKAGE);
// else means no dynamic import or has dynamic import but no wildcard "*" in it.
if (dynamicImportHeader != null && dynamicImportHeader.contains("*")) {
Bundle[] bundles = localBundleContext.getBundles();
for (Bundle candidate : bundles) {
if (candidate.equals(bundle)) {
continue;
}
ExportedPackage[] candidateExports = packageAdmin.getExportedPackages(candidate);
if (candidateExports != null) {
for (ExportedPackage exportedPackage : candidateExports) {
Bundle[] userBundles = exportedPackage.getImportingBundles();
if (userBundles != null && arrayContains(userBundles, bundle)) {
result.add(exportedPackage);
}
}
// end for candidateExports
}
}
// end for bundles
} else {
// only query ExportPackage for package names declared as imported
List<String> importPackages = new ArrayList<String>();
String importPackageHeader = bundleHeaders.get(Constants.IMPORT_PACKAGE);
if (importPackageHeader != null && importPackageHeader.length() > 0) {
importPackages.addAll(extractHeaderDeclaration(importPackageHeader));
}
if (dynamicImportHeader != null) {
importPackages.addAll(extractHeaderDeclaration(dynamicImportHeader));
}
for (String packageName : importPackages) {
ExportedPackage[] candidateExports = packageAdmin.getExportedPackages(packageName);
if (candidateExports != null) {
for (ExportedPackage exportedPackage : candidateExports) {
Bundle[] userBundles = exportedPackage.getImportingBundles();
if (userBundles != null && arrayContains(userBundles, bundle)) {
result.add(exportedPackage);
}
}
// end for candidateExports
}
}
}
return result;
}
use of org.osgi.service.packageadmin.ExportedPackage in project aries by apache.
the class FrameworkUtils method getDependentBundles.
/**
* Returns an array of ids of bundles that depend on the given bundle
*
* @param bundle
* @param packageAdmin
* @return
* @throws IllegalArgumentException
* if bundle or packageAdmin are null
*/
public static long[] getDependentBundles(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>();
// Handle imported packages (statically or dynamically)
ExportedPackage[] exportedPackages = packageAdmin.getExportedPackages(bundle);
if (exportedPackages != null) {
for (ExportedPackage exportedPackage : exportedPackages) {
Bundle[] importingBundles = exportedPackage.getImportingBundles();
if (importingBundles != null) {
for (Bundle importingBundle : importingBundles) {
dependencies.add(importingBundle);
}
}
}
}
// Handle required bundles
RequiredBundle[] requiredBundles = packageAdmin.getRequiredBundles(bundle.getSymbolicName());
if (requiredBundles != null) {
for (RequiredBundle requiredBundle : requiredBundles) {
Bundle required = requiredBundle.getBundle();
if (required == bundle) {
Bundle[] requiringBundles = requiredBundle.getRequiringBundles();
if (requiringBundles != null) {
for (Bundle requiringBundle : requiringBundles) {
dependencies.add(requiringBundle);
}
}
}
}
}
// Handle fragment bundles
Bundle[] fragments = packageAdmin.getFragments(bundle);
if (fragments != null) {
for (Bundle fragment : fragments) {
dependencies.add(fragment);
}
}
return getBundleIds(dependencies.toArray(new Bundle[dependencies.size()]));
}
use of org.osgi.service.packageadmin.ExportedPackage in project aries by apache.
the class FrameworkUtils method isBundleRequiredByOthers.
/**
* Checks if the given bundle is currently required by other bundles
*
* @param bundle
* @param packageAdmin
* @return
* @throws IllegalArgumentException
* if bundle or packageAdmin are null
*/
public static boolean isBundleRequiredByOthers(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;
// Check imported packages (statically or dynamically)
ExportedPackage[] exportedPackages = packageAdmin.getExportedPackages(bundle);
if (exportedPackages != null) {
for (ExportedPackage exportedPackage : exportedPackages) {
Bundle[] importingBundles = exportedPackage.getImportingBundles();
if (importingBundles != null && importingBundles.length > 0) {
result = true;
break;
}
}
}
if (!result) {
// Check required bundles
RequiredBundle[] requiredBundles = packageAdmin.getRequiredBundles(bundle.getSymbolicName());
if (requiredBundles != null) {
for (RequiredBundle requiredBundle : requiredBundles) {
Bundle required = requiredBundle.getBundle();
if (required == bundle) {
Bundle[] requiring = requiredBundle.getRequiringBundles();
if (requiring != null && requiring.length > 0) {
result = true;
break;
}
}
}
}
}
if (!result) {
// Check fragment bundles
Bundle[] fragments = packageAdmin.getFragments(bundle);
if (fragments != null && fragments.length > 0) {
result = true;
}
}
return result;
}
use of org.osgi.service.packageadmin.ExportedPackage in project aries by apache.
the class FrameworkUtils method getBundleDependencies.
/**
* Returns an array of ids of bundles the given bundle depends on
*
* @param localBundleContext
* BundleContext object of this bundle/caller
* @param bundle
* target Bundle object to query dependencies for
* @param packageAdmin
*
* @return
* @throws IllegalArgumentException
* if bundle or packageAdmin are null
*/
@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());
}
// Handle required bundles
Dictionary<String, String> bundleHeaders = bundle.getHeaders();
String requireBundleHeader = bundleHeaders.get(Constants.REQUIRE_BUNDLE);
if (requireBundleHeader != null) {
// only check if Require-Bundle is used
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());
}
}
}
}
}
// Handle fragment bundles
Bundle[] hosts = packageAdmin.getHosts(bundle);
if (hosts != null) {
for (Bundle host : hosts) {
dependencies.add(host);
}
}
return getBundleIds(dependencies.toArray(new Bundle[dependencies.size()]));
}
Aggregations