according to http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fua_help_war.htm I set up a dedicated Tomcat server that runs several eclipse-help-plugins.
The server and also the help is up and running well. But now I realized that stopping the server especially the OSGi-Framework seems to be a problem. I always have to kill the server process if the help-war is deployed and I believe that I have to shutdown the OSGi-Framework gracefully.
After some investigation I came up with the following implementation of a ServletContextListener, that stops the system-bundle by calling 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");
}
}
But FrameworkUtil.getBundle(org.eclipse.core.runtime.adaptor.EclipseStarter.class) always returns null, so I never get a reference to a BundleContext to stop the framework.
EDIT: I changed the code to sce.getServletContext().getAttribute("osgi-bundlecontext") in contextDestroyed() and contextInitialized() as well, but in both cases I get no reference to the bundle context. Bundle context is always null.
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");
}
}
How do I get a bundleContext in this situation to stop the system bundle? Or how do I gracefully stop the OSGi-Framework when the server shuts down?