Annotation of java/classes/org/w3c/jigsaw/servlet/RemoteServletWrapper.java, revision 1.14

1.5       ylafon      1: // RemoteServletWrapper.java
1.14    ! ylafon      2: // $Id: RemoteServletWrapper.java,v 1.13 2001/11/12 14:02:05 ylafon Exp $
1.5       ylafon      3: // (c) COPYRIGHT MIT and INRIA, 1997.
                      4: // Please first read the full copyright statement in file COPYRIGHT.html
                      5: 
1.6       bmahe       6: package org.w3c.jigsaw.servlet;
1.1       abaird      7: 
1.11      ylafon      8: import javax.servlet.ServletException;
                      9: import org.w3c.tools.resources.Attribute;
                     10: import org.w3c.tools.resources.AttributeRegistry;
                     11: import org.w3c.tools.resources.StringAttribute;
1.1       abaird     12: 
1.2       abaird     13: /**
1.14    ! ylafon     14:  * @author Alexandre Rafalovitch <alex@access.com.au>
        !            15:  * @author Anselm Baird-Smith <abaird@w3.org>
1.2       abaird     16:  */
                     17: 
1.1       abaird     18: public class RemoteServletWrapper extends ServletWrapper {
1.8       bmahe      19:     private static final boolean debug = false;
1.1       abaird     20: 
                     21:     /**
                     22:      * Attribute index - The servlet content base.
                     23:      */
                     24:     protected static int ATTR_SERVLET_BASE = -1;
                     25: 
                     26:     static {
1.14    ! ylafon     27:         Class c = null;
        !            28:         Attribute a = null;
        !            29:         try {
        !            30:             c = Class.forName("org.w3c.jigsaw.servlet.RemoteServletWrapper");
        !            31:         } catch (Exception ex) {
        !            32:             ex.printStackTrace();
        !            33:             System.exit(1);
        !            34:         }
        !            35:         // Register the servlet base URL:
        !            36:         a = new StringAttribute("servlet-base"
        !            37:                 , null
        !            38:                 , Attribute.EDITABLE | Attribute.MANDATORY);
        !            39:         ATTR_SERVLET_BASE = AttributeRegistry.registerAttribute(c, a);
1.1       abaird     40:     }
1.14    ! ylafon     41: 
1.1       abaird     42:     /**
                     43:      * The ServletLoader instance for loading that servlet.
                     44:      */
                     45:     protected ServletLoader loader = null;
                     46: 
1.10      bmahe      47:     /**
                     48:      * Check the servlet class, ans try to initialize it.
1.14    ! ylafon     49:      *
        !            50:      * @throws ClassNotFoundException if servlet class can't be found.
        !            51:      * @throws ServletException       if servlet can't be initialized.
        !            52:      */
        !            53:     protected void checkServlet()
        !            54:             throws ClassNotFoundException, ServletException {
        !            55:         synchronized (servletPool) {
1.13      ylafon     56:             // synchronization for pool access added, tk, 21.10.2001
1.14    ! ylafon     57:             if (!inited) {
        !            58:                 inited = launchServlet();
1.12      ylafon     59:             }
                     60:         }
1.7       bmahe      61:     }
                     62: 
1.14    ! ylafon     63:     /**
1.1       abaird     64:      * Get or create a suitable ServletLoader instance to load that servlet.
1.14    ! ylafon     65:      *
1.1       abaird     66:      * @return A ServletLoader instance.
                     67:      */
                     68: 
                     69:     protected synchronized ServletLoader getServletLoader() {
1.14    ! ylafon     70:         if (loader == null) {
        !            71:             loader = new ServletLoader(this);
        !            72:         }
        !            73:         return loader;
1.1       abaird     74:     }
                     75: 
                     76:     /**
                     77:      * Get the remote servlet URL base.
1.14    ! ylafon     78:      *
1.1       abaird     79:      * @return The String encoded base URL for that servlet, or <strong>null
1.14    ! ylafon     80:      *         </strong> if undefined.
1.1       abaird     81:      */
                     82: 
                     83:     public String getServletBase() {
1.14    ! ylafon     84:         return getString(ATTR_SERVLET_BASE, null);
1.1       abaird     85:     }
1.11      ylafon     86: 
1.1       abaird     87:     public void setValue(int idx, Object value) {
1.14    ! ylafon     88:         super.setValueOfSuperClass(idx, value);
        !            89:         try {
        !            90:             // synchronization for pool access added, tk, 21.10.2001
        !            91:             synchronized (servletPool) {
        !            92:                 if ((idx == ATTR_SERVLET_CLASS) && (value != null)) {
        !            93:                     inited = launchServlet();
        !            94:                 }
        !            95:                 if ((idx == ATTR_SERVLET_BASE) && (value != null)) {
        !            96:                     inited = launchServlet();
        !            97:                 }
1.12      ylafon     98:             }
1.14    ! ylafon     99:         } catch (Exception ex) {
        !           100:             String msg = ("unable to set servlet class \"" +
        !           101:                     getServletClass() +
        !           102:                     "\" : " +
        !           103:                     ex.getMessage());
        !           104:             getServer().errlog(msg);
        !           105:         }
1.1       abaird    106:     }
                    107: 
1.10      bmahe     108:     /**
                    109:      * Initialize the servlet.
1.14    ! ylafon    110:      *
        !           111:      * @throws ClassNotFoundException if servlet class can't be found.
        !           112:      * @throws ServletException       if servlet can't be initialized.
        !           113:      */
        !           114:     protected boolean launchServlet()
        !           115:             throws ClassNotFoundException, ServletException {
        !           116:         if (debug) {
        !           117:             System.out.println("Launching servlet: " + getServletClass());
        !           118:         }
        !           119:         // Get and check the servlet class:
        !           120:         // if ( servlet != null )
        !           121:         destroyServlet();
        !           122:         if (inited) {
        !           123:             String msg = "relaunching servlet failed due to incomplete \""
        !           124:                     + getServletClass() + "\" cleanup.";
        !           125:             getServer().errlog(this, msg);
        !           126:             return false;
1.13      ylafon    127:         } else {
1.14    ! ylafon    128:             // Load appropriate servlet class:
        !           129:             Class c = null;
        !           130:             try {
        !           131:                 // Load the servlet class through the loader:
        !           132:                 c = getServletLoader().loadClass(getServletClass(), true);
        !           133:             } catch (ClassFormatError er) {
        !           134:                 String msg = ("class \"" + getServletClass() + "\" loaded from "
        !           135:                         + getServletBase() + ", invalid format.");
        !           136:                 if (debug) {
        !           137:                     er.printStackTrace();
        !           138:                 }
        !           139:                 getServer().errlog(this, msg);
        !           140:             } catch (ClassNotFoundException ex) {
        !           141:                 String msg = ("class \"" + getServletClass() + "\" loaded from "
        !           142:                         + getServletBase() + ", not found.");
        !           143:                 if (debug) {
        !           144:                     ex.printStackTrace();
        !           145:                 }
        !           146:                 getServer().errlog(this, msg);
        !           147:             }
        !           148:             return (c != null) ? launchServlet(c) : false;
1.13      ylafon    149:         }
1.1       abaird    150:     }
                    151: }

Webmaster