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

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

任何指向文档或其他内容的链接?任何帮助表示赞赏。

最佳答案

将 OSGi 框架启动器添加到 Web 应用程序并不是什么大问题。

您需要在 web.xml 中添加一个监听器来启动框架启动器



启动监听器可能看起来像这样

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/

10-13 08:03