gpt4 book ai didi

web-applications - 在 war 中嵌入 OSGI

转载 作者:行者123 更新时间:2023-12-04 08:35:28 26 4
gpt4 key购买 nike

我有兴趣将 OSGI 容器添加到我的 WAR 中,但我找不到有关如何执行此操作的教程或文档。我发现了一些根本没有用的东西。我对 Felix 实现和 Atlassian 实现很感兴趣。

我愿意这样做,以便我的 war 接受插件,并且我可以动态扩展我的 Web 应用程序并将其部署到任何 Web 服务器。

是否有指向文档或其他内容的链接?感谢您的帮助。

最佳答案

将 OSGi 框架启动器添加到 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 类,它是激活您的 war 中包含的所有包的类。 (例如/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>

关于web-applications - 在 war 中嵌入 OSGI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com