Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSGI: is importing package without activating bundle possible?

Tags:

java

osgi

Is it possible to import package from another bundle without calling its activator? This package isn't required any bundle initializations. I understand that this is bad design but I cannot change it.

Exception:

org.osgi.framework.BundleException: The activator my-package.Activator for bundle my-bundle is invalid
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:157)  
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:750)  
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:352)  
at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:370)  
at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1068)  
at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:557)  
at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:464)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:248)  
at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:445)  
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:220)  
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:330)
like image 806
bellum Avatar asked Dec 10 '25 21:12

bellum


1 Answers

Yes, it is possible, it is allowed by the OSGi spec, and it is not even bad practice.

Let me see if I have understand you right. You have bundle A, which exports a package in its manifest, and bundle B, which imports it. You want to do it without starting bundle A, right?

Yes, this is possible. If the only purpose of bundle A is to provide some java classes and packages, like a library, it may even not have an activator. According to the OSGi spec all exported packages of a bundle are available to other bundles right after the bundle goes in the resolved state (i.e. it is installed, dependencies are resolved, but it is not yet started).

The state of bundle B, which uses bundle A, doesn't matter. It also doesn't matter whether bundle B uses classes from the imported package or not. You also don't have to do anything special, just declare the dependency in the usual OSGi way:

Bundle A manifest: Export-Package: com.acme.aaa

Bundle B manifest: Import-Package: com.acme.aaa

You should install bundle A and check whether it has gone in the RESOLVED state, i.e. whether its dependencies are satisfied in the system. If yes, you can leave it like this and use it in bundle B.

The people above probably confuse importing packages with using a SERVICE in OSGi. Providing a SERVICE (i.e. a "live" java object) requires the bundle to be started and its activator to be called. Just exporting packages doesn't.

like image 133
pooh Avatar answered Dec 12 '25 09:12

pooh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!