001: // ServletDirectoryFrame.java
002: // $Id: ServletDirectoryFrame.java,v 1.23 2001/11/12 14:02:51 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.servlet;
007:
008: import java.io.File;
009: import java.net.URLStreamHandlerFactory;
010: import java.util.Enumeration;
011: import java.util.Hashtable;
012:
013: import javax.servlet.Servlet;
014: import javax.servlet.ServletContext;
015:
016: import org.w3c.jigsaw.http.httpd;
017: import org.w3c.util.ObservableProperties;
018: import org.w3c.tools.resources.FramedResource;
019: import org.w3c.tools.resources.InvalidResourceException;
020: import org.w3c.tools.resources.Resource;
021: import org.w3c.tools.resources.ResourceReference;
022: import org.w3c.jigsaw.frames.HTTPFrame;
023:
024: import javax.servlet.http.HttpSessionContext;
025:
026: /**
027: * @author Alexandre Rafalovitch <alex@access.com.au>
028: * @author Anselm Baird-Smith <abaird@w3.org>
029: * @author Benoit Mahe <bmahe@w3.org>
030: */
031:
032: public class ServletDirectoryFrame extends HTTPFrame {
033:
034: /**
035: * The servlet Context.
036: */
037: protected ServletContext servletContext = null;
038:
039: /**
040: * The Session Context.
041: */
042: protected HttpSessionContext sessionContext = null;
043:
044: /**
045: * Register the resource and add ServletProperties in httpd.
046: * @param resource The resource to register.
047: */
048: public void registerResource(FramedResource resource) {
049: super .registerResource(resource);
050: if (getServletProps() == null) {
051: synchronized (this .getClass()) {
052: httpd s = (httpd) getServer();
053: if (s != null) {
054: // Register the property sheet if not done yet:
055: ObservableProperties props = s.getProperties();
056: s.registerPropertySet(new ServletProps(s));
057: }
058: }
059: }
060: }
061:
062: /**
063: * ServletContext implementation - Lookup a given servlet.
064: */
065: public Servlet getServlet(String name) {
066: if (dresource != null) {
067: ResourceReference rr = dresource.lookup(name);
068: if (rr != null) {
069: try {
070: Resource resource = rr.lock();
071: if (resource instanceof ServletWrapper)
072: return ((ServletWrapper) resource).getServlet();
073: } catch (InvalidResourceException ex) {
074: return null;
075: } finally {
076: rr.unlock();
077: }
078: }
079: }
080: return null;
081: }
082:
083: /**
084: * Lookup a given servlet without accessing it.
085: * @return true if and only if loading was successful
086: */
087: public boolean isServletLoaded(String name) {
088: if (dresource != null) {
089: ResourceReference rr = dresource.lookup(name);
090: if (rr != null) {
091: try {
092: Resource resource = rr.lock();
093: if (resource instanceof ServletWrapper)
094: return ((ServletWrapper) resource)
095: .isServletLoaded();
096: } catch (InvalidResourceException ex) {
097: return false;
098: } finally {
099: rr.unlock();
100: }
101: }
102: }
103: return false;
104: }
105:
106: /**
107: * ServletContext implementation - Enumerate all servlets within context.
108: */
109:
110: public Enumeration getServlets() {
111: if (dresource != null)
112: return new ServletEnumeration(this , dresource
113: .enumerateResourceIdentifiers());
114: else
115: return new ServletEnumeration(this , null);
116: }
117:
118: /**
119: * ServletContext implementation - Enumerate all servlets names
120: * within context.
121: */
122:
123: public Enumeration getServletNames() {
124: if (dresource != null)
125: return new ServletNamesEnumeration(this , dresource
126: .enumerateResourceIdentifiers());
127: else
128: return new ServletNamesEnumeration(this , null);
129: }
130:
131: /**
132: * ServletContext implementation - Get server informations.
133: */
134:
135: public String getServerInfo() {
136: return getServer().getSoftware();
137: }
138:
139: /**
140: * ServletContext implementation - Get an attribute value.
141: * We map this into the ServletWrapper attributes, without
142: * support for name clashes though.
143: * @param name The attribute name.
144: */
145:
146: public Object getAttribute(String name) {
147: if (definesAttribute(name))
148: return getValue(name, null);
149: else if (resource.definesAttribute(name))
150: return resource.getValue(name, null);
151: return null;
152: }
153:
154: protected HttpSessionContext getHttpSessionContext() {
155: if (sessionContext == null) {
156: ServletProps sprops = getServletProps();
157: if (sprops != null)
158: sessionContext = sprops.getSessionContext();
159: }
160: return (HttpSessionContext) sessionContext;
161: }
162:
163: protected ServletProps getServletProps() {
164: httpd server = (httpd) getServer();
165: return (ServletProps) server
166: .getPropertySet(ServletProps.SERVLET_PROPS_NAME);
167: }
168:
169: protected ServletContext getServletContext() {
170: if (servletContext == null) {
171: servletContext = new JigsawServletContext(
172: getFrameReference(), getServer().getProperties());
173: File tmp = new File(getServer().getTempDirectory(), String
174: .valueOf(resource.getURLPath().hashCode()));
175: tmp.mkdirs();
176: servletContext.setAttribute(JigsawServletContext.TEMPDIR_P,
177: tmp);
178: }
179: return (ServletContext) servletContext;
180: }
181:
182: /**
183: * We add a <em>context</em> attribute to all our children.
184: * The <em>context</em> attribute is any object implementing the
185: * ServletContext interface.
186: */
187:
188: protected void updateDefaultChildAttributes(Hashtable attrs) {
189: attrs.put("servlet-context", getServletContext());
190: attrs.put("session-context", getHttpSessionContext());
191: }
192:
193: }
|