我試圖從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'項目? –