Annotation of java/classes/org/w3c/jigsaw/servlet/ServletPropertiesReader.java, revision 1.3
1.2 bmahe 1: // ServletPropertiesReader.java
2: // $Id: ServletPropertiesReader.java,v 1.1.2.1 2000/01/11 16:33:15 bmahe Exp $
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.3 ! bmahe 11: import java.util.Hashtable;
! 12: import java.util.StringTokenizer;
! 13:
! 14: import javax.servlet.ServletException;
1.2 bmahe 15:
16: import org.w3c.jigsaw.http.httpd;
17: import org.w3c.jigsaw.http.httpdPreloadInterface;
18:
19: import org.w3c.util.LookupTable;
1.3 ! 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.2 bmahe 30:
31: /**
32: * @version $Revision: 1.1.2.1 $
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.3 ! 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.2 bmahe 55: protected LookupTable general = null;
56: protected LookupTable servlets = null;
57:
1.3 ! 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.2 bmahe 70: public void preload(httpd server) {
71: File dir = server.getConfigDirectory();
72: File servletProps = new File(dir, SERVLET_PROPS_FILE);
73: if (servletProps.exists()) {
74: readProperties(servletProps);
75: Enumeration enum = servlets.keys();
76: while (enum.hasMoreElements()) {
1.3 ! bmahe 77: String name = (String)enum.nextElement(); //servlet name
! 78: initializeServlet(name, server);
! 79: }
! 80: // startup servlets...
! 81: String startups = (String) general.get(STARTUP_P);
! 82: if (startups != null) {
! 83: StringTokenizer st =
! 84: new StringTokenizer(startups, ARGS_SEPARATOR);
! 85: FramedResource root = server.getRoot();
! 86: LookupState ls = null;
! 87: LookupResult lr = null;
! 88: String name = null;
! 89: String uri = null;
! 90: while (st.hasMoreTokens()) {
! 91: name = st.nextToken();
! 92: uri = SERVLET_BASE_P+"/"+name;
! 93: try {
! 94: ls = new LookupState(uri);
! 95: lr = new LookupResult(root.getResourceReference());
! 96: root.lookup(ls, lr);
! 97: ResourceReference rr = lr.getTarget();
! 98: if (rr != null) {
! 99: try {
! 100: ServletWrapper wrapper =
! 101: (ServletWrapper) rr.lock();
! 102: wrapper.launchServlet();
! 103: } catch(InvalidResourceException ex) {
! 104: ex.printStackTrace();
! 105: } catch (ClassNotFoundException cnfex) {
! 106: cnfex.printStackTrace();
! 107: } catch (ServletException sex) {
! 108: sex.printStackTrace();
! 109: } finally {
! 110: rr.unlock();
! 111: }
! 112: }
! 113: } catch (ProtocolException pex) {
! 114: pex.printStackTrace();
! 115: }
! 116: }
! 117: }
! 118: }
! 119: }
! 120:
! 121: protected void initializeServlet(String name, httpd server) {
! 122: String uri = SERVLET_BASE_P+"/"+name;
! 123: // internal lookup
! 124: FramedResource root = server.getRoot();
! 125: try {
! 126: LookupState ls = new LookupState(SERVLET_BASE_P);
! 127: LookupResult lr = new LookupResult(root.getResourceReference());
! 128: root.lookup(ls, lr);
! 129: ResourceReference servletb = lr.getTarget();
! 130: if (servletb == null) {
! 131: throw new RuntimeException("No servlet directory defined!");
1.2 bmahe 132: }
1.3 ! bmahe 133: try {
! 134: DirectoryResource parent = (DirectoryResource) servletb.lock();
! 135: ls = new LookupState(uri);
! 136: lr = new LookupResult(root.getResourceReference());
! 137: root.lookup(ls, lr);
! 138: ResourceReference target = lr.getTarget();
! 139: if (target != null) {
! 140: try {
! 141: ServletWrapper wrapper =
! 142: (ServletWrapper) target.lock();
! 143: initialize(name, wrapper, parent);
! 144: } finally {
! 145: target.unlock();
! 146: }
! 147: } else { // doesn't exists, so create it...
! 148: initialize(name, null, parent);
! 149: }
! 150: } catch (InvalidResourceException ex) {
! 151: ex.printStackTrace();
! 152: } catch (MultipleLockException mlex) {
! 153: mlex.printStackTrace();
! 154: } catch (ClassCastException ccex) {
! 155: ccex.printStackTrace();
! 156: } finally {
! 157: servletb.unlock();
! 158: }
! 159: } catch (ProtocolException ex) {
! 160: // FIXME
! 161: }
! 162: }
! 163:
! 164: /**
! 165: * Initialize a ServletWrapper.
! 166: * @param name the servlet's name
! 167: * @param wrapper the ServletWrapper (or null)
! 168: */
! 169: protected void initialize(String name,
! 170: ServletWrapper wrapper,
! 171: DirectoryResource parent)
! 172: throws InvalidResourceException, MultipleLockException
! 173: {
! 174: //initialize
! 175: LookupTable props = (LookupTable) servlets.get(name);
! 176:
! 177: String value = (String) props.get(CODEBASE_P);
! 178: if (value != null) {
! 179: if (wrapper == null) { // create a RemoteServletWrapper
! 180: wrapper = new RemoteServletWrapper();
! 181: Hashtable defs = new Hashtable();
! 182: defs.put("servlet-base", value);
! 183: parent.registerResource(name, wrapper, defs);
! 184: } else if (wrapper instanceof RemoteServletWrapper) {
! 185: int idx = RemoteServletWrapper.ATTR_SERVLET_BASE;
! 186: wrapper.setValueOfSuperClass(idx, value);
! 187: } else {
! 188: // transform a ServletWrapper in a RemoteServletWrapper
! 189: RemoteServletWrapper rwrapper = new RemoteServletWrapper();
! 190: Hashtable defs = new Hashtable();
! 191:
! 192: defs.put("servlet-base", value);
! 193:
! 194: String sclass = wrapper.getServletClass();
! 195: if (sclass != null) {
! 196: defs.put("servlet-class", wrapper.getServletClass());
! 197: }
! 198:
! 199: ArrayDictionary params = wrapper.getServletParameters();
! 200: if (params != null) {
! 201: defs.put("servlet-parameters", params);
! 202: }
! 203:
! 204: Object timeout =
! 205: wrapper.getValue(wrapper.ATTR_SERVLET_TIMEOUT, null);
! 206: if (timeout != null) {
! 207: defs.put("servlet-timeout", timeout);
! 208: }
! 209:
! 210: wrapper.delete();
! 211: parent.registerResource(name, rwrapper, defs);
! 212: }
! 213: } else if (wrapper == null){ // create a ServletWrapper
! 214: wrapper = new ServletWrapper();
! 215: parent.registerResource(name, wrapper, null);
! 216: }
! 217: value = (String) props.get(CODE_P);
! 218: if (value != null) {
! 219: wrapper.setValueOfSuperClass(wrapper.ATTR_SERVLET_CLASS,
! 220: value);
! 221: }
! 222:
! 223: value = (String) props.get(INIT_ARGS_P);
! 224: if (value != null) {
! 225: ArrayDictionary args = new ArrayDictionary();
! 226: StringTokenizer st =
! 227: new StringTokenizer(value, ARGS_SEPARATOR);
! 228: while (st.hasMoreTokens()) {
! 229: String arg = st.nextToken();
! 230: // arg=value
! 231: int idx = arg.indexOf('=');
! 232: if (idx != -1) {
! 233: value = arg.substring(idx+1);
! 234: arg = arg.substring(0, idx);
! 235: args.put(arg, value);
! 236: }
! 237: }
! 238: wrapper.setValueOfSuperClass(wrapper.ATTR_PARAMETERS, args);
! 239: }
! 240:
! 241: //
! 242: // frame attributes
! 243: //
! 244: ServletWrapperFrame frame =
! 245: (ServletWrapperFrame) wrapper.getFrame(frameclass);
! 246: value = (String) props.get(DESCRIPTION_P);
! 247: if (value != null) {
! 248: frame.setValue("title", value);
! 249: }
! 250: value = (String) props.get(ICON_P);
! 251: if (value != null) {
! 252: frame.setValue("icon", value);
1.2 bmahe 253: }
254: }
255:
256: protected void readProperties(File file) {
257: Properties props = new Properties();
258: servlets = new LookupTable();
259: general = new LookupTable();
260: try {
261: InputStream in =
262: new BufferedInputStream(new FileInputStream(file));
263: props.load(in);
264: } catch (FileNotFoundException fnfex) {
265: // nothing
266: } catch (IOException ioex) {
267: // nothing to do
268: }
269: Enumeration enum = props.propertyNames();
270: while (enum.hasMoreElements()) {
271: String property = (String) enum.nextElement();
272: if (property.startsWith("servlet.")) {
273: String value = props.getProperty(property);
274: property = property.substring(8); // remove "servlet."
275: int idx = property.indexOf('.');
276: if (idx != -1) {
277: String name = property.substring(0, idx);
278: property = property.substring(idx+1);
279: if (idx != -1) {
280: LookupTable lt = (LookupTable) servlets.get(name);
281: if (lt == null) {
282: lt = new LookupTable();
283: servlets.put(name, lt);
284: }
285: lt.put(property, value);
286: }
287: }
288: } else if (property.startsWith("servlets.")) {
289: String value = props.getProperty(property);
290: String name = property.substring(9); // remove "servlets."
291: general.put(name, value);
292: }
293: }
294: }
295:
296: }
Webmaster