Created
May 27, 2011 13:52
-
-
Save criminy/995291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Abstract class which bootstraps the Spring MVC system on top of the OSGI interface 'JettyService' on startup. | |
*/ | |
public abstract class AbstractOsgiSpringMvcApplication implements BundleContextAware,InitializingBean | |
{ | |
private BundleContext bundleContext; | |
private JettyService jettyService; | |
private Handler registered; | |
public abstract String getContextConfigLocation(); | |
public abstract String getServletName(); | |
public abstract String getRootPath(); | |
public abstract String getPathSpec(); | |
public void afterPropertiesSet() throws Exception { | |
try { | |
init(bundleContext); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
/** | |
* Extension of DispatcherServlet which inserts the BundleContext through | |
* the ServletContext at the right moment. | |
* | |
* | |
*/ | |
final class D extends DispatcherServlet { | |
private static final long serialVersionUID = 1L; | |
private BundleContext ctx; | |
private AbstractOsgiSpringMvcApplication parent; | |
public D(AbstractOsgiSpringMvcApplication parent,BundleContext c) { | |
this.parent = parent; | |
this.ctx = c; | |
} | |
@Override | |
protected WebApplicationContext initWebApplicationContext() { | |
ServletContext context = getServletContext(); | |
context.setAttribute(OsgiBundleXmlWebApplicationContext.BUNDLE_CONTEXT_ATTRIBUTE,ctx); | |
this.setContextConfigLocation(parent.getContextConfigLocation()); | |
return super.initWebApplicationContext(); | |
} | |
} | |
/** | |
* Starts up the SpringMVC system and attaches it to the external OSGI HTTP service (JettyService). | |
* @param arg0 | |
* @throws Exception | |
*/ | |
final public void init(BundleContext arg0) throws Exception { | |
ServletHandler servletHandler = new ServletHandler(); | |
D dispatcherServlet = new D(this,arg0); | |
dispatcherServlet.setContextClass(Class.forName("org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext")); | |
ServletHolder holder = new ServletHolder(); | |
holder.setName(getServletName()); | |
holder.setServlet(dispatcherServlet); | |
holder.setInitOrder(1); | |
holder.setInitParameter("contextClass","org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext"); | |
ServletMapping mapping = new ServletMapping(); | |
mapping.setPathSpec(getPathSpec()); | |
mapping.setServletName(getServletName()); | |
servletHandler.addServlet(holder); | |
servletHandler.addServletMapping(mapping); | |
registered = jettyService.registerApp(getRootPath(), servletHandler,new HashMap()); | |
} | |
final public void destroy() throws Exception { | |
jettyService.unregisterApp(registered); | |
} | |
final public void setBundleContext(BundleContext arg0) { | |
this.bundleContext = arg0; | |
} | |
final public void setJettyService(JettyService jettyService) { | |
this.jettyService = jettyService; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment