use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.
the class UninstallBundleTest method testUninstallBundleCleansUpRevision.
public void testUninstallBundleCleansUpRevision() throws Exception {
Map<String, String> params = new HashMap<String, String>();
params.put(Constants.FRAMEWORK_SYSTEMPACKAGES, "org.osgi.framework; version=1.4.0," + "org.osgi.service.packageadmin; version=1.2.0," + "org.osgi.service.startlevel; version=1.1.0," + "org.osgi.util.tracker; version=1.3.3," + "org.osgi.service.url; version=1.0.0");
File cacheDir = File.createTempFile("felix-cache", ".dir");
cacheDir.delete();
cacheDir.mkdirs();
String cache = cacheDir.getPath();
params.put("felix.cache.profiledir", cache);
params.put("felix.cache.dir", cache);
params.put(Constants.FRAMEWORK_STORAGE, cache);
String mfA = "Bundle-SymbolicName: A\n" + "Bundle-ManifestVersion: 2\n" + "Export-Package: org.foo.bar\n";
File bundleAFile = createBundle(mfA, cacheDir);
String mfB = "Bundle-SymbolicName: B\n" + "Bundle-ManifestVersion: 2\n" + "Export-Package: org.foo.bbr\n";
File bundleBFile = createBundle(mfB, cacheDir);
String mfC = "Bundle-SymbolicName: C\n" + "Bundle-ManifestVersion: 2\n" + "Import-Package: org.foo.bar, org.foo.bbr\n";
File bundleCFile = createBundle(mfC, cacheDir);
final List<Bundle> shouldNotBeRefreshed = new ArrayList<Bundle>();
Felix felix = new Felix(params) {
@Override
void refreshPackages(Collection<Bundle> targets, FrameworkListener[] listeners) {
if (targets != null) {
for (Bundle b : targets) {
if (shouldNotBeRefreshed.contains(b))
fail("Bundle " + b + " should not be refreshed");
}
}
super.refreshPackages(targets, listeners);
}
};
felix.init();
felix.start();
try {
Bundle bundleA = felix.getBundleContext().installBundle(bundleAFile.toURI().toString());
bundleA.start();
Bundle bundleB = felix.getBundleContext().installBundle(bundleBFile.toURI().toString());
bundleB.start();
// This bundle is not going to be uninstalled, so it should not be refreshed
shouldNotBeRefreshed.add(bundleB);
Bundle bundleC = felix.getBundleContext().installBundle(bundleCFile.toURI().toString());
bundleC.start();
bundleA.uninstall();
boolean foundBar = false;
for (ExportedPackage ep : felix.getExportedPackages((Bundle) null)) {
if ("org.foo.bar".equals(ep.getName()))
foundBar = true;
}
assertTrue("The system should still export org.foo.bar as C is importing it.", foundBar);
bundleC.uninstall();
for (ExportedPackage ep : felix.getExportedPackages((Bundle) null)) {
if ("org.foo.bar".equals(ep.getName()))
fail("Should not export org.foo.bar any more!");
}
boolean foundBbr = false;
for (ExportedPackage ep : felix.getExportedPackages((Bundle) null)) {
if ("org.foo.bbr".equals(ep.getName()))
foundBbr = true;
}
assertTrue("The system should still export org.foo.bbr as it was not uninstalled.", foundBbr);
} finally {
felix.stop();
Thread.sleep(DELAY);
deleteDir(cacheDir);
}
}
use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.
the class ClassUtils method getClassFromComponentClassLoader.
/**
* Returns the class object representing the class of the field reference
* The class loader of the component class is used to load the service class.
* <p>
* It may well be possible, that the class loader of the target class cannot
* see the service object class, for example if the service reference is
* inherited from a component class of another bundle.
*
* @return The class object for the referred to service or <code>null</code>
* if the class loader of the <code>targetClass</code> cannot see that
* class.
*/
public static Class<?> getClassFromComponentClassLoader(final Class<?> componentClass, final String className, final SimpleLogger logger) {
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getReferenceClass: Looking for interface class {0} through loader of {1}", new Object[] { className, componentClass.getName() }, null);
}
try {
// need the class loader of the target class, which may be the
// system classloader, which case getClassLoader may retur null
ClassLoader loader = componentClass.getClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
final Class<?> referenceClass = loader.loadClass(className);
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: Found class {0}", new Object[] { referenceClass.getName() }, null);
}
return referenceClass;
} catch (final ClassNotFoundException cnfe) {
// if we can't load the class, perhaps the method is declared in a
// super class so we try this class next
}
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: Not found through component class, using PackageAdmin service", null);
}
// try to load the class with the help of the PackageAdmin service
PackageAdmin pa = (PackageAdmin) getPackageAdmin();
if (pa != null) {
final String referenceClassPackage = className.substring(0, className.lastIndexOf('.'));
ExportedPackage[] pkg = pa.getExportedPackages(referenceClassPackage);
if (pkg != null) {
for (int i = 0; i < pkg.length; i++) {
try {
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: Checking Bundle {0}/{1}", new Object[] { pkg[i].getExportingBundle().getSymbolicName(), pkg[i].getExportingBundle().getBundleId() }, null);
}
Class<?> referenceClass = pkg[i].getExportingBundle().loadClass(className);
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: Found class {0}", new Object[] { referenceClass.getName() }, null);
}
return referenceClass;
} catch (ClassNotFoundException cnfe) {
// exported package does not provide the interface !!!!
}
}
} else if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: No bundles exporting package {0} found", new Object[] { referenceClassPackage }, null);
}
} else if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: PackageAdmin service not available, cannot find class", null);
}
// export, so we fall back to assuming Object
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: No class found, falling back to class Object", null);
}
return OBJECT_CLASS;
}
use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.
the class ClassUtils method getClassFromComponentClassLoader.
/**
* Returns the class object representing the class of the field reference
* The class loader of the component class is used to load the service class.
* <p>
* It may well be possible, that the class loader of the target class cannot
* see the service object class, for example if the service reference is
* inherited from a component class of another bundle.
*
* @return The class object for the referred to service or <code>null</code>
* if the class loader of the <code>targetClass</code> cannot see that
* class.
*/
public static Class<?> getClassFromComponentClassLoader(final Class<?> componentClass, final String className, final ComponentLogger logger) {
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getReferenceClass: Looking for interface class {0} through loader of {1}", null, className, componentClass.getName());
}
try {
// need the class loader of the target class, which may be the
// system classloader, which case getClassLoader may retur null
ClassLoader loader = componentClass.getClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
final Class<?> referenceClass = loader.loadClass(className);
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: Found class {0}", null, referenceClass.getName());
}
return referenceClass;
} catch (final ClassNotFoundException cnfe) {
// if we can't load the class, perhaps the method is declared in a
// super class so we try this class next
}
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: Not found through component class, using PackageAdmin service", null);
}
// try to load the class with the help of the PackageAdmin service
PackageAdmin pa = (PackageAdmin) getPackageAdmin();
if (pa != null) {
final String referenceClassPackage = className.substring(0, className.lastIndexOf('.'));
ExportedPackage[] pkg = pa.getExportedPackages(referenceClassPackage);
if (pkg != null) {
for (int i = 0; i < pkg.length; i++) {
try {
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: Checking Bundle {0}/{1}", null, pkg[i].getExportingBundle().getSymbolicName(), pkg[i].getExportingBundle().getBundleId());
}
Class<?> referenceClass = pkg[i].getExportingBundle().loadClass(className);
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: Found class {0}", null, referenceClass.getName());
}
return referenceClass;
} catch (ClassNotFoundException cnfe) {
// exported package does not provide the interface !!!!
}
}
} else if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: No bundles exporting package {0} found", null, referenceClassPackage);
}
} else if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: PackageAdmin service not available, cannot find class", null);
}
// export, so we fall back to assuming Object
if (logger.isLogEnabled(LogService.LOG_DEBUG)) {
logger.log(LogService.LOG_DEBUG, "getParameterClass: No class found, falling back to class Object", null);
}
return OBJECT_CLASS;
}
use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.
the class InspectCommandImpl method printExportedPackages.
private void printExportedPackages(String[] ids, PrintStream out, PrintStream err) {
PackageAdmin pa = getPackageAdmin();
if (pa == null) {
out.println("PackageAdmin service is unavailable.");
} else {
boolean separatorNeeded = false;
Bundle[] bundles = null;
if ((ids == null) || (ids.length == 0)) {
bundles = m_context.getBundles();
} else {
bundles = new Bundle[ids.length];
for (int idIdx = 0; idIdx < ids.length; idIdx++) {
try {
long l = Long.parseLong(ids[idIdx]);
Bundle b = m_context.getBundle(l);
if (b == null) {
err.println("Bundle ID " + ids[idIdx] + " is invalid.");
}
bundles[idIdx] = b;
} catch (NumberFormatException ex) {
err.println("Unable to parse id '" + ids[idIdx] + "'.");
}
}
}
for (int bundleIdx = 0; bundleIdx < bundles.length; bundleIdx++) {
try {
if (bundles[bundleIdx] != null) {
ExportedPackage[] exports = pa.getExportedPackages(bundles[bundleIdx]);
if (separatorNeeded) {
out.println("");
}
String title = bundles[bundleIdx] + " exports packages:";
out.println(title);
out.println(Util.getUnderlineString(title));
if ((exports != null) && (exports.length > 0)) {
for (int expIdx = 0; expIdx < exports.length; expIdx++) {
out.println(exports[expIdx]);
}
} else {
out.println("Nothing");
}
separatorNeeded = true;
}
} catch (Exception ex) {
err.println(ex.toString());
}
}
}
ungetPackageAdmin();
}
use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.
the class BundlesServlet method listImportExportsUnresolved.
private void listImportExportsUnresolved(final List props, Bundle bundle, final String pluginRoot) {
Dictionary dict = bundle.getHeaders();
String target = (String) dict.get(Constants.EXPORT_PACKAGE);
if (target != null) {
Clause[] pkgs = Parser.parseHeader(target);
if (pkgs != null && pkgs.length > 0) {
// do alphabetical sort
Arrays.sort(pkgs, new Comparator() {
public int compare(Clause p1, Clause p2) {
return p1.getName().compareTo(p2.getName());
}
@Override
public int compare(Object o1, Object o2) {
return compare((Clause) o1, (Clause) o2);
}
});
Object[] val = new Object[pkgs.length];
for (int i = 0; i < pkgs.length; i++) {
Clause export = new Clause(pkgs[i].getName(), pkgs[i].getDirectives(), pkgs[i].getAttributes());
val[i] = collectExport(export.getName(), export.getAttribute(Constants.VERSION_ATTRIBUTE));
}
keyVal(props, "Exported Packages", val);
} else {
keyVal(props, "Exported Packages", "---");
}
}
target = (String) dict.get(Constants.IMPORT_PACKAGE);
if (target != null) {
Clause[] pkgs = Parser.parseHeader(target);
if (pkgs != null && pkgs.length > 0) {
Map imports = new TreeMap();
for (int i = 0; i < pkgs.length; i++) {
Clause pkg = pkgs[i];
imports.put(pkg.getName(), new Clause(pkg.getName(), pkg.getDirectives(), pkg.getAttributes()));
}
// collect import packages first
final Map candidates = new HashMap();
PackageAdmin packageAdmin = getPackageAdmin();
if (packageAdmin != null) {
ExportedPackage[] exports = packageAdmin.getExportedPackages((Bundle) null);
if (exports != null && exports.length > 0) {
for (int i = 0; i < exports.length; i++) {
final ExportedPackage ep = exports[i];
Clause imp = (Clause) imports.get(ep.getName());
if (imp != null && isSatisfied(imp, ep)) {
candidates.put(ep.getName(), ep);
}
}
}
}
// now sort
Object[] val;
if (imports.size() > 0) {
final List importList = new ArrayList();
for (Iterator ii = imports.values().iterator(); ii.hasNext(); ) {
Clause r4Import = (Clause) ii.next();
ExportedPackage ep = (ExportedPackage) candidates.get(r4Import.getName());
// bundle has the package, ignore the entry in this case
if (ep == null) {
String path = r4Import.getName().replace('.', '/');
if (bundle.getEntry(path) != null) {
continue;
}
}
importList.add(collectImport(r4Import.getName(), r4Import.getAttribute(Constants.VERSION_ATTRIBUTE), Constants.RESOLUTION_OPTIONAL.equals(r4Import.getDirective(Constants.RESOLUTION_DIRECTIVE)), ep, pluginRoot));
}
val = importList.toArray(new Object[importList.size()]);
} else {
// add description if there are no imports
val = new Object[1];
val[0] = "---";
}
keyVal(props, "Imported Packages", val);
}
}
}
Aggregations