我试图从Java Web应用程序调用OSGi包的方法。两者都应该在Tomcat 7上运行。在Tomcat中使用来自Java Web应用程序的OSGi包
我已经编写了一个普通的Java应用程序,它调用OSGi包中的方法,如本站所述:http://drupal.osgibook.org/node/37。
为了获得Equinox环境的上下文,我从应用程序开始了它,并从内部安装了捆绑包。此外,该上下文用于检索正在运行的捆绑服务的服务引用并获取其服务。
的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;
}
和ServiceRunner类的invokeMethod中:使用eclipse bridge现在
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;
}
,Tomcat上,我不知道如何检索Equinox环境的正确背景。当我尝试在Equinox的Tomcat上运行它时,我得到了NoClassDefFound异常。我将不胜感激关于如何解决这个问题的任何建议。
非常感谢。 Cheers,Nic
我认为关键是要保持对启动OSgi框架的控制。我不太了解Equinox的确切工作方式,但我确定他们有类似于Felix所描述的内容,在这里:http://felix.apache.org/site/apache-felix-framework-launching-and -embedding.html –
感谢您的评论。这也是我尝试的方法。虽然我需要在Tomcat服务器上运行我的项目,但上面描述的Bridge似乎是需要的 - 而且我无法手动启动。或者有什么方法可以绕过这个Bridge并使用手动启动的OSGi框架? – Nicolas
我看不出如何使用Tomcat将您与特定的servlet桥接实现联系起来。你为什么不开始定制'org.eclipse.equinox.servletbridge'项目? –