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

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

Webmaster