View Javadoc

1   package org.apache.struts2.osgi;
2   
3   import org.apache.struts2.StrutsException;
4   
5   import javax.servlet.ServletContextListener;
6   import javax.servlet.ServletContextEvent;
7   import javax.servlet.ServletContext;
8   
9   /***
10   * ServletContextListener that starts Apache Felix
11   */
12  public class StrutsOsgiListener implements ServletContextListener {
13      public static final String OSGI_HOST = "__struts_osgi_host";
14      private FelixOsgiHost osgiHost;
15  
16      public void contextInitialized(ServletContextEvent sce) {
17          ServletContext servletContext = sce.getServletContext();
18          osgiHost = new FelixOsgiHost();
19          servletContext.setAttribute(OSGI_HOST, osgiHost);
20          try {
21              osgiHost.init(servletContext);
22          } catch (Exception e) {
23              throw new StrutsException("Apache Felix failed to start", e);
24          }
25      }
26  
27      public void contextDestroyed(ServletContextEvent sce) {
28          try {
29              osgiHost.destroy();
30          } catch (Exception e) {
31              throw new StrutsException("Apache Felix failed to stop", e);
32          }
33      }
34  }