View Javadoc

1   /* ModuleHttpServlet.java - Awaiting description
2    *
3    * Copyright (c)2005 Roscopeco Open Technologies & Contributors
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   * File version: $Revision: 1.1 $ $Date: 2005/08/17 23:44:00 $
18   * Originated: 11-Jul-2005
19   * Author: Ross Bamford (rosco<at>roscopeco.co.uk)
20   */
21  
22  package org.roscopeco.janno.util;
23  
24  import java.io.IOException;
25  
26  import javax.servlet.Servlet;
27  import javax.servlet.ServletConfig;
28  import javax.servlet.ServletException;
29  import javax.servlet.GenericServlet;
30  import javax.servlet.ServletRequest;
31  import javax.servlet.ServletResponse;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import static org.roscopeco.janno.core.CoreConstants.LOG_SEPARATOR;
37  import static org.roscopeco.janno.util.UtilityConstants.SERVLET_KEY_PARAM;
38  import static org.roscopeco.janno.util.UtilityConstants.SERVLET_MODULE_PARAM;
39  import static org.roscopeco.janno.util.WrapperUtils.*;
40  
41  /*** 
42   * Utility class that allows <code>Servlet</code> implementations supplied
43   * by Moxy modules to be mapped into <code>web.xml</code>. This is a simple 
44   * class that just obtains the servlet implementation registered in the named
45   * module with the specified key. This information is supplied in context-
46   * parameters:
47   * <p/>
48   * <ul>
49   *    <li><strong>janno.servlet.context</strong> - full path to the context within
50   *    which the {@link javax.servlet.Servlet} implementation is registered.</li><br/>
51   *    
52   *    <li><strong>janno.servlet.key</strong> - Public key class for this servlet.
53   *    This is the key by which the implementation is registered in the root module
54   *    context.</li><br/>
55   * </ul>
56   * <p/>
57   * Note that servlets supplied by modules are directly fetched from the module
58   * context, rather than being injected directly. This allows multiple modules
59   * to supply the same servlet or use the same key (e.g. the <code>Servlet</code>
60   * class) without any clashes. When registering Servlet implementations in
61   * modules you should register the facade adapter in <code>parent</code> rather
62   * than exporting it to the root context. You will also need to use an
63   * implementation hiding adapter around that one if you're not exporting your
64   * Servlet class itself.
65   * <p/>
66   * Note also that the supplied key doesn't <i>have</i> to be a class, but it is 
67   * always looked up as a class first. If this results in an exception then it will
68   * be tried as a straight String key. Class lookups are done in the module's realm,
69   * so you don't have to have exported your key class.
70   * <p/>
71   * This class is compatible with any <code>Servlet</code> implementation, not 
72   * just <code>HttpServlet</code>. The instantiated object must be 
73   * assignment-compatible with the {@link javax.servlet.Servlet} class. The 
74   * wrapped servlet is fetched <strong>once only</strong> from the specified 
75   * context (during <code>init(ServletConfig)</code>), and lifecycle is managed
76   * by the wrapper. This, in combination with the Servlet specification's
77   * requirement for a null constructor, should enable any servlet to be used
78   * with the wrapper with no modification.
79   *
80   * @author Ross Bamford (rosco&lt;at&gt;roscopeco.co.uk)
81   * @version $Revision: 1.1 $ $Date: 2005/08/17 23:44:00 $ 
82   */
83  public class ServletWrapper extends GenericServlet implements WrapperImpl
84  {
85    static Log log = LogFactory.getLog(ServletWrapper.class); /* hide */
86    
87    private Servlet delegate = null;
88    Servlet delegate() {                // for testmodel
89      return delegate;
90    }
91    
92    /* Testmodel constructor */
93    ServletWrapper(Servlet testDelegate) {
94      this.delegate = testDelegate;
95    }
96    
97    /* Real constructor */
98    public ServletWrapper() {
99      super();
100   }
101    
102   /***
103    * Initializes the wrapper, obtains the delegate instance, and passes the
104    * call through it it's <code>init(ServletConfig)</code> method, after
105    * which point initialization continues in the regular Servlets manner.
106    */
107   @Override
108   public void init(ServletConfig config) throws ServletException {
109     super.init(config);
110     log.info(LOG_SEPARATOR);
111     log.info("ServletWrapper init begins...");
112     
113     try {
114       delegate = (Servlet)getWrappedImpl(this,SERVLET_MODULE_PARAM,SERVLET_KEY_PARAM);
115     } catch (ClassCastException e) {
116       log.error("Specified component is not a valid implementation of javax.servlet.Servlet");
117     }
118     
119     // init delegate
120     log.debug("Initialising delegate...");
121     delegate.init(config);
122     log.info("Wrapper initialisation complete");
123   }
124   
125   /***
126    * Passes the request and response to the delegate's <code>service</code> 
127    * method.
128    */
129   @Override
130   public void service(ServletRequest request, ServletResponse response)
131   throws ServletException, IOException {
132     if (delegate == null) throw(new ServletException("ServletWrapper not initialized!"));
133     delegate.service(request,response);   
134   }
135   
136   /***
137    * Calls the delegate's destroy method and performs wrapper cleanup.
138    */
139   public void destroy() {
140     log.debug("Destroying delegate...");
141     if (delegate != null) {
142       delegate.destroy();
143     } else {
144       log.warn("Cannot destroy() uninitialised delegate Servlet");
145     }
146     log.info("ServletWrapper finished");
147   }
148   
149   /* WrapperImpl - allows common code to log as this class */
150   public Log log() { return log; }
151 }