1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 * <?xml version="1.0"?>
86 * <web-app>
87 * <servlet>
88 * <servlet-name>ledge</servlet-name>
89 * <servlet-class>org.objectledge.web.LedgeServlet</servlet-class>
90 * </servlet>
91 * <servlet-mapping>
92 * <servlet-name>ledge</servlet-name>
93 * <url-pattern>/ledge/*</url-pattern>
94 * </servlet-mapping>
95 * </web-app>
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 * <?xml version="1.0"?>
104 * <Context path="/app"
105 * docBase="/home/app/production/app.war"
106 * reloadable="false">
107 * <Parameter name="ledge.root"
108 * value="/home/app/production/work"
109 * override="false"/>
110 * </Context>
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 }