1 17 18 package org.apache.jasper.servlet; 19 20 21 import java.io.File ; 22 import java.io.InputStream ; 23 import java.io.PrintWriter ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.Enumeration ; 27 import java.util.HashSet ; 28 import java.util.Hashtable ; 29 import java.util.Set ; 30 import java.util.Vector ; 31 32 import javax.servlet.RequestDispatcher ; 33 import javax.servlet.Servlet ; 34 import javax.servlet.ServletContext ; 35 import javax.servlet.ServletException ; 36 37 38 44 45 public class JspCServletContext implements ServletContext { 46 47 48 50 51 54 protected Hashtable myAttributes; 55 56 57 60 protected PrintWriter myLogWriter; 61 62 63 66 protected URL myResourceBaseURL; 67 68 69 71 72 78 public JspCServletContext(PrintWriter aLogWriter, URL aResourceBaseURL) { 79 80 myAttributes = new Hashtable (); 81 myLogWriter = aLogWriter; 82 myResourceBaseURL = aResourceBaseURL; 83 84 } 85 86 87 89 90 95 public Object getAttribute(String name) { 96 97 return (myAttributes.get(name)); 98 99 } 100 101 102 105 public Enumeration getAttributeNames() { 106 107 return (myAttributes.keys()); 108 109 } 110 111 112 117 public ServletContext getContext(String uripath) { 118 119 return (null); 120 121 } 122 123 124 127 public String getContextPath() { 128 129 return (null); 130 131 } 132 133 134 139 public String getInitParameter(String name) { 140 141 return (null); 142 143 } 144 145 146 150 public Enumeration getInitParameterNames() { 151 152 return (new Vector ().elements()); 153 154 } 155 156 157 160 public int getMajorVersion() { 161 162 return (2); 163 164 } 165 166 167 172 public String getMimeType(String file) { 173 174 return (null); 175 176 } 177 178 179 182 public int getMinorVersion() { 183 184 return (3); 185 186 } 187 188 189 194 public RequestDispatcher getNamedDispatcher(String name) { 195 196 return (null); 197 198 } 199 200 201 207 public String getRealPath(String path) { 208 209 if (!myResourceBaseURL.getProtocol().equals("file")) 210 return (null); 211 if (!path.startsWith("/")) 212 return (null); 213 try { 214 return 215 (getResource(path).getFile().replace('/', File.separatorChar)); 216 } catch (Throwable t) { 217 return (null); 218 } 219 220 } 221 222 223 228 public RequestDispatcher getRequestDispatcher(String path) { 229 230 return (null); 231 232 } 233 234 235 244 public URL getResource(String path) throws MalformedURLException { 245 246 if (!path.startsWith("/")) 247 throw new MalformedURLException ("Path '" + path + 248 "' does not start with '/'"); 249 URL url = new URL (myResourceBaseURL, path.substring(1)); 250 InputStream is = null; 251 try { 252 is = url.openStream(); 253 } catch (Throwable t) { 254 url = null; 255 } finally { 256 if (is != null) { 257 try { 258 is.close(); 259 } catch (Throwable t2) { 260 } 262 } 263 } 264 return url; 265 266 } 267 268 269 275 public InputStream getResourceAsStream(String path) { 276 277 try { 278 return (getResource(path).openStream()); 279 } catch (Throwable t) { 280 return (null); 281 } 282 283 } 284 285 286 292 public Set getResourcePaths(String path) { 293 294 Set thePaths = new HashSet (); 295 if (!path.endsWith("/")) 296 path += "/"; 297 String basePath = getRealPath(path); 298 if (basePath == null) 299 return (thePaths); 300 File theBaseDir = new File (basePath); 301 if (!theBaseDir.exists() || !theBaseDir.isDirectory()) 302 return (thePaths); 303 String theFiles[] = theBaseDir.list(); 304 for (int i = 0; i < theFiles.length; i++) { 305 File testFile = new File (basePath + File.separator + theFiles[i]); 306 if (testFile.isFile()) 307 thePaths.add(path + theFiles[i]); 308 else if (testFile.isDirectory()) 309 thePaths.add(path + theFiles[i] + "/"); 310 } 311 return (thePaths); 312 313 } 314 315 316 319 public String getServerInfo() { 320 321 return ("JspCServletContext/1.0"); 322 323 } 324 325 326 333 public Servlet getServlet(String name) throws ServletException { 334 335 return (null); 336 337 } 338 339 340 343 public String getServletContextName() { 344 345 return (getServerInfo()); 346 347 } 348 349 350 355 public Enumeration getServletNames() { 356 357 return (new Vector ().elements()); 358 359 } 360 361 362 367 public Enumeration getServlets() { 368 369 return (new Vector ().elements()); 370 371 } 372 373 374 379 public void log(String message) { 380 381 myLogWriter.println(message); 382 383 } 384 385 386 394 public void log(Exception exception, String message) { 395 396 log(message, exception); 397 398 } 399 400 401 407 public void log(String message, Throwable exception) { 408 409 myLogWriter.println(message); 410 exception.printStackTrace(myLogWriter); 411 412 } 413 414 415 420 public void removeAttribute(String name) { 421 422 myAttributes.remove(name); 423 424 } 425 426 427 433 public void setAttribute(String name, Object value) { 434 435 myAttributes.put(name, value); 436 437 } 438 439 440 441 } 442 | Popular Tags |