在Tomcat中使用来自Java Web应用程序的OSGi Bundle

Nic*_*las 15 java tomcat osgi eclipse-plugin equinox

我试图从Java Web应用程序调用OSGi包的方法.两者都应该在Tomcat 7上运行.

我已经编写了一个普通的Java应用程序来调用OSGi包中的方法,如本网站所述:http://drupal.osgibook.org/node/37.

为了获得Equinox环境的上下文,我从应用程序启动它并从内部安装了bundle.此外,上下文用于检索正在运行的包的服务引用并获取其服务.

EquinoxRunner类的runEquinox方法:

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

public BundleContext runEquinox([...]) throws Exception {
    [...]

    BundleContext bundleContext = EclipseStarter.startup(new String[]{"-console"}, null);
    bundleContext.installBundle("file:C:/.../plugins/myosgiclass.interface_1.0.0.201108301327.jar");
    Bundle bundleTranslationImpl =  bundleContext.installBundle("file:C:/.../plugins/myosgiclass.impl_1.0.0.201108301327.jar");
    bundleTranslationImpl.start();

    [...]
    return bundleContext;
}
Run Code Online (Sandbox Code Playgroud)

和ServiceRunner类的invokeMethod:

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

[...]

public Object invokeMethod(BundleContext bundleContext, Object value, [...]){
    ServiceReference serviceReference = bundleContext.getServiceReference(MyOSGiClass.class.getName());
    Object result = null;
    if (serviceReference != null) {
        MyOSGiClass myOSGiClass = (MyOSGiClass) bundleContext.getService(serviceReference);
        if (myOSGiClass != null) result = myOSGiClass.method(value);
        bundleContext.ungetService(serviceReference);
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

现在,在使用eclipse桥的 Tomcat上,我不知道如何检索Equinox环境的正确上下文.当我尝试使用Equinox在Tomcat上运行它时,我得到NoClassDefFound Exceptions.我很感激有关如何解决这个问题的任何建议.

非常感谢提前.干杯,尼克

小智 2

当您嵌入这样的 OSGi 框架,然后想要从外部环境访问 OSGi 服务时,您需要确保OSGi 内部和外部的服务接口相同。

因此,配置您的 OSGi 容器以将服务接口的包从 Tomcat 导出到 OSGi。要实现此目的,请使用“FRAMEWORK_SYSTEMPACKAGES_EXTRA”属性配置您的 OSGi 框架。

更多信息请参见http://felix.apache.org/site/apache-felix-framework-launching-and-embedding.html#ApacheFelixFrameworkLaunchingandEmbedding-hostservices(即使是针对 Apache Felix,嵌入 API 也是标准化的)。