Annotation of java/classes/org/w3c/jigsaw/servlet/ServletPropertiesReader.java, revision 1.1.2.6

1.1.2.1   bmahe       1: // ServletPropertiesReader.java
1.1.2.6 ! bmahe       2: // $Id: ServletPropertiesReader.java,v 1.7 2000/06/22 13:52:58 bmahe Exp $
1.1.2.1   bmahe       3: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
                      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.util.Properties;
                     10: import java.util.Enumeration;
1.1.2.2   bmahe      11: import java.util.Hashtable;
                     12: import java.util.StringTokenizer;
                     13: 
                     14: import javax.servlet.ServletException;
1.1.2.1   bmahe      15: 
                     16: import org.w3c.jigsaw.http.httpd;
                     17: import org.w3c.jigsaw.http.httpdPreloadInterface;
                     18: 
                     19: import org.w3c.util.LookupTable;
1.1.2.2   bmahe      20: import org.w3c.util.ArrayDictionary;
                     21: 
                     22: import org.w3c.tools.resources.DirectoryResource;
                     23: import org.w3c.tools.resources.InvalidResourceException;
                     24: import org.w3c.tools.resources.MultipleLockException;
                     25: import org.w3c.tools.resources.ProtocolException;
                     26: import org.w3c.tools.resources.FramedResource;
                     27: import org.w3c.tools.resources.ResourceReference;
                     28: import org.w3c.tools.resources.LookupState;
                     29: import org.w3c.tools.resources.LookupResult;
1.1.2.1   bmahe      30: 
                     31: /**
1.1.2.6 ! bmahe      32:  * @version $Revision: 1.7 $
1.1.2.1   bmahe      33:  * @author  Benoît Mahé (bmahe@w3.org)
                     34:  */
                     35: public class ServletPropertiesReader implements httpdPreloadInterface {
                     36:     
                     37:     public static final String SERVLET_PROPS_FILE = "servlets.properties";
                     38: 
1.1.2.2   bmahe      39:     // servlet base
                     40:     public static final String SERVLET_BASE_P = "/servlet";
                     41: 
                     42:     // servlet properties
                     43:     public static final String ALLOW_DELETE_P = "allow_delete";
                     44:     public static final String CODE_P         = "code";
                     45:     public static final String INIT_ARGS_P    = "initArgs";
                     46:     public static final String DESCRIPTION_P  = "description";
                     47:     public static final String CODEBASE_P     = "codebase";
                     48:     public static final String ICON_P         = "icon";
                     49: 
                     50:     public static final String ARGS_SEPARATOR = " \n";
                     51: 
                     52:     // general properties
                     53:     public static final String STARTUP_P      = "startup";
                     54: 
1.1.2.1   bmahe      55:     protected LookupTable general  = null;
                     56:     protected LookupTable servlets = null;
                     57: 
1.1.2.2   bmahe      58:     protected static Class frameclass = null;
                     59: 
                     60:     static {
                     61:        try {
                     62:            frameclass = 
                     63:                Class.forName("org.w3c.jigsaw.servlet.ServletWrapperFrame") ;
                     64:        } catch (Exception ex) {
                     65:            ex.printStackTrace();
                     66:            System.exit(0);
                     67:        }
                     68:     }
                     69: 
1.1.2.3   bmahe      70:     /**
                     71:      * Load the servlets configuration from servlets.properties.
                     72:      * @param server the http server to configure
                     73:      */
1.1.2.1   bmahe      74:     public void preload(httpd server) {
                     75:        File dir = server.getConfigDirectory();
                     76:        File servletProps = new File(dir, SERVLET_PROPS_FILE);
                     77:        if (servletProps.exists()) {
                     78:            readProperties(servletProps);
1.1.2.4   bmahe      79:            Enumeration       enum = servlets.keys();
                     80:            ResourceReference sdir = getServletDirectoryReference(server);
                     81:            if (sdir == null) {
                     82:                throw new RuntimeException("No servlet directory defined!");
                     83:            }
1.1.2.1   bmahe      84:            while (enum.hasMoreElements()) {
1.1.2.2   bmahe      85:                String name = (String)enum.nextElement(); //servlet name
1.1.2.4   bmahe      86:                initializeServlet(name, server, sdir);
1.1.2.1   bmahe      87:            }
1.1.2.2   bmahe      88:            // startup servlets...
                     89:            String startups = (String) general.get(STARTUP_P);
                     90:            if (startups != null) {
                     91:                StringTokenizer st = 
                     92:                    new StringTokenizer(startups, ARGS_SEPARATOR);
                     93:                FramedResource root = server.getRoot();
                     94:                LookupState    ls   = null;
                     95:                LookupResult   lr   = null;
                     96:                String         name = null;
                     97:                String         uri  = null;
                     98:                while (st.hasMoreTokens()) {
                     99:                    name = st.nextToken();
                    100:                    uri  = SERVLET_BASE_P+"/"+name;
                    101:                    try {
                    102:                        ls   = new LookupState(uri);
                    103:                        lr   = new LookupResult(root.getResourceReference());
                    104:                        root.lookup(ls, lr);
                    105:                        ResourceReference rr = lr.getTarget();
                    106:                        if (rr != null) {
                    107:                            try {
                    108:                                ServletWrapper wrapper = 
                    109:                                    (ServletWrapper) rr.lock();
1.1.2.5   bmahe     110:                                wrapper.checkServlet();
1.1.2.2   bmahe     111:                            } catch(InvalidResourceException ex) {
                    112:                                ex.printStackTrace();
                    113:                            } catch (ClassNotFoundException cnfex) {
                    114:                                cnfex.printStackTrace();
                    115:                            } catch (ServletException sex) {
                    116:                                sex.printStackTrace();
                    117:                            } finally {
                    118:                                rr.unlock();
                    119:                            }
                    120:                        }
                    121:                    } catch (ProtocolException pex) {
                    122:                        pex.printStackTrace();
                    123:                    }
                    124:                }
                    125:            }
                    126:        }
                    127:     }
                    128: 
1.1.2.3   bmahe     129:     /**
1.1.2.4   bmahe     130:      * Get the servlet directory reference.
                    131:      * @return a ResourceReference
                    132:      */
                    133:     protected ResourceReference getServletDirectoryReference(httpd server) {
1.1.2.6 ! bmahe     134:        ResourceReference rr = server.getEditRoot(); 
1.1.2.4   bmahe     135:        try {
1.1.2.6 ! bmahe     136:            FramedResource root = (FramedResource) rr.lock();
        !           137:            try {
        !           138:                LookupState  ls = new LookupState(SERVLET_BASE_P);
        !           139:                LookupResult lr = 
        !           140:                    new LookupResult(root.getResourceReference());
        !           141:                root.lookup(ls, lr);
        !           142:                return lr.getTarget();
        !           143:            } catch (ProtocolException ex) {
        !           144:                ex.printStackTrace();
        !           145:                return null;
        !           146:            }
        !           147:        } catch (InvalidResourceException ex) {
1.1.2.4   bmahe     148:            return null;
1.1.2.6 ! bmahe     149:        } finally {
        !           150:            rr.unlock();
1.1.2.4   bmahe     151:        }
                    152:     }
                    153: 
                    154:     /**
1.1.2.3   bmahe     155:      * Initialize a servlet or create it if not found
                    156:      * @param name the servlet's name
                    157:      * @param server the http server
                    158:      */
1.1.2.4   bmahe     159:     protected void initializeServlet(String name, 
                    160:                                     httpd server, 
                    161:                                     ResourceReference sdir) 
                    162:     {
1.1.2.2   bmahe     163:        String uri  = SERVLET_BASE_P+"/"+name;
                    164:        // internal lookup
1.1.2.6 ! bmahe     165:        ResourceReference rr = server.getEditRoot();
1.1.2.2   bmahe     166:        try {
1.1.2.6 ! bmahe     167:            FramedResource root = (FramedResource) rr.lock();
        !           168:            try {
        !           169:                DirectoryResource parent = (DirectoryResource) sdir.lock();
        !           170:                LookupState  ls = new LookupState(uri);
        !           171:                LookupResult lr = 
        !           172:                    new LookupResult(root.getResourceReference());
        !           173:                root.lookup(ls, lr);
        !           174:                ResourceReference target = lr.getTarget();
        !           175:                if (target != null) {
        !           176:                    try {
        !           177:                        ServletWrapper wrapper = 
        !           178:                            (ServletWrapper) target.lock();
        !           179:                        initialize(name, wrapper, parent);
        !           180:                    } finally {
        !           181:                        target.unlock();
        !           182:                    }
        !           183:                } else { // doesn't exists, so create it...
        !           184:                    initialize(name, null, parent);
1.1.2.2   bmahe     185:                }
1.1.2.6 ! bmahe     186:            } catch (ProtocolException pex) {
        !           187:                pex.printStackTrace();
        !           188:            } catch (InvalidResourceException ex) {
        !           189:                ex.printStackTrace();
        !           190:            } catch (MultipleLockException mlex) {
        !           191:                mlex.printStackTrace();
        !           192:            } catch (ClassCastException ccex) {
        !           193:                ccex.printStackTrace();
        !           194:            } finally {
        !           195:                sdir.unlock();
1.1.2.2   bmahe     196:            }
1.1.2.4   bmahe     197:        } catch (InvalidResourceException ex) {
1.1.2.6 ! bmahe     198:            // nothing to do :(
1.1.2.4   bmahe     199:        } finally {
1.1.2.6 ! bmahe     200:            rr.unlock();
1.1.2.2   bmahe     201:        }
                    202:     }
                    203: 
                    204:     /**
                    205:      * Initialize a ServletWrapper.
                    206:      * @param name the servlet's name
                    207:      * @param wrapper the ServletWrapper (or null)
                    208:      */
                    209:     protected void initialize(String name, 
                    210:                              ServletWrapper wrapper,
                    211:                              DirectoryResource parent)
                    212:        throws InvalidResourceException, MultipleLockException
                    213:     {
                    214:        //initialize
                    215:        LookupTable props = (LookupTable) servlets.get(name);
                    216: 
                    217:        String value = (String) props.get(CODEBASE_P);
                    218:        if (value != null) { 
                    219:            if (wrapper == null) { // create a RemoteServletWrapper
                    220:                wrapper = new RemoteServletWrapper();
                    221:                Hashtable defs = new Hashtable();
                    222:                defs.put("servlet-base", value);
                    223:                parent.registerResource(name, wrapper, defs);
                    224:            } else if (wrapper instanceof RemoteServletWrapper) {
                    225:                int idx = RemoteServletWrapper.ATTR_SERVLET_BASE;
1.1.2.4   bmahe     226:                wrapper.setSilentValue(idx, value);
1.1.2.2   bmahe     227:            } else { 
                    228:                // transform a ServletWrapper in a RemoteServletWrapper
                    229:                RemoteServletWrapper rwrapper = new RemoteServletWrapper();
                    230:                Hashtable defs = new Hashtable();
                    231: 
                    232:                defs.put("servlet-base", value);
                    233: 
                    234:                String sclass = wrapper.getServletClass();
                    235:                if (sclass != null) {
                    236:                    defs.put("servlet-class", wrapper.getServletClass());
                    237:                }
                    238: 
                    239:                ArrayDictionary params = wrapper.getServletParameters();
                    240:                if (params != null) {
                    241:                    defs.put("servlet-parameters", params);
                    242:                }
                    243: 
                    244:                Object timeout = 
                    245:                    wrapper.getValue(wrapper.ATTR_SERVLET_TIMEOUT, null);
                    246:                if (timeout != null) {
                    247:                    defs.put("servlet-timeout", timeout);
                    248:                }
                    249: 
                    250:                wrapper.delete();
                    251:                parent.registerResource(name, rwrapper, defs);
                    252:            }
                    253:        } else if (wrapper == null){ // create a ServletWrapper
                    254:            wrapper = new ServletWrapper();
                    255:            parent.registerResource(name, wrapper, null);
                    256:        }
                    257:        value = (String) props.get(CODE_P);
                    258:        if (value != null) {
1.1.2.4   bmahe     259:            wrapper.setSilentValue(wrapper.ATTR_SERVLET_CLASS, 
                    260:                                   value);
1.1.2.2   bmahe     261:        }
                    262:            
                    263:        value = (String) props.get(INIT_ARGS_P);
                    264:        if (value != null) {
                    265:            ArrayDictionary args = new ArrayDictionary();
                    266:            StringTokenizer st   = 
                    267:                new StringTokenizer(value, ARGS_SEPARATOR);
                    268:            while (st.hasMoreTokens()) {
                    269:                String arg = st.nextToken();
                    270:                // arg=value
                    271:                int idx = arg.indexOf('=');
                    272:                if (idx != -1) {
                    273:                    value = arg.substring(idx+1);
                    274:                    arg   = arg.substring(0, idx);
                    275:                    args.put(arg, value);
                    276:                }
                    277:            }
1.1.2.4   bmahe     278:            wrapper.setSilentValue(wrapper.ATTR_PARAMETERS, args);
1.1.2.2   bmahe     279:        }
                    280: 
                    281:        //
                    282:        // frame attributes
                    283:        // 
                    284:        ServletWrapperFrame frame = 
                    285:            (ServletWrapperFrame) wrapper.getFrame(frameclass);
                    286:        value = (String) props.get(DESCRIPTION_P);
                    287:        if (value != null) {
1.1.2.4   bmahe     288:            frame.setSilentValue("title", value);
1.1.2.2   bmahe     289:        }
                    290:        value = (String) props.get(ICON_P);
                    291:        if (value != null) {
1.1.2.4   bmahe     292:            frame.setSilentValue("icon", value);
1.1.2.1   bmahe     293:        }
                    294:     }
                    295: 
1.1.2.3   bmahe     296:     /**
                    297:      * Read the servlets.properties file
                    298:      * @param file the servlets.properties file.
                    299:      */
1.1.2.1   bmahe     300:     protected void readProperties(File file) {
                    301:        Properties props = new Properties();
                    302:        servlets = new LookupTable();
                    303:        general  = new LookupTable();
                    304:        try {
                    305:            InputStream in = 
                    306:                new BufferedInputStream(new FileInputStream(file));
                    307:            props.load(in);
1.1.2.4   bmahe     308:            in.close();
1.1.2.1   bmahe     309:        } catch (FileNotFoundException fnfex) {
                    310:            // nothing
                    311:        } catch (IOException ioex) {
                    312:            // nothing to do
                    313:        }
                    314:        Enumeration enum  = props.propertyNames();
                    315:        while (enum.hasMoreElements()) {
                    316:            String property = (String) enum.nextElement();
                    317:            if (property.startsWith("servlet.")) {
                    318:                String value = props.getProperty(property);
                    319:                property     = property.substring(8); // remove "servlet."
                    320:                int idx      = property.indexOf('.');
                    321:                if (idx != -1) {
                    322:                    String name = property.substring(0, idx);
                    323:                    property    = property.substring(idx+1);
                    324:                    if (idx != -1) {
                    325:                        LookupTable lt = (LookupTable) servlets.get(name);
                    326:                        if (lt == null) {
                    327:                            lt = new LookupTable();
                    328:                            servlets.put(name, lt);
                    329:                        }
                    330:                        lt.put(property, value);
                    331:                    }
                    332:                }
                    333:            } else if (property.startsWith("servlets.")) {
                    334:                String value = props.getProperty(property);
                    335:                String name  = property.substring(9); // remove "servlets."
                    336:                general.put(name, value);
                    337:            }
                    338:        }
                    339:     }
                    340: 
                    341: }

Webmaster