根据http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fua_help_war.htm我设置了一个运行多个 eclipse-help- 的专用 Tomcat 服务器插件。
服务器和帮助都启动并运行良好。但现在我意识到停止服务器尤其是 OSGi 框架似乎是一个问题。如果部署了帮助战,我总是必须终止服务器进程,并且我相信我必须优雅地关闭 OSGi 框架。
经过一番调查,我想出了以下 ServletContextListener 的实现,它通过调用 bundleContext.getBundle(0).stop() 来停止系统捆绑:
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.FrameworkUtil;
public class OsgiShutdownListener implements ServletContextListener {
/** {@inheritDoc} */
@Override
public void contextDestroyed(ServletContextEvent sce) {
Bundle bundle = FrameworkUtil.getBundle(org.eclipse.core.runtime.adaptor.EclipseStarter.class);
BundleContext bundleContext = bundle.getBundleContext();
try {
bundleContext.getBundle(0).stop();
} catch (BundleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** {@inheritDoc} */
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("starting");
}
}
但是 FrameworkUtil.getBundle(org.eclipse.core.runtime.adaptor.EclipseStarter.class) 总是返回 null,所以我从来没有得到对 BundleContext 的引用来停止框架。
编辑: 我在 contextDestroyed() 和 contextInitialized() 中也将代码更改为 sce.getServletContext().getAttribute("osgi-bundlecontext") ,但在这两种情况下我都没有引用包上下文。捆绑上下文始终为空。
public class OsgiShutdownListener implements ServletContextListener {
private BundleContext bundleContext;
/** {@inheritDoc} */
@Override
public void contextDestroyed(ServletContextEvent sce) {
// Bundle bundle =
// FrameworkUtil.getBundle(org.eclipse.core.runtime.adaptor.EclipseStarter.class);
// this.bundleContext = bundle.getBundleContext();
ServletContext context = sce.getServletContext();
this.bundleContext = (BundleContext) context
.getAttribute("osgi-bundlecontext");
try {
this.bundleContext.getBundle(0).stop();
} catch (BundleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** {@inheritDoc} */
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
this.bundleContext = (BundleContext) context
.getAttribute("osgi-bundlecontext");
}
}
在这种情况下如何获取 bundleContext 来停止系统捆绑?或者当服务器关闭时如何优雅地停止 OSGi 框架?