View Javadoc

1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //   this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //   this list of conditions and the following disclaimer in the documentation  
12  //   and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //   nor the names of its contributors may be used to endorse or promote products  
15  //   derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  
29  package org.objectledge.web;
30  
31  import java.io.File;
32  import java.io.IOException;
33  
34  import javax.servlet.ServletConfig;
35  import javax.servlet.ServletContext;
36  import javax.servlet.ServletException;
37  import javax.servlet.http.HttpServlet;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  import org.apache.log4j.BasicConfigurator;
42  import org.apache.log4j.Logger;
43  import org.objectledge.container.LedgeContainer;
44  import org.objectledge.filesystem.ClasspathFileSystemProvider;
45  import org.objectledge.filesystem.FileSystem;
46  import org.objectledge.filesystem.FileSystemProvider;
47  import org.objectledge.filesystem.LocalFileSystemProvider;
48  import org.objectledge.filesystem.ServletFileSystemProvider;
49  
50  /***
51   * LedgeServlet is the entry point of a Ledge application in the servlet environment.
52   * 
53   * <p>
54   * It performs startup and shutdown of the system and forwards HTTP requests to designated
55   * components through {@link org.objectledge.web.HttpDispatcher} interface.
56   * </p>
57   * 
58   * <h3>Configuration parameters</h3>
59   * <table>
60   * <tr>
61   * <th>name</th>
62   * <th>default</th>
63   * <th width="100%">description</th>
64   * </tr>
65   * <tr>
66   * <td>root</td>
67   * <td><code>javax.servlet.context.tempdir</code> context attribute, or <code>user.dir</code>
68   * system property if the former is not available.</td>
69   * <td>The root directory of the local file system td use.</td>
70   * </tr>
71   * <tr>
72   * <td>config</td>
73   * <td>/config</td>
74   * <td>The base path of system configuration, within Ledge FileSystem.</td>
75   * </tr>
76   * </table>
77   * 
78   * <p>
79   * The configuration parameters may be given as servlet initailizaion parameters in the
80   * <code>web.xml</code> file, or as servlet context attributes. In the latter case the actual name
81   * of the attribute is composed of the servlet-name under which LedgeServlet is registered, a dot
82   * and the actual parameter name. Consider the following <code>web.xml</code> file:
83   * </p>
84   * <pre>
85   * &lt;?xml version="1.0"?&gt;
86   * &lt;web-app&gt;
87   *   &lt;servlet&gt;
88   *     &lt;servlet-name&gt;ledge&lt;/servlet-name&gt;
89   *     &lt;servlet-class&gt;org.objectledge.web.LedgeServlet&lt;/servlet-class&gt;
90   *   &lt;/servlet&gt;
91   *   &lt;servlet-mapping&gt;
92   *     &lt;servlet-name&gt;ledge&lt;/servlet-name&gt;
93   *     &lt;url-pattern&gt;/ledge/*&lt;/url-pattern&gt;
94   *   &lt;/servlet-mapping&gt;
95   * &lt;/web-app&gt;
96   * </pre>
97   * 
98   * <p>
99   * The root directory can be used at deployment time with the following Tomcat application
100  * definition file:
101  * </p>
102  * <pre>
103  * &lt;?xml version="1.0"?&gt;
104  * &lt;Context path="/app"
105  *       docBase="/home/app/production/app.war"
106  *       reloadable="false"&gt;
107  *   &lt;Parameter name="ledge.root" 
108  *       value="/home/app/production/work" 
109  *       override="false"/&gt;
110  * &lt;/Context&gt;
111  * </pre>
112  * 
113  * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
114  * @version $Id: LedgeServlet.java,v 1.19 2005/07/07 08:29:27 zwierzem Exp $
115  */
116 public class LedgeServlet extends HttpServlet
117 {
118     /*** The request dispatcher. */
119     protected HttpDispatcher dispatcher;
120     
121     /*** The container. */
122     protected LedgeContainer container;
123     
124     /***
125      * {@inheritDoc}
126      */
127     protected void service(HttpServletRequest request, HttpServletResponse response)
128         throws ServletException, IOException
129     {
130         if(!dispatcher.dispatch(request, response))
131         {
132             super.service(request, response);
133         }
134     }
135 
136     /***
137      * {@inheritDoc}
138      */
139     public void init(ServletConfig servletConfig) throws ServletException
140     {
141         BasicConfigurator.configure();
142         Logger log = Logger.getLogger(ServletConfig.class);
143 
144         ServletContext context = servletConfig.getServletContext();
145         String ctxRootParam = servletConfig.getServletName()+".root";
146         String ctxConfigParam = servletConfig.getServletName()+".config";
147 
148         String root = servletConfig.getInitParameter("root");
149         if(root == null)
150         {
151             root = (String)context.getInitParameter(ctxRootParam);
152         }
153         if(root == null)
154         {
155             File tempDir = (File)context.getAttribute("javax.servlet.context.tempdir");
156             if(tempDir == null)
157             {
158                 root = System.getProperty("java.io.tmpdir");
159             }
160             else
161             {
162                 root = tempDir.getAbsolutePath(); 
163             }
164         }
165 
166         String config = servletConfig.getInitParameter("config");
167         if(config == null)
168         {
169             config = (String)context.getInitParameter(ctxConfigParam);
170         }
171         if(config == null)
172         {
173             config = "/config";
174         }
175 
176         log.info("starting up: root="+root+" config="+config);
177 
178         LocalFileSystemProvider lfs = new LocalFileSystemProvider("local", root);
179         ServletFileSystemProvider sfs = new ServletFileSystemProvider("servlet", context);
180         ClasspathFileSystemProvider cfs = new ClasspathFileSystemProvider("classpath", 
181             getClass().getClassLoader());
182         FileSystem fs = new FileSystem(new FileSystemProvider[] { lfs, sfs, cfs }, 4096, 4194304);
183         try
184         {
185             container = new LedgeContainer(fs, config, getClass().getClassLoader());    
186         }
187         catch(Exception e)
188         {
189             log.error("failed to initialize container", e);
190             throw new ServletException("failed to initialize container", e);
191         }
192 
193         dispatcher = (HttpDispatcher)container.getContainer().
194             getComponentInstance(HttpDispatcher.class);
195         if(dispatcher == null)
196         {
197             log.error("dispatcher component is missing");
198             throw new ServletException("dispatcher component is missing");
199         }
200     }
201 }