Download the dynamic OSGi package from the file system

I have a modular application that uses the OSGi infrastructure. Here I am using the org.eclipse.equinox.common_3.4.0 OSGi container. So, now the application is already running with all osgi packages installed and active, and I show all active OSGi packages in the user interface by looping with a hash map based on some actions. Now that the application is already running, I want to install a new OSGi package from the file system, providing this new package to the OSGi container of the application so that it starts this package.

How do I achieve this? I tried to read the OSGi package as a JarInputstream and read the full path of the bundle bundle activator class and tried to instantiate this method using the Class.forName ("") class and inject the type into the BundleActivator interface. But at startup, it takes a connectivity context as an argument to start the method.

Is there a way when I can simply provide the OSGi package to the container pragmatically so that it takes care of installing and running the package, and then my user interface will automatically select this new package name on the display.

+2
source share
1 answer

Assuming you have a file to download, you can install the package as:

void install( BundleContext context, File file) throws Exception { Bundle b = context.installBundle( file.toURI().toString() ); b.start(); } 

And you can delete it (if the file is gone):

 void uninstall( BundleContext context, File file) throws Exception { Bundle b = context.getBundle( file.toURI().toString() ); b.uninstall(); } 

You get the BundleContext from the Activator component or Declarative services to be activated. These are recommended methods, but in severe cases you can also use:

 BundleContext context = FrameworkUtil.getBundle( this.getClass() ).getBundleContext(); 

Although it is convenient, it bypasses some mechanism that you might want to use in the future, so it is better to use the context in the recommended form

+4
source

Source: https://habr.com/ru/post/1271926/


All Articles