How to notify Bundle A that there are other Bundles that need to be loaded
and installed
I am working on a project in which I need to dynamically load and install
the OSGI bundles using Java. I will be having around 2-3 OSGi bundles that
I need to install.
Now the problem is, those 2-3 bundles has to be installed from one of the
other bundles which is my main bundle. So meaning, I will load Bundle A
first of all, then Bundle A will get notified of how many other Bundles I
need to install more which can be Bundle B and Bundle C or any other. So
Bundle A will be responsible of loading and installing other Bundles basis
on some kind of notification mechanism.
Now I am not sure what's the right way to do this. As I am totally new to
OSGi framework thing. Any step by step guidance will be appreciated on
this.
I am loading and installing Bundle A from my main application. I am naming
Bundle A as my FrameworkBundle that will be responsible for other Bundles.
Below is my main application that will load and install Bundle A.
FileUtils.deleteDirectory(new File("felix-cache"));
FrameworkFactory frameworkFactory =
ServiceLoader.load(FrameworkFactory.class).iterator().next();
Framework framework = frameworkFactory.newFramework(new HashMap<String,
String>());
framework.start();
BundleContext bundleContext = framework.getBundleContext();
modulesNameVersionHolder.put("Bundle-A", "1.0.0");
List<Bundle> installedBundles = new LinkedList<Bundle>();
String basePath = "C:\\StaasClientTool\\LocalStorage";// this is where I
have stored all my bundles.
for (Map.Entry<String, String> entry : modulesNameVersionHolder.entrySet()) {
String version = entry.getValue();
final String filename = name + Constants.DASH + version +
Constants.DOTJAR;
final String localFilename = BullseyeModulesConstants.FILE_PROTOCOL +
basePath+ File.separatorChar + filename;
installedBundles.add(bundleContext.installBundle(localFilename));
}
for (Bundle bundle : installedBundles) {
bundle.start(); // this will start the Bundle A
}
Now how should I notify Bundle A that there are other Bundles that need to
be loaded and installed. And if it gets notified then where I should be
loading and installing the other bundles (Bundles B and Bundle C) in
Bundle A's code.
Below is the Activator class for Bundle A which will be called once I am
going to install and start Bundle A from the above code.
public class Activator implements BundleActivator {
private static final String BUNDLE_VERSION_KEY = "Bundle-Version";
private static Logger s_logger =
Logger.getLogger(Activator.class.getName());
@Override
public void start(BundleContext context) throws Exception {
final Bundle bundle = context.getBundle();
final String bundleName = bundle.getSymbolicName();
final String bundleVersion = (String)
bundle.getHeaders().get(BUNDLE_VERSION_KEY);
}
@Override
public void stop(BundleContext context) throws Exception {
}
}
Any help will be appreciated on this. Any simple example that can clear my
doubts in how should I proceed further will really help me on this.
I am looking for a simple example basis on my above scenario. Any help
will be appreciated on this.
Update
I will be having Bundles Information in my database. Meaning for this
bundle, this is the version. So I will be using this database and then I
will check which version of bundles I need to install and then basis on
that, I will be loading and installing the bundles.
So What I am trying to do is, I want to have one Framework Bundle,
responsible for loading, installing/uninstall the bundles instead of my
main application code doing it. And my framework Bundle will also be
responsible in delegating the input calls to various other bundles that I
have installed. As all my bundles will be implementing same interface. So
any input will go to all the Bundles for processing and then after
processing of those inputs, response will be returned back to my framework
bundle.
That's what I have thought of. Is there anything wrong in my approach. I
am just trying to make the system much cleaner.
No comments:
Post a Comment