Ale*_*lex 2 osgi web-applications war osgi-bundle embedded-osgi
我有兴趣在我的WAR中添加一个OSGI容器,但我找不到有关如何执行此操作的教程或文档.我找到了一些根本没用的东西.我对Felix实现和Atlassian实现很感兴趣.
我愿意这样做,以便我的战争接受插件,我可以动态扩展我的Web应用程序,并将其部署到任何Web服务器.
任何指向文档的链接?任何帮助表示赞赏.
将OSGi Framework启动程序添加到Web应用程序并不是什么大问题.
您需要添加一个侦听器以在web.xml中启动框架启动程序
Run Code Online (Sandbox Code Playgroud)<listener> <listener-class>at.badgateway.StartupListener</listener-class> </listener>
startuplistener看起来像这样
public class StartupListener implements ServletContextListener {
//vars
@Override
public void contextInitialized(ServletContextEvent event) {
// set props
Map<String, String> config = new HashMap<String, String>();
config.put(Constants.FRAMEWORK_STORAGE, "path to cache");
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
try {
// get framework and start it
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
framework = frameworkFactory.newFramework(config);
framework.start();
// start existing bundles
bundleContext = framework.getBundleContext();
starter = new MyBundleStarter(servletContext, bundleContext);
starter.launch();
} catch (Exception ex)
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// stop framework
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的引文中处理MyBundlestarter类,它是激活战争中包含的所有包的类.(例如/ WEB-INF/Osgi-Bundles)
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
public class MyBundleStarter{
private BundleContext bundleContext = null;
public void launch() throws Exception {
ArrayList<Bundle> availableBundles= new ArrayList<Bundle>();
//get and open available bundles
for (URL url : getBundlesInWar()) {
Bundle bundle = bundleContext.installBundle(url.getFile(), url.openStream());
availableBundles.add(bundle);
}
//start the bundles
for (Bundle bundle : availableBundles) {
try{
bundle.start();
}catch()
}
private List<URL> getBundlesInWar() throws Exception {
// returns a list of URLs located at destination
}
}
Run Code Online (Sandbox Code Playgroud)
最后但同样重要的是,您必须为项目添加一个osgi框架.
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
要么
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)