How can I add packages to a bundle's import from within code? I need it since I use libraries which rely on reflection and require other packages and I don't want to need to manually add those packages to MANIFEST.MF for each bundle I develop
相关问题
- How to reimport module with ES6 import
- how to deploy wab files in glassfish
- Webpack / typescript require works but import does
- Why can't Python's import work like C'
- Pygame for Python 3.2 on mac - import error
相关文章
- Java “static import” vs. “import static” in Java 8
- How can I convert a Bundle to a PersistableBundle?
- Bundle install is not working
- Is there a way to modify the entity mapping config
- Check if two Bundle objects are equal in Android?
- MVC 5 Bundle Error
- How do I add a Python import path permanently?
- Python Imports From The Directory Above
In addition to Filippo's solution, you can try to invert the dependency. F.i. instead of calling and inspecting the other bundles, you can let the bundles do that. Another way would be using a bundle tracker and obtaining the ClassLoader of the Bundle tracked. With this class loader you can act "as the bundle", so you don't need the Import-Package clause anymore.
When I started using OSGi, this was one of the first requirements I came up. Over time, I realized that in almost all cases, there is a cleaner and more consistent solution. So, think about it, if you really need this dependency. Is there no way to invert or abstract it to create a generic solution?
If nothing helps (as a last resort), you can also create (in Memory) a fragment with your core bundle as host, providing the required imports. The BundleContext offers you a method to load a bundle from a stream. You then have to update and call refreshpackages(via PackageAdmin service) on your host bundle in order to get the updated ClassLoader (implies a restart of your bundle). However, at the end, you'll have access to all your packages.
As a side note, I would not recommend to manipulate your host bundle by tweaking the import statement and then update... This renders your bundle indeterministic and won't work with signed bundles. Also, this is against everything one expects from OSGi (imagine a growing bundle over time... you need to shrink the imports at some point as well!!!)
Cheers, Mirko
You cannot. Import-Packages are evaluated in the Resolution phase. (Phases are Installed -> Resolved -> Active ).
Your code is executed when the bundle is Active, therefore too late to add Import-Packages.
You can do 2 things: