Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawServletContext.java, revision 1.36
1.1 bmahe 1: // JigsawServletContext.java
1.36 ! ylafon 2: // $Id: JigsawServletContext.java,v 1.35 2012/06/16 15:48:46 ylafon Exp $
1.1 bmahe 3: // (c) COPYRIGHT MIT and INRIA, 1998.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
6: package org.w3c.jigsaw.servlet;
7:
1.30 ylafon 8: import java.io.ByteArrayOutputStream;
9: import java.io.File;
10: import java.io.IOException;
11: import java.io.InputStream;
12: import java.io.PrintWriter;
13: import java.io.RandomAccessFile;
14: import java.net.MalformedURLException;
15: import java.net.URL;
16: import java.net.URLConnection;
17: import java.util.Date;
18: import java.util.Enumeration;
19: import java.util.Hashtable;
1.33 ylafon 20: import java.util.Set;
1.30 ylafon 21: import javax.servlet.RequestDispatcher;
22: import javax.servlet.Servlet;
23: import javax.servlet.ServletContext;
1.36 ! ylafon 24: import org.w3c.jigsaw.frames.HTTPFrame;
! 25: import org.w3c.jigsaw.http.httpd;
! 26: import org.w3c.jigsaw.proxy.ForwardFrame;
! 27: import org.w3c.jigsaw.resources.VirtualHostResource;
1.30 ylafon 28: import org.w3c.tools.resources.DirectoryResource;
29: import org.w3c.tools.resources.FileResource;
30: import org.w3c.tools.resources.FramedResource;
31: import org.w3c.tools.resources.InvalidResourceException;
32: import org.w3c.tools.resources.LookupResult;
33: import org.w3c.tools.resources.LookupState;
34: import org.w3c.tools.resources.Resource;
35: import org.w3c.tools.resources.ResourceFrame;
36: import org.w3c.tools.resources.ResourceReference;
37: import org.w3c.tools.resources.ServerInterface;
1.36 ! ylafon 38: import org.w3c.tools.resources.event.StructureChangedAdapter;
! 39: import org.w3c.tools.resources.event.StructureChangedEvent;
! 40: import org.w3c.util.EmptyEnumeration;
! 41: import org.w3c.util.ObservableProperties;
! 42: import org.w3c.util.PropertyMonitoring;
1.1 bmahe 43:
44: /**
1.36 ! ylafon 45: * @author Benoît Mahé (bmahe@w3.org)
! 46: * @version $Revision: 1.35 $
1.1 bmahe 47: */
1.5 bmahe 48: public class JigsawServletContext extends StructureChangedAdapter
1.36 ! ylafon 49: implements ServletContext,
! 50: PropertyMonitoring {
1.5 bmahe 51:
1.25 bmahe 52: public static final String TEMPDIR_P = "javax.servlet.context.tempdir";
53:
1.5 bmahe 54: class Logger {
1.36 ! ylafon 55: File logfile = null;
! 56: RandomAccessFile log = null;
! 57: byte msgbuf[] = null;
! 58: boolean closed = true;
! 59:
! 60: private final String monthnames[] = {
! 61: "Jan", "Feb", "Mar", "Apr", "May", "Jun",
! 62: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
! 63: };
! 64:
! 65: String getDate() {
! 66: Date now = new Date();
! 67: return (now.getDate()
! 68: + "/" + monthnames[now.getMonth()]
! 69: + "/" + (now.getYear() + 1900)
! 70: + ((now.getHours() < 10)
! 71: ? (":0" + now.getHours())
! 72: : (":" + now.getHours()))
! 73: + ((now.getMinutes() < 10)
! 74: ? (":0" + now.getMinutes())
! 75: : (":" + now.getMinutes()))
! 76: + ((now.getSeconds() < 10)
! 77: ? (":0" + now.getSeconds())
! 78: : (":" + now.getSeconds()))
! 79: + ((now.getTimezoneOffset() < 0)
! 80: ? " " + (now.getTimezoneOffset() / 60)
! 81: : " +" + (now.getTimezoneOffset() / 60)));
! 82: }
! 83:
! 84: void log(String msg) {
! 85: msg = "[" + getDate() + "] " + msg + "\n";
! 86: try {
! 87: if (log == null || closed)
! 88: openLogFile();
! 89: if (log != null) {
! 90: int len = msg.length();
! 91: if (len > msgbuf.length)
! 92: msgbuf = new byte[len];
! 93: msg.getBytes(0, len, msgbuf, 0);
! 94: log.write(msgbuf, 0, len);
! 95: }
! 96: } catch (IOException ex) {
! 97: System.out.println("Can't write (" +
! 98: msg + ") to logfile [" +
! 99: logfile + "] : " + ex.getMessage());
! 100: }
! 101: }
! 102:
! 103: void log(Exception ex, String msg) {
! 104: log(msg + " : " + ex.getClass().getName() + " (" + ex.getMessage() + ")");
! 105: }
! 106:
! 107: void log(Throwable throwable, String msg) {
! 108: ByteArrayOutputStream out = new ByteArrayOutputStream();
! 109: PrintWriter writer = new PrintWriter(out);
! 110: throwable.printStackTrace(writer);
! 111: writer.close();
! 112: String stacktrace = out.toString();
! 113: log(msg + " " + stacktrace);
! 114: }
! 115:
! 116: void openLogFile()
! 117: throws IOException {
! 118: RandomAccessFile old = log;
! 119: log = new RandomAccessFile(logfile, "rw");
! 120: log.seek(log.length());
! 121: closed = false;
! 122: if (old != null)
! 123: old.close();
! 124: }
! 125:
! 126: void close() {
! 127: try {
! 128: if (log != null)
! 129: log.close();
! 130: closed = true;
! 131: } catch (IOException ex) {
! 132: ex.printStackTrace();
! 133: }
! 134: }
! 135:
! 136: Logger(File logfile) {
! 137: this.logfile = logfile;
! 138: this.msgbuf = new byte[128];
! 139: log("Servlet Logger started");
! 140: }
1.5 bmahe 141: }
1.9 bmahe 142:
1.1 bmahe 143: private ResourceReference reference = null;
144:
1.5 bmahe 145: private Logger logger = null;
146:
1.11 bmahe 147: private ObservableProperties props = null;
148:
1.15 bmahe 149: private File directory = null;
150:
1.36 ! ylafon 151: private Hashtable<String, Object> attributes = null;
1.20 bmahe 152:
1.36 ! ylafon 153: protected static String logdir = "logs";
1.12 bmahe 154:
155: protected static String deflogfile = "servlets";
156:
1.36 ! ylafon 157: public boolean propertyChanged(String name) {
! 158: if (name.equals(ServletProps.SERVLET_LOG_FILE_P)) {
! 159: if (logger != null) {
! 160: logger.close();
! 161: File newlogfile = new File((String)
! 162: props.get(ServletProps.SERVLET_LOG_FILE_P));
! 163: if (newlogfile.getPath().length() < 1)
! 164: newlogfile = getServletLogFile();
! 165: logger = new Logger(newlogfile);
! 166: }
! 167: }
! 168: return true;
! 169: }
! 170:
! 171: public void resourceUnloaded(StructureChangedEvent evt) {
! 172: if (logger != null) {
! 173: logger.close();
! 174: }
1.21 bmahe 175: }
176:
177: protected long getServletTimeout() {
1.36 ! ylafon 178: return props.getLong(ServletProps.SERVLET_TIMEOUT, -1);
1.5 bmahe 179: }
180:
1.32 ylafon 181: protected int getServletInstanceMax() { // added for single thread model servlet instance pool size limitation, tk, 20.10.2001
1.36 ! ylafon 182: return props.getInteger(ServletProps.SERVLET_INSTANCEMAX, 0);
1.32 ylafon 183: }
1.36 ! ylafon 184:
1.1 bmahe 185: /**
186: * A useful utility routine that tries to guess the content-type
187: * of an object based upon its extension.
188: */
189: protected static String guessContentTypeFromName(String fname) {
1.36 ! ylafon 190: return org.w3c.www.mime.Utils.guessContentTypeFromName(fname);
1.1 bmahe 191: }
192:
193: /**
194: * ServletContext implementation - Get the MIME type for given file.
195: */
196: public String getMimeType(String filename) {
1.36 ! ylafon 197: return guessContentTypeFromName(filename);
1.7 bmahe 198: }
199:
1.19 bmahe 200: public ServerInterface getServer() {
1.36 ! ylafon 201: try {
! 202: Resource res = reference.lock();
! 203: return ((ServletDirectoryFrame) res).getServer();
! 204: } catch (InvalidResourceException ex) {
! 205: ex.printStackTrace();
! 206: return null;
! 207: } finally {
! 208: reference.unlock();
! 209: }
1.1 bmahe 210: }
211:
1.11 bmahe 212: public File getServletLogFile() {
1.36 ! ylafon 213: ServerInterface server = getServer();
! 214: File logfile = null;
! 215: String file = (String)
! 216: server.getProperties().getString(ServletProps.SERVLET_LOG_FILE_P,
! 217: null);
! 218: if (file != null)
! 219: logfile = new File(file);
! 220: if ((logfile != null) && (logfile.getPath().length() < 1))
! 221: logfile = null;
! 222: if (logfile == null) {
! 223: File root_dir = server.getRootDirectory();
! 224: if (root_dir == null) {
! 225: throw new RuntimeException("unable to build a default " +
! 226: "value for the servlet log file.");
! 227: }
! 228: logfile = new File(new File(root_dir, logdir), deflogfile);
! 229: server.getProperties().putValue(ServletProps.SERVLET_LOG_FILE_P,
! 230: logfile.getAbsolutePath());
! 231: }
! 232: return logfile;
1.11 bmahe 233: }
234:
1.1 bmahe 235: /**
236: * ServletContext implementation - Lookup a given servlet.
1.36 ! ylafon 237: *
1.20 bmahe 238: * @deprecated since jsdk2.1
1.1 bmahe 239: */
240:
241: public Servlet getServlet(String name) {
1.36 ! ylafon 242: try {
! 243: Resource res = reference.lock();
! 244: return ((ServletDirectoryFrame) res).getServlet(name);
! 245: } catch (InvalidResourceException ex) {
! 246: ex.printStackTrace();
! 247: return null;
! 248: } finally {
! 249: reference.unlock();
! 250: }
1.1 bmahe 251: }
252:
253: /**
254: * ServletContext implementation - Enumerate all servlets within context.
1.36 ! ylafon 255: *
1.20 bmahe 256: * @deprecated since jsdk2.1
1.1 bmahe 257: */
258:
259: public Enumeration getServlets() {
1.36 ! ylafon 260: try {
! 261: Resource res = reference.lock();
! 262: return ((ServletDirectoryFrame) res).getServlets();
! 263: } catch (InvalidResourceException ex) {
! 264: ex.printStackTrace();
! 265: return null;
! 266: } finally {
! 267: reference.unlock();
! 268: }
1.1 bmahe 269: }
1.4 bmahe 270:
271: /**
1.36 ! ylafon 272: * ServletContext implementation - Enumerate all servlets names
1.4 bmahe 273: * within context.
1.36 ! ylafon 274: *
1.20 bmahe 275: * @deprecated since jsdk2.1
1.4 bmahe 276: */
1.1 bmahe 277: public Enumeration getServletNames() {
1.36 ! ylafon 278: try {
! 279: Resource res = reference.lock();
! 280: return ((ServletDirectoryFrame) res).getServletNames();
! 281: } catch (InvalidResourceException ex) {
! 282: ex.printStackTrace();
! 283: return null;
! 284: } finally {
! 285: reference.unlock();
! 286: }
1.1 bmahe 287: }
288:
289: /**
290: * ServletContext implementation - Log a message.
291: */
292: public void log(String msg) {
1.36 ! ylafon 293: logger.log(msg);
1.1 bmahe 294: }
1.30 ylafon 295:
1.20 bmahe 296: /**
297: * @deprecated since jsdk2.1
298: */
1.1 bmahe 299: public void log(Exception ex, String msg) {
1.36 ! ylafon 300: logger.log(ex, msg);
1.1 bmahe 301: }
302:
1.20 bmahe 303: public void log(String message, Throwable throwable) {
1.36 ! ylafon 304: logger.log(throwable, message);
1.20 bmahe 305: }
306:
1.1 bmahe 307: /**
308: * ServletContext implementation - Translate a piece of path.
1.36 ! ylafon 309: *
! 310: * @param path the virtual path to translate
1.27 bmahe 311: * @param rr_root the Root ResourceReference
1.36 ! ylafon 312: * @param rr the target ResourceReference
1.27 bmahe 313: * @return the real path
314: */
1.36 ! ylafon 315: protected static String getRealPath(String path,
! 316: ResourceReference rr_root,
! 317: ResourceReference rr) {
! 318: ResourceReference local_root = getLocalRoot(rr_root, rr);
! 319: try {
! 320: FramedResource root = (FramedResource) local_root.lock();
! 321: LookupState ls = new LookupState(path);
! 322: LookupResult lr = new LookupResult(local_root);
! 323: if (root.lookup(ls, lr)) {
! 324: ResourceReference target = lr.getTarget();
! 325: if (target != null) {
! 326: try {
! 327: FramedResource res = (FramedResource) target.lock();
! 328: if (res instanceof FileResource) {
! 329: File file = ((FileResource) res).getFile();
! 330: return file.getAbsolutePath();
! 331: } else if (res instanceof DirectoryResource) {
! 332: DirectoryResource dir = (DirectoryResource) res;
! 333: return dir.getDirectory().getAbsolutePath();
! 334: //return getFilePath(dir);
! 335: }
! 336: } finally {
! 337: target.unlock();
! 338: }
! 339: }
! 340: }
! 341: return null;
! 342: } catch (InvalidResourceException ex) {
! 343: return null;
! 344: } catch (org.w3c.tools.resources.ProtocolException pex) {
! 345: return null;
! 346: } finally {
! 347: local_root.unlock();
! 348: }
1.33 ylafon 349: }
1.36 ! ylafon 350:
1.33 ylafon 351: /**
352: * from Servlet 2.3, very file-oriented
353: * FIXME always returning null as of now
354: */
355: public Set getResourcePaths(String path) {
1.36 ! ylafon 356: return null;
1.33 ylafon 357: }
1.36 ! ylafon 358:
1.33 ylafon 359: /**
360: * from Servlet 2.3, very file-oriented should return a web-app container
361: * name
362: * FIXME always returning null as of now
363: */
364: public String getServletContextName() {
1.36 ! ylafon 365: return null;
1.27 bmahe 366: }
367:
368: /**
369: * ServletContext implementation - Translate a piece of path.
1.36 ! ylafon 370: *
1.27 bmahe 371: * @param path the virtual path to translate
372: * @return the real path
1.1 bmahe 373: */
1.27 bmahe 374: public String getRealPath(String path) {
1.36 ! ylafon 375: ResourceReference rr_root = ((httpd) getServer()).getRootReference();
! 376: return getRealPath(path, rr_root, reference);
1.27 bmahe 377: }
1.1 bmahe 378:
1.27 bmahe 379: protected static String getFilePath(DirectoryResource dir) {
1.36 ! ylafon 380: HTTPFrame frame =
! 381: (HTTPFrame) dir.getFrame("org.w3c.jigsaw.frames.HTTPFrame");
! 382: String indexes[] = frame.getIndexes();
! 383: if (indexes != null) {
! 384: for (String index : indexes) {
! 385: if (index != null && index.length() > 0) {
! 386: ResourceReference rr = dir.lookup(index);
! 387: if (rr != null) {
! 388: try {
! 389: FramedResource ri = (FramedResource) rr.lock();
! 390: if (ri instanceof FileResource) {
! 391: FileResource fr = (FileResource) ri;
! 392: File file = fr.getFile();
! 393: return file.getAbsolutePath();
! 394: } else {
! 395: // we don't know
! 396: return null;
! 397: }
! 398: } catch (InvalidResourceException ex) {
! 399: } finally {
! 400: rr.unlock();
! 401: }
! 402: }
! 403: }
! 404: }
! 405: return dir.getDirectory().getAbsolutePath();
! 406: } else {
! 407: return dir.getDirectory().getAbsolutePath();
! 408: }
! 409: }
! 410:
! 411: protected static ResourceReference getLocalRoot(ResourceReference rr_root,
! 412: ResourceReference ref) {
! 413: try {
! 414: FramedResource root = (FramedResource) rr_root.lock();
! 415: if (root instanceof VirtualHostResource) {
! 416: //backward to the virtual host resource
! 417: ResourceReference rr = null;
! 418: ResourceReference rrp = null;
! 419: FramedResource res = null;
! 420: try {
! 421: res = (FramedResource) ref.lock();
! 422: if (res instanceof ResourceFrame) {
! 423: ResourceFrame fr = (ResourceFrame) res;
! 424: rr = fr.getResource().getResourceReference();
! 425: } else {
! 426: rr = ref;
! 427: }
! 428: } catch (InvalidResourceException ex) {
! 429: return rr_root;
! 430: } finally {
! 431: ref.unlock();
! 432: }
! 433:
! 434: while (true) {
1.35 ylafon 435: try {
1.36 ! ylafon 436: res = (FramedResource) rr.lock();
! 437: rrp = res.getParent();
! 438: if ((rrp == rr_root) || (rrp == null)) {
! 439: return getLocalRoot(rr, ref);
1.35 ylafon 440: }
441: } catch (InvalidResourceException ex) {
1.36 ! ylafon 442: return rr_root;
1.35 ylafon 443: } finally {
444: rr.unlock();
445: }
1.36 ! ylafon 446: rr = rrp;
! 447: }
! 448: } else {
! 449: try {
! 450: FramedResource res = (FramedResource) rr_root.lock();
! 451: ForwardFrame ffr = (ForwardFrame)
! 452: res.getFrame("org.w3c.jigsaw.proxy.ForwardFrame");
! 453: if (ffr == null) {
! 454: return rr_root;
! 455: } else {
! 456: ResourceReference rr = ffr.getLocalRootResource();
! 457: return getLocalRoot(rr, ref);
! 458: }
! 459: } catch (InvalidResourceException ex) {
! 460: return rr_root;
1.35 ylafon 461: }
462: }
1.36 ! ylafon 463: } catch (InvalidResourceException ex) {
! 464: return rr_root;
! 465: } finally {
! 466: rr_root.unlock();
1.35 ylafon 467: }
1.1 bmahe 468: }
469:
470: /**
471: * ServletContext implementation - Get server informations.
472: */
473:
474: public String getServerInfo() {
1.36 ! ylafon 475: try {
! 476: Resource res = reference.lock();
! 477: return ((ServletDirectoryFrame) res).getServerInfo();
! 478: } catch (InvalidResourceException ex) {
! 479: ex.printStackTrace();
! 480: return null;
! 481: } finally {
! 482: reference.unlock();
! 483: }
1.1 bmahe 484: }
485:
486: /**
487: * ServletContext implementation - Get an attribute value.
488: * We map this into the ServletWrapper attributes, without
489: * support for name clashes though.
1.36 ! ylafon 490: *
1.1 bmahe 491: * @param name The attribute name.
492: */
1.30 ylafon 493:
1.1 bmahe 494: public Object getAttribute(String name) {
1.36 ! ylafon 495: Object attribute = attributes.get(name);
! 496: if (attribute != null) {
! 497: return attribute;
! 498: } else {
! 499: try {
! 500: Resource res = reference.lock();
! 501: return ((ServletDirectoryFrame) res).getAttribute(name);
! 502: } catch (InvalidResourceException ex) {
! 503: ex.printStackTrace();
! 504: return null;
! 505: } finally {
! 506: reference.unlock();
! 507: }
! 508: }
1.1 bmahe 509: }
510:
1.20 bmahe 511: public void setAttribute(String name, Object object) {
1.36 ! ylafon 512: attributes.put(name, object);
1.20 bmahe 513: }
514:
515: public void removeAttribute(String name) {
1.36 ! ylafon 516: attributes.remove(name);
1.20 bmahe 517: }
518:
519: public Enumeration getAttributeNames() {
1.36 ! ylafon 520: return attributes.keys();
1.20 bmahe 521: }
522:
1.25 bmahe 523: /**
524: * Returns a <code>String</code> containing the value of the named
1.36 ! ylafon 525: * context-wide initialization parameter, or <code>null</code> if the
1.25 bmahe 526: * parameter does not exist.
1.36 ! ylafon 527: * <p/>
1.25 bmahe 528: * <p>This method can make available configuration information useful
1.36 ! ylafon 529: * to an entire "web application". For example, it can provide a
! 530: * webmaster's email address or the name of a system that holds
1.25 bmahe 531: * critical data.
532: *
533: * @param name a <code>String</code> containing the name of the
1.36 ! ylafon 534: * parameter whose value is requested
! 535: * @return a <code>String</code> containing at least the
! 536: * servlet container name and version number
1.25 bmahe 537: */
538: public String getInitParameter(String name) {
1.36 ! ylafon 539: // @@ not implemented @@
! 540: return null;
1.25 bmahe 541: }
1.30 ylafon 542:
1.25 bmahe 543:
544: /**
545: * Returns the names of the context's initialization parameters as an
546: * <code>Enumeration</code> of <code>String</code> objects, or an
547: * empty <code>Enumeration</code> if the context has no initialization
548: * parameters.
549: *
1.36 ! ylafon 550: * @return an <code>Enumeration</code> of <code>String</code>
! 551: * objects containing the names of the context's initialization parameters
1.25 bmahe 552: */
553: public Enumeration getInitParameterNames() {
1.36 ! ylafon 554: // @@ not implemented @@
! 555: return new EmptyEnumeration();
1.25 bmahe 556: }
557:
1.15 bmahe 558: private AutoReloadServletLoader loader = null;
559:
1.36 ! ylafon 560: /**
! 561: * Get or create a suitable LocalServletLoader instance to load
1.15 bmahe 562: * that servlet.
1.36 ! ylafon 563: *
1.15 bmahe 564: * @return A LocalServletLoader instance.
565: */
566: protected synchronized AutoReloadServletLoader getLocalServletLoader() {
1.36 ! ylafon 567: if (loader == null) {
! 568: loader = new AutoReloadServletLoader(this);
! 569: }
! 570: return loader;
1.15 bmahe 571: }
572:
1.36 ! ylafon 573: protected synchronized AutoReloadServletLoader createNewLocalServletLoader(boolean keepold) {
! 574: if ((loader != null) && keepold)
! 575: loader = new AutoReloadServletLoader(loader);
! 576: else
! 577: loader = new AutoReloadServletLoader(this);
! 578: return loader;
1.15 bmahe 579: }
580:
581: public File getServletDirectory() {
1.36 ! ylafon 582: return directory;
1.15 bmahe 583: }
584:
1.20 bmahe 585: //jsdk2.1
586:
587: /**
1.36 ! ylafon 588: * Returns a RequestDispatcher object for the specified URL path if
1.20 bmahe 589: * the context knows of an active source (such as a servlet, JSP page,
590: * CGI script, etc) of content for the particular path. This format of
1.36 ! ylafon 591: * the URL path must be of the form /dir/dir/file.ext. The servlet
! 592: * engine is responsible for implementing whatever functionality is
1.20 bmahe 593: * required to wrap the target source with an implementation of
1.36 ! ylafon 594: * the RequestDispatcher interface.
! 595: *
1.20 bmahe 596: * @param urlpath Path to use to look up the target server resource
597: */
598: public RequestDispatcher getRequestDispatcher(String urlpath) {
1.36 ! ylafon 599: return JigsawRequestDispatcher.getRequestDispatcher(urlpath,
! 600: (httpd) getServer(),
! 601: reference);
1.25 bmahe 602: }
603:
604: /**
605: * Returns a {@link RequestDispatcher} object that acts
606: * as a wrapper for the named servlet.
1.36 ! ylafon 607: * <p/>
! 608: * <p>Servlets (and JSP pages also) may be given names via server
1.25 bmahe 609: * administration or via a web application deployment descriptor.
1.36 ! ylafon 610: * A servlet instance can determine its name using
! 611: * <p/>
! 612: * <p>This method returns <code>null</code> if the
1.25 bmahe 613: * <code>ServletContext</code>
614: * cannot return a <code>RequestDispatcher</code> for any reason.
615: *
616: * @param name a <code>String</code> specifying the name
1.36 ! ylafon 617: * of a servlet to wrap
1.25 bmahe 618: * @return a <code>RequestDispatcher</code> object
1.36 ! ylafon 619: * that acts as a wrapper for the named servlet
1.25 bmahe 620: * @see RequestDispatcher
621: * @see ServletContext#getContext
622: */
623: public RequestDispatcher getNamedDispatcher(String name) {
1.36 ! ylafon 624: if (name == null) {
! 625: throw new IllegalArgumentException("null");
! 626: }
! 627: return JigsawRequestDispatcher.getRequestDispatcher(name,
! 628: reference,
! 629: (httpd) getServer());
1.20 bmahe 630: }
631:
632: public int getMajorVersion() {
1.36 ! ylafon 633: return 2;
1.20 bmahe 634: }
635:
636: public int getMinorVersion() {
1.36 ! ylafon 637: return 2;
1.20 bmahe 638: }
1.30 ylafon 639:
1.20 bmahe 640: public ServletContext getContext(String uripath) {
1.36 ! ylafon 641: if (uripath == null)
! 642: return null;
! 643: //first, find the ServletDirectoryFrame.
! 644: // Prepare for lookup:
! 645: ResourceReference rr_root = null;
! 646: rr_root = ((httpd) getServer()).getRootReference();
! 647:
! 648: FramedResource root = null;
! 649: root = ((httpd) getServer()).getRoot();
! 650:
! 651: // Do the lookup:
! 652: ResourceReference r_target = null;
! 653: try {
! 654: LookupState ls = new LookupState(uripath);
! 655: LookupResult lr = new LookupResult(rr_root);
! 656: root.lookup(ls, lr);
! 657: r_target = lr.getTarget();
! 658: } catch (Exception ex) {
! 659: r_target = null;
! 660: }
! 661: //then return its context
! 662: if (r_target != null) {
! 663: try {
! 664: Resource target = r_target.lock();
! 665: if (target instanceof FramedResource) {
! 666: ServletDirectoryFrame frame = (ServletDirectoryFrame)
! 667: ((FramedResource) target).
! 668: getFrame("org.w3c.jigsaw.servlet.ServletDirectoryFrame");
! 669: if (frame != null)
! 670: return frame.getServletContext();
! 671: }
! 672: } catch (InvalidResourceException ex) {
! 673: // continue
! 674: } finally {
! 675: r_target.unlock();
! 676: }
! 677: }
! 678: return null;
! 679: }
! 680:
! 681: public URL getResource(String path)
! 682: throws MalformedURLException {
! 683: // FIXME? is it allowed?
! 684: File file = new File(path);
! 685: if (file.exists()) {
! 686: return new URL("file", "", file.getAbsolutePath());
! 687: }
! 688: String realpath = getRealPath(path);
! 689: if (realpath != null) {
! 690: file = new File(realpath);
! 691: if (file.exists()) {
! 692: return new URL("file", "", file.getAbsolutePath());
! 693: } else {
! 694: return null;
! 695: }
! 696: } else {
! 697: // it could be a virtual resource
! 698: // check that it exists (on server)
! 699: // FIXME (Virtual host)
! 700: return null;
! 701: }
1.24 bmahe 702: }
1.26 bmahe 703:
1.20 bmahe 704: public InputStream getResourceAsStream(String path) {
1.36 ! ylafon 705: try {
! 706: URL resource = getResource(path);
! 707: if (resource == null)
! 708: return null;
! 709: try {
! 710: URLConnection c = resource.openConnection();
! 711: return c.getInputStream();
! 712: } catch (IOException ex) {
! 713: return null;
! 714: }
! 715: } catch (MalformedURLException ex) {
! 716: return null;
! 717: }
1.20 bmahe 718: }
719:
1.1 bmahe 720: /**
721: * Create a new ServletContext.
1.36 ! ylafon 722: *
1.1 bmahe 723: * @param ref a ResourceReference pointing on a ServletDirectoryFrame.
724: */
1.36 ! ylafon 725: protected JigsawServletContext(ResourceReference ref,
! 726: ObservableProperties props) {
! 727: this.reference = ref;
! 728: this.props = props;
! 729: this.attributes = new Hashtable<String, Object>(3);
! 730: this.logger = new Logger(getServletLogFile());
! 731: this.loader = new AutoReloadServletLoader(this);
! 732:
! 733: props.registerObserver(this);
! 734:
! 735: try {
! 736: Resource res = reference.lock();
! 737: if (!(res instanceof ServletDirectoryFrame)) {
! 738: throw new IllegalArgumentException("This reference is not " +
! 739: "pointing on a ServletDirectoryFrame.");
! 740: } else {
! 741: ServletDirectoryFrame sframe = (ServletDirectoryFrame) res;
! 742: FramedResource resource = (FramedResource) sframe.getResource();
! 743: resource.addStructureChangedListener(this);
! 744: if (resource.definesAttribute("directory"))
! 745: this.directory =
! 746: (File) resource.getValue("directory", null);
! 747: }
! 748: } catch (InvalidResourceException ex) {
! 749: throw new IllegalArgumentException("This reference is pointing on" +
! 750: " an Invalid ServletDirectoryFrame.");
! 751: } finally {
! 752: reference.unlock();
! 753: }
1.1 bmahe 754: }
755:
756: }
Webmaster