OSGiコンテナをプログラム的に起動し、バンドルをロードしてから起動するクラスがあります。私が抱えている問題は、バンドルが正しい順序で開始されていないということです。一部のバンドルは、起動に依存するバンドルより前に読み込まれ、起動されます。OSGiバンドルが正しい順序でロードされるようにする方法
すべてのバンドルが依存しているバンドルよりも前に、正しい順序でバンドルされていることを確認するにはどうすればよいですか?
P.S:私がこれまでに抱いていたことについて示唆しているコードの改善はどれも歓迎します。
ありがとうございます。あなたが開始順序に依存するOSGiコードを記述する場合、あなたはOSGiのコードを持っていない
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();
}
}