39

I have a Java application that runs in OSGi/Equinox. From this application, I need to spawn Java subprocesses (e.g. via ProcessBuilder.start()) that run in OSGi/Equinox as well in order to handle class loading correctly. The subprocess will require multiple bundles, so I would ideally like some fairly dynamic way of installing those bundles in the subprocess' Equinox container, such as by reading feature.xml files.

I have read through program launching here but I don't see how Equinox can fit into it. I also looked into doing something like this, but it wouldn't be very dynamic, especially when the entry-point bundle of the subprocess requires multiple other bundles, which require further bundles, etc.

So, how can I spawn a subprocess to run in OSGi/Equinox with a fairly dynamic way of loading bundles into the container?

Note: I need to use separate processes. The subprocesses will do data processing using a JNA native library that uses global variables (and I can't modify the native library). So, in order to be able to process different data concurrently, the data processing needs to run in separate processes.

mapeters
  • 1,067
  • 7
  • 11
  • Do you want to read the `feature.xml` files to find out which bundles are required? To resolve the installation dependencies, Eclipse reads the metadata parts of the p2 repositories created from the feature `feature.xml` and more. It sounds like you want to do what the Equinox p2 director does, whose [command line usage is described in the Eclipse help](https://help.eclipse.org/photon/topic/org.eclipse.platform.doc.isv/guide/p2_director.html). Where do the bundles in your case come from (from a directory, Eclipse installation or from a p2 repository)? – howlger Aug 31 '18 at 16:38
  • I think you can use the `EquinoxFactory.newFramework()` with the configuration data required, like `osgi.install.area` then you can add bundles to that framework, but that will run in the same jvm, don't remember exactly the name but it is like a CompositeTarget or was called in that way at some point, unfortunately I can't point to any docs on that. – nmorenor Nov 30 '18 at 17:51
  • `ProcessBuilder.start()` will just execute commands on your system, e.g. you need find a way to launch your processing application as an executable or script on your operating system. As this will be a completely new process, it does not really matter whether the invoking program is OSGi or something else. – Hannes Erven Aug 29 '22 at 20:38
  • This is an old question, but have you looked into the `EquinoxApplication` class provided by `org.eclipse.core.runtime.adaptor.EquinoxApplication`. It lets you start an OSGi/Equinox container in a separate process and install bundles in the container. – old_dd Dec 21 '22 at 02:49

1 Answers1

0

you can use the Equinox Launcher API. Here's an example of how you can use the Equinox Launcher api to launch a new instance of equinox with a set of bundles: `

EquinoxLauncher launcher = new EquinoxLauncher();
String equinoxHome = "/path/to/equinox/home"; 
String[] bundlePaths = { "/path/to/bundle1.jar", "/path/to/bundle2.jar" }; 

EquinoxRunConfiguration runConfig = launcher.newConfiguration();
runConfig.setWorkingDir(new File(equinoxHome));
runConfig.setFramework(new File(equinoxHome, "plugins/org.eclipse.osgi.jar"));
runConfig.addProgramArg("-console");
runConfig.addProgramArg("-noExit");
for (String bundlePath : bundlePaths) {
    runConfig.addBundle(new File(bundlePath).toURI());
}

EquinoxRunMonitor monitor = launcher.launch(runConfig);

`