Annotation of java/classes/org/w3c/jigsaw/servlet/ServletDirectoryFrame.java, revision 1.21

1.1       bmahe       1: // ServletDirectoryFrame.java
1.21    ! ylafon      2: // $Id: ServletDirectoryFrame.java,v 1.20 2000/02/11 13:20:01 bmahe Exp $
1.1       bmahe       3: // (c) COPYRIGHT MIT and INRIA, 1996.
                      4: // Please first read the full copyright statement in file COPYRIGHT.html
                      5: 
                      6: package org.w3c.jigsaw.servlet;
                      7: 
1.21    ! ylafon      8: import java.io.File;
        !             9: import java.net.URLStreamHandlerFactory;
        !            10: import java.util.Enumeration;
        !            11: import java.util.Hashtable;
        !            12: import javax.servlet.*;
        !            13: import org.w3c.jigsaw.http.httpd;
        !            14: import org.w3c.util.ObservableProperties;
        !            15: import org.w3c.tools.resources.FramedResource;
        !            16: import org.w3c.tools.resources.InvalidResourceException;
        !            17: import org.w3c.tools.resources.Resource;
        !            18: import org.w3c.tools.resources.ResourceReference;
        !            19: import org.w3c.jigsaw.frames.HTTPFrame;
1.1       bmahe      20: 
1.18      bmahe      21: import javax.servlet.http.HttpSessionContext;
1.1       bmahe      22: 
                     23: /**
                     24:  *  @author Alexandre Rafalovitch <alex@access.com.au>
                     25:  *  @author Anselm Baird-Smith <abaird@w3.org>
1.3       bmahe      26:  *  @author Benoit Mahe <bmahe@w3.org>
1.1       bmahe      27:  */
                     28: 
1.3       bmahe      29: public class ServletDirectoryFrame extends HTTPFrame {
1.10      bmahe      30: 
                     31:     /**
1.12      bmahe      32:      * The servlet Context.
                     33:      */
1.20      bmahe      34:     protected ServletContext servletContext = null;
1.6       bmahe      35: 
1.12      bmahe      36:     /**
                     37:      * The Session Context.
                     38:      */
1.20      bmahe      39:     protected HttpSessionContext sessionContext = null;
1.10      bmahe      40: 
1.11      bmahe      41:     /**
                     42:      * Register the resource and add ServletProperties in httpd.
                     43:      * @param resource The resource to register.
                     44:      */
                     45:     public void registerResource(FramedResource resource) {
                     46:        super.registerResource(resource);
1.15      bmahe      47:        if (getServletProps() == null ) {
1.11      bmahe      48:            synchronized (this.getClass()) {
                     49:                httpd s = (httpd) getServer();
                     50:                if ( s != null ) {
1.17      bmahe      51:                    // Register the property sheet if not done yet:
1.11      bmahe      52:                    ObservableProperties props = s.getProperties() ;
1.14      bmahe      53:                    s.registerPropertySet(new ServletProps(s));
1.11      bmahe      54:                }
1.6       bmahe      55:            }
                     56:        }
                     57:     }
                     58: 
                     59:     /**
                     60:      * ServletContext implementation - Lookup a given servlet.
                     61:      */
                     62: 
                     63:     public Servlet getServlet(String name) {
                     64:        if (dresource != null) {
                     65:            ResourceReference rr = dresource.lookup(name);
1.16      bmahe      66:            if (rr != null) {
                     67:                try {
                     68:                    Resource resource = rr.lock();
                     69:                    if (resource instanceof ServletWrapper)
                     70:                        return ((ServletWrapper) resource).getServlet();
                     71:                } catch (InvalidResourceException ex) {
                     72:                    return null;
                     73:                } finally {
                     74:                    rr.unlock();
                     75:                }
1.6       bmahe      76:            }
                     77:        }
1.1       bmahe      78:        return null;
1.6       bmahe      79:     }
                     80: 
                     81:     /**
                     82:      * ServletContext implementation - Enumerate all servlets within context.
                     83:      */
                     84: 
                     85:     public Enumeration getServlets() {
                     86:        if (dresource != null)
                     87:            return new ServletEnumeration(this, 
                     88:                                  dresource.enumerateResourceIdentifiers());
                     89:        else
                     90:            return new ServletEnumeration(this, null);
                     91:     }
1.5       bmahe      92: 
                     93:     /**
                     94:      * ServletContext implementation - Enumerate all servlets names
                     95:      * within context.
                     96:      */
                     97: 
                     98:     public Enumeration getServletNames() {
                     99:        if (dresource != null)
                    100:            return new ServletNamesEnumeration(this, 
                    101:                               dresource.enumerateResourceIdentifiers());
                    102:        else
                    103:            return new ServletNamesEnumeration(this, null);
                    104:     }
1.1       bmahe     105: 
1.6       bmahe     106:     /**
                    107:      * ServletContext implementation - Get server informations.
                    108:      */
                    109: 
                    110:     public String getServerInfo() {
1.20      bmahe     111:        return getServer().getSoftware();
1.6       bmahe     112:     }
                    113: 
                    114:     /**
                    115:      * ServletContext implementation - Get an attribute value.
                    116:      * We map this into the ServletWrapper attributes, without
                    117:      * support for name clashes though.
                    118:      * @param name The attribute name.
                    119:      */
                    120: 
                    121:     public Object getAttribute(String name) {
                    122:        if ( definesAttribute(name) )
                    123:            return getValue(name, null);
                    124:        else if (resource.definesAttribute(name))
                    125:            return resource.getValue(name, null);
                    126:        return null;
                    127:     }
                    128: 
1.18      bmahe     129:     protected HttpSessionContext getHttpSessionContext() {
                    130:        if (sessionContext == null) {
                    131:            ServletProps sprops = getServletProps();
                    132:            if (sprops != null)
                    133:                 sessionContext = sprops.getSessionContext();
                    134:        }
1.19      bmahe     135:        return (HttpSessionContext)sessionContext;
1.15      bmahe     136:     }
                    137: 
                    138:     protected ServletProps getServletProps() {
                    139:        httpd server = (httpd) getServer();
                    140:        return (ServletProps)
                    141:            server.getPropertySet(ServletProps.SERVLET_PROPS_NAME);
1.11      bmahe     142:     }
                    143: 
1.18      bmahe     144:     protected ServletContext getServletContext() {
1.20      bmahe     145:        if (servletContext == null) {
                    146:            servletContext = new JigsawServletContext(getFrameReference(),
                    147:                                                 getServer().getProperties());
                    148:            File tmp = new File(getServer().getTempDirectory(), 
                    149:                       String.valueOf(resource.getURLPath().hashCode()));
                    150:            tmp.mkdirs();
                    151:            servletContext.setAttribute(JigsawServletContext.TEMPDIR_P, tmp);
                    152:        }
1.19      bmahe     153:        return (ServletContext) servletContext;
1.18      bmahe     154:     }
                    155: 
1.6       bmahe     156:     /**
                    157:      * We add a <em>context</em> attribute to all our children.
                    158:      * The <em>context</em> attribute is any object implementing the
                    159:      * ServletContext interface.
                    160:      */
                    161: 
                    162:     protected void updateDefaultChildAttributes(Hashtable attrs) {
1.18      bmahe     163:        attrs.put("servlet-context", getServletContext());
                    164:        attrs.put("session-context", getHttpSessionContext());
1.6       bmahe     165:     }
1.1       bmahe     166: 
                    167: }

Webmaster