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