Ich habe eine Klasse, die einen OSGi-Container programmatisch startet, Bündel lädt und dann startet. Das Problem, das ich habe, ist, dass die Bündel nicht in der richtigen Reihenfolge gestartet werden. Einige Pakete werden geladen und gestartet, bevor sie vom Start abhängig sind.So stellen Sie sicher, dass OSGi-Bundles in der richtigen Reihenfolge geladen werden
Wie kann ich sicherstellen, dass alle Bundles in der richtigen Reihenfolge gestartet werden, nicht vor denen, von denen sie abhängig sind?
S.S.Jede Codeverbesserung vorgeschlagen auf was ich bisher habe, sind willkommen.
Vielen Dank im Voraus.
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
public class App {
public static void main(String[] args) throws BundleException, URISyntaxException {
App app = new App();
app.initialize();
}
private void initialize() throws BundleException, URISyntaxException {
Map<String, String> map = new HashMap<String, String>();
// make sure the cache is cleaned
map.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
map.put("ds.showtrace", "true");
map.put("ds.showerrors", "true");
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Framework framework = frameworkFactory.newFramework(map);
System.out.println("Starting OSGi Framework");
framework.init();
File baseDir = new File("target/dist-1.0-SNAPSHOT-bin/plugins/");
loadScrBundle(framework);
// get all the Bundles from our plugins directory
File[] fList = baseDir.listFiles();
for (File file : fList) {
if (file.isFile()) {
System.out.println(file.getAbsolutePath());
if (file.getName().endsWith(".jar")) {
framework.getBundleContext().installBundle(file.toURI().toString());
}
} else if (file.isDirectory()) {
// recurse
}
}
for (Bundle bundle : framework.getBundleContext().getBundles()) {
bundle.start();
System.out.println("Bundle: " + bundle.getSymbolicName());
if (bundle.getRegisteredServices() != null) {
for (ServiceReference<?> serviceReference : bundle.getRegisteredServices())
System.out.println("\tRegistered service: " + serviceReference);
}
}
}
private void loadScrBundle(Framework framework) throws URISyntaxException, BundleException {
URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
if (url == null)
throw new RuntimeException("Could not find the class org.apache.felix.scr.ScrService");
String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
System.out.println("Found declarative services implementation: " + jarPath);
framework.getBundleContext().installBundle(jarPath).start();
}
}