View Javadoc

1   /*
2    * Copyright (c) 2005 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL licence
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/copyleft/lesser.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.spring.server.deployment.jetty;
14  
15  import org.abstracthorizon.extend.server.deployment.Module;
16  import org.abstracthorizon.extend.server.deployment.ModuleId;
17  import org.abstracthorizon.extend.support.spring.service.ServiceApplicationContextModule;
18  import org.mortbay.jetty.Server;
19  import org.mortbay.jetty.servlet.Context;
20  import org.mortbay.jetty.webapp.WebAppContext;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.context.ApplicationContext;
24  
25  /**
26   * <p>
27   *   Module that represents a war module deployed with Jetty. Also it can define beans in
28   *   a same way as service application module (this is an extension of it).
29   * </p>
30   * <p>
31   *   It uses &quot;WEB-INF/web-application.xml&quot; for application context xml.
32   * </p>
33   * @author Daniel Sendula
34   */
35  public class JettyWebApplicationContext extends ServiceApplicationContextModule {
36  
37      /** Logger */
38      private final Logger logger = LoggerFactory.getLogger(JettyWebApplicationContext.class);
39  
40      /** Reference to embedded Jetty */
41      protected Server jetty;
42  
43      /** Jetty context */
44      protected WebAppContext context;
45  
46      /** Context of web server - Jetty's service application context */
47      protected ApplicationContext webServerContext;
48  
49      /** Context path for this module */
50      protected String contextPath;
51  
52      /**
53       * Empty constructor
54       */
55      public JettyWebApplicationContext(ModuleId moduleId) {
56          super(moduleId);
57      }
58  
59      /**
60       * Obtains references to the embedded Jetty and to the host.
61       * Sets this module to be depended on Jetty's service application module.
62       */
63      protected void createInternal() {
64          Module module = (Module)webServerContext;
65          getDependsOn().add(module);
66          module.getDependOnThis().add(this);
67  
68          jetty = (Server)webServerContext.getBean("jetty");
69      }
70  
71      /**
72       * Creates and adds context to the obtained reference of the host.
73       */
74      @Override
75      protected void startInternal() {
76  
77          // TODO check protocol
78          String contextPath = getContextPath();
79          if (contextPath == null) {
80              contextPath = "/" + getModuleId().getArtifactId();
81              setContextPath(contextPath);
82          }
83          if (!contextPath.startsWith("/")) {
84              contextPath = "/" + contextPath;
85              setContextPath(contextPath);
86          }
87  
88          context = new WebAppContext(getWorkingLocation().toString(), contextPath);
89          if (logger.isDebugEnabled()) {
90          	logger.debug("Started context: " + contextPath);
91          }
92  
93          jetty.addHandler(context);
94  
95          try {
96              context.start();
97          } catch (Exception e) {
98              throw new RuntimeException(e);
99          }
100     }
101 
102     /**
103      * Removes context from the host.
104      */
105     @Override
106     protected void stopInternal() {
107         jetty.removeHandler(context);
108         try {
109             context.stop();
110         } catch (Exception e) {
111             throw new RuntimeException(e);
112         }
113         context = null;
114     }
115 
116     /**
117      * Removes all references of the embedded Jetty and the host
118      */
119     @Override
120     protected void destroyInternal() {
121         jetty = null;
122     }
123 
124     /**
125      * It uses &quot;WEB-INF/web-application.xml&quot; for application context xml.
126      * @return &quot;WEB-INF/web-application.xml&quot;
127      */
128     protected String getContextFileName() {
129         return "WEB-INF/web-application.xml";
130     }
131 
132     /**
133      * Jetty's service application context
134      * @return Jetty's service application context
135      */
136     public ApplicationContext getWebServerContext() {
137         return webServerContext;
138     }
139 
140     /**
141      * Set's Jetty's service application context
142      * @param webServerContext Jetty's service application context
143      */
144     public void setWebServerContext(ApplicationContext webServerContext) {
145         this.webServerContext = webServerContext;
146     }
147 
148     /**
149      * @return Returns the contextPath.
150      */
151     public String getContextPath() {
152         return contextPath;
153     }
154 
155     /**
156      * @param contextPath The contextPath to set.
157      */
158     public void setContextPath(String contextPath) {
159         this.contextPath = contextPath;
160     }
161 
162     /**
163      * Returns Jetty context
164      *
165      * @return Jetty context
166      */
167     public Context getJettyContext() {
168         return context;
169     }
170 
171 }