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

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

Webmaster