1 16 package org.apache.cocoon.servlet; 17 18 import java.io.File ; 19 import java.io.InputStream ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.util.Enumeration ; 23 import java.util.Set ; 24 25 import javax.servlet.RequestDispatcher ; 26 import javax.servlet.Servlet ; 27 import javax.servlet.ServletConfig ; 28 import javax.servlet.ServletContext ; 29 import javax.servlet.ServletException ; 30 31 48 49 public class BootstrapServlet extends ParanoidCocoonServlet { 50 51 protected File contextDir; 52 53 protected File getContextDir() throws ServletException { 54 55 ServletContext context = getServletContext(); 56 ServletConfig config = getServletConfig(); 57 58 log("getRealPath(\"/\") = " + context.getRealPath("/")); 59 60 String contextDirParam = config.getInitParameter("context-directory"); 61 62 if (contextDirParam == null) { 63 throw new ServletException ("The 'context-directory' parameter must be set to the root of the servlet context"); 64 } 65 66 if (contextDirParam.endsWith("/")) { 69 contextDirParam = contextDirParam.substring(0, contextDirParam.length() - 1); 70 } 71 72 this.contextDir = new File (contextDirParam); 74 if (!this.contextDir.exists()) { 75 String msg = "Context dir '" + this.contextDir + "' doesn't exist"; 76 log(msg); 77 throw new ServletException (msg); 78 } 79 80 if (!this.contextDir.isDirectory()) { 81 String msg = "Context dir '" + this.contextDir + "' should be a directory"; 82 log(msg); 83 throw new ServletException (msg); 84 } 85 86 context.log("Context dir set to " + this.contextDir); 87 88 return this.contextDir; 89 } 90 91 92 protected void initServlet() throws ServletException { 93 94 ServletContext newContext = new ContextWrapper(getServletContext(), this.contextDir); 95 ServletConfig newConfig = new ConfigWrapper(getServletConfig(), newContext); 96 97 this.servlet.init(newConfig); 98 } 99 100 105 public static class ConfigWrapper implements ServletConfig { 106 ServletConfig config; 107 ServletContext context; 108 109 113 public ConfigWrapper(ServletConfig config, ServletContext context) { 114 this.config = config; 115 this.context = context; 116 } 117 public String getServletName() { 118 return config.getServletName(); 119 } 120 121 public Enumeration getInitParameterNames() { 122 return this.config.getInitParameterNames(); 123 } 124 125 public ServletContext getServletContext() { 126 return this.context; 127 } 128 129 public String getInitParameter(String name) { 130 return config.getInitParameter(name); 131 } 132 } 133 134 140 public static class ContextWrapper implements ServletContext { 141 ServletContext context; 142 File contextRoot; 143 144 148 public ContextWrapper(ServletContext context, File contextRoot) { 149 this.context = context; 150 this.contextRoot = contextRoot; 151 } 152 153 public ServletContext getContext(String param) { 154 return this.context.getContext(param); 155 } 156 157 public int getMajorVersion() { 158 return this.context.getMajorVersion(); 159 } 160 161 public int getMinorVersion() { 162 return this.context.getMinorVersion(); 163 } 164 165 public String getMimeType(String param) { 166 return this.context.getMimeType(param); 167 } 168 169 174 public URL getResource(String path) throws MalformedURLException { 175 File file = new File (this.contextRoot, path); 176 if (file.exists()) { 177 URL result = file.toURL(); 178 return result; 180 } else { 181 return null; 183 } 184 } 185 186 190 public InputStream getResourceAsStream(String path) { 191 try { 192 URL url = getResource(path); 193 return (url == null) ? null : url.openStream(); 194 } catch(Exception e) { 195 this.context.log("getResourceAsStream(" + path + ") failed", e); 196 return null; 197 } 198 } 199 200 public RequestDispatcher getRequestDispatcher(String param) { 201 return this.context.getRequestDispatcher(param); 202 } 203 204 public RequestDispatcher getNamedDispatcher(String param) { 205 return this.context.getNamedDispatcher(param); 206 } 207 208 213 public Servlet getServlet(String param) throws ServletException { 214 return this.context.getServlet(param); 215 } 216 217 222 public Enumeration getServlets() { 223 return this.context.getServlets(); 224 } 225 226 231 public Enumeration getServletNames() { 232 return this.context.getServletNames(); 233 } 234 235 public void log(String msg) { 236 this.context.log(msg); 237 } 238 239 240 public void log(Exception ex, String msg) { 241 this.context.log(ex, msg); 242 } 243 244 public void log(String msg, Throwable thr) { 245 this.context.log(msg, thr); 246 } 247 248 251 public String getRealPath(String path) { 252 String result = this.contextRoot + path; 253 return result; 255 } 256 257 public String getServerInfo() { 258 return this.context.getServerInfo(); 259 } 260 261 public String getInitParameter(String param) { 262 return this.context.getInitParameter(param); 263 } 264 265 public Enumeration getInitParameterNames() { 266 return this.context.getInitParameterNames(); 267 } 268 269 public Object getAttribute(String param) { 270 Object result = this.context.getAttribute(param); 271 return result; 273 } 274 275 public Enumeration getAttributeNames() { 276 return this.context.getAttributeNames(); 277 } 278 279 public void setAttribute(String name, Object value) { 280 this.context.setAttribute(name, value); 281 } 282 283 public void removeAttribute(String name) { 284 this.context.removeAttribute(name); 285 } 286 287 public Set getResourcePaths(String param) { 291 return null; 292 } 293 294 public String getServletContextName() { 295 return "Cocoon context"; 296 } 297 } 298 } 299 300 | Popular Tags |