我有兴趣在我的WAR中添加OSGI容器,但找不到有关如何执行此操作的教程或文档。我发现一些根本没有用的东西。我对Felix实现和Atlassian实现感兴趣。
我愿意这样做,以便我的战争接受插件,并且我可以动态扩展Web应用程序并将其部署到任何Web服务器。
是否有任何指向文档的链接?任何帮助表示赞赏。
将OSGi Framework启动器添加到Web应用程序并不重要。
您需要添加一个侦听器以在web.xml中启动框架启动器
<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
}
}
在上引号中注意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
}
}
最后但并非最不重要的是,您必须在项目中添加osgi框架。
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
</dependency>
或
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi</artifactId>
</dependency>
我可以看到这是一篇旧文章,但也许对某人有用:这个writing在该主题中包含很多有用的东西,至少对我来说,这确实是一个很大的帮助。并且值得一看该页面上的其他帖子。
如果使用WebLogic托管应用程序,则可以将OSGi捆绑包嵌入WAR中,并将它们部署到系统定义的OSGi服务器中。很好,因为可以在WebLogic日志中自动看到来自OSGi日志服务的日志消息。取消部署应用程序后,捆绑包也会从目标OSGi服务器中删除。
有关更多信息,请参见Configuration OSGi containers或developing OSGi apps或此blog post。