001: // ServletPropertiesReader.java
002: // $Id: ServletPropertiesReader.java,v 1.10 2007/02/11 10:57:01 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.servlet;
007:
008: import java.io.BufferedInputStream;
009: import java.io.File;
010: import java.io.FileInputStream;
011: import java.io.FileNotFoundException;
012: import java.io.IOException;
013: import java.io.InputStream;
014:
015: import java.util.Properties;
016: import java.util.Enumeration;
017: import java.util.Hashtable;
018: import java.util.StringTokenizer;
019:
020: import javax.servlet.ServletException;
021:
022: import org.w3c.jigsaw.http.httpd;
023: import org.w3c.jigsaw.http.httpdPreloadInterface;
024:
025: import org.w3c.util.LookupTable;
026: import org.w3c.util.ArrayDictionary;
027:
028: import org.w3c.tools.resources.DirectoryResource;
029: import org.w3c.tools.resources.InvalidResourceException;
030: import org.w3c.tools.resources.MultipleLockException;
031: import org.w3c.tools.resources.ProtocolException;
032: import org.w3c.tools.resources.FramedResource;
033: import org.w3c.tools.resources.ResourceReference;
034: import org.w3c.tools.resources.LookupState;
035: import org.w3c.tools.resources.LookupResult;
036:
037: /**
038: * @version $Revision: 1.10 $
039: * @author Benoît Mahé (bmahe@w3.org)
040: */
041: public class ServletPropertiesReader implements httpdPreloadInterface {
042:
043: public static final String SERVLET_PROPS_FILE = "servlets.properties";
044:
045: // servlet base
046: public static final String SERVLET_BASE_P = "/servlet";
047:
048: // servlet properties
049: public static final String ALLOW_DELETE_P = "allow_delete";
050: public static final String CODE_P = "code";
051: public static final String INIT_ARGS_P = "initArgs";
052: public static final String DESCRIPTION_P = "description";
053: public static final String CODEBASE_P = "codebase";
054: public static final String ICON_P = "icon";
055:
056: public static final String ARGS_SEPARATOR = ",";
057:
058: // general properties
059: public static final String STARTUP_P = "startup";
060:
061: protected LookupTable general = null;
062: protected LookupTable servlets = null;
063:
064: protected static Class frameclass = null;
065:
066: static {
067: try {
068: frameclass = Class
069: .forName("org.w3c.jigsaw.servlet.ServletWrapperFrame");
070: } catch (Exception ex) {
071: ex.printStackTrace();
072: System.exit(0);
073: }
074: }
075:
076: /**
077: * Load the servlets configuration from servlets.properties.
078: * @param server the http server to configure
079: */
080: public void preload(httpd server) {
081: File dir = server.getConfigDirectory();
082: File servletProps = new File(dir, SERVLET_PROPS_FILE);
083: if (servletProps.exists()) {
084: readProperties(servletProps);
085: Enumeration e = servlets.keys();
086: ResourceReference sdir = getServletDirectoryReference(server);
087: if (sdir == null) {
088: throw new RuntimeException(
089: "No servlet directory defined!");
090: }
091: while (e.hasMoreElements()) {
092: String name = (String) e.nextElement(); //servlet name
093: initializeServlet(name, server, sdir);
094: }
095: // startup servlets...
096: String startups = (String) general.get(STARTUP_P);
097: if (startups != null) {
098: StringTokenizer st = new StringTokenizer(startups,
099: ARGS_SEPARATOR);
100: FramedResource root = server.getRoot();
101: LookupState ls = null;
102: LookupResult lr = null;
103: String name = null;
104: String uri = null;
105: while (st.hasMoreTokens()) {
106: name = st.nextToken();
107: uri = SERVLET_BASE_P + "/" + name;
108: try {
109: ls = new LookupState(uri);
110: lr = new LookupResult(root
111: .getResourceReference());
112: root.lookup(ls, lr);
113: ResourceReference rr = lr.getTarget();
114: if (rr != null) {
115: try {
116: ServletWrapper wrapper = (ServletWrapper) rr
117: .lock();
118: wrapper.checkServlet();
119: } catch (InvalidResourceException ex) {
120: ex.printStackTrace();
121: } catch (ClassNotFoundException cnfex) {
122: cnfex.printStackTrace();
123: } catch (ServletException sex) {
124: sex.printStackTrace();
125: } finally {
126: rr.unlock();
127: }
128: }
129: } catch (ProtocolException pex) {
130: pex.printStackTrace();
131: }
132: }
133: }
134: }
135: }
136:
137: /**
138: * Get the servlet directory reference.
139: * @return a ResourceReference
140: */
141: protected ResourceReference getServletDirectoryReference(
142: httpd server) {
143: ResourceReference rr = server.getEditRoot();
144: try {
145: FramedResource root = (FramedResource) rr.lock();
146: try {
147: LookupState ls = new LookupState(SERVLET_BASE_P);
148: LookupResult lr = new LookupResult(root
149: .getResourceReference());
150: root.lookup(ls, lr);
151: return lr.getTarget();
152: } catch (ProtocolException ex) {
153: ex.printStackTrace();
154: return null;
155: }
156: } catch (InvalidResourceException ex) {
157: return null;
158: } finally {
159: rr.unlock();
160: }
161: }
162:
163: /**
164: * Initialize a servlet or create it if not found
165: * @param name the servlet's name
166: * @param server the http server
167: */
168: protected void initializeServlet(String name, httpd server,
169: ResourceReference sdir) {
170: String uri = SERVLET_BASE_P + "/" + name;
171: // internal lookup
172: ResourceReference rr = server.getEditRoot();
173: try {
174: FramedResource root = (FramedResource) rr.lock();
175: try {
176: DirectoryResource parent = (DirectoryResource) sdir
177: .lock();
178: LookupState ls = new LookupState(uri);
179: LookupResult lr = new LookupResult(root
180: .getResourceReference());
181: root.lookup(ls, lr);
182: ResourceReference target = lr.getTarget();
183: if (target != null) {
184: try {
185: ServletWrapper wrapper = (ServletWrapper) target
186: .lock();
187: initialize(name, wrapper, parent);
188: } finally {
189: target.unlock();
190: }
191: } else { // doesn't exists, so create it...
192: initialize(name, null, parent);
193: }
194: } catch (ProtocolException pex) {
195: pex.printStackTrace();
196: } catch (InvalidResourceException ex) {
197: ex.printStackTrace();
198: } catch (MultipleLockException mlex) {
199: mlex.printStackTrace();
200: } catch (ClassCastException ccex) {
201: ccex.printStackTrace();
202: } finally {
203: sdir.unlock();
204: }
205: } catch (InvalidResourceException ex) {
206: // nothing to do :(
207: } finally {
208: rr.unlock();
209: }
210: }
211:
212: /**
213: * Initialize a ServletWrapper.
214: * @param name the servlet's name
215: * @param wrapper the ServletWrapper (or null)
216: */
217: protected void initialize(String name, ServletWrapper wrapper,
218: DirectoryResource parent) throws InvalidResourceException,
219: MultipleLockException {
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;
232: wrapper.setSilentValue(idx, value);
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
243: .put("servlet-class", wrapper
244: .getServletClass());
245: }
246:
247: ArrayDictionary params = wrapper.getServletParameters();
248: if (params != null) {
249: defs.put("servlet-parameters", params);
250: }
251:
252: Object timeout = wrapper.getValue(
253: wrapper.ATTR_SERVLET_TIMEOUT, null);
254: if (timeout != null) {
255: defs.put("servlet-timeout", timeout);
256: }
257:
258: wrapper.delete();
259: parent.registerResource(name, rwrapper, defs);
260: }
261: } else if (wrapper == null) { // create a ServletWrapper
262: wrapper = new ServletWrapper();
263: parent.registerResource(name, wrapper, null);
264: }
265: value = (String) props.get(CODE_P);
266: if (value != null) {
267: wrapper.setSilentValue(wrapper.ATTR_SERVLET_CLASS, value);
268: }
269:
270: value = (String) props.get(INIT_ARGS_P);
271: if (value != null) {
272: ArrayDictionary args = new ArrayDictionary();
273: StringTokenizer st = new StringTokenizer(value,
274: ARGS_SEPARATOR);
275: while (st.hasMoreTokens()) {
276: String arg = st.nextToken();
277: // arg=value
278: int idx = arg.indexOf('=');
279: if (idx != -1) {
280: value = arg.substring(idx + 1);
281: arg = arg.substring(0, idx);
282: args.put(arg, value);
283: }
284: }
285: wrapper.setSilentValue(wrapper.ATTR_PARAMETERS, args);
286: }
287:
288: //
289: // frame attributes
290: //
291: ServletWrapperFrame frame = (ServletWrapperFrame) wrapper
292: .getFrame(frameclass);
293: value = (String) props.get(DESCRIPTION_P);
294: if (value != null) {
295: frame.setSilentValue("title", value);
296: }
297: value = (String) props.get(ICON_P);
298: if (value != null) {
299: frame.setSilentValue("icon", value);
300: }
301: }
302:
303: /**
304: * Read the servlets.properties file
305: * @param file the servlets.properties file.
306: */
307: protected void readProperties(File file) {
308: Properties props = new Properties();
309: servlets = new LookupTable();
310: general = new LookupTable();
311: try {
312: InputStream in = new BufferedInputStream(
313: new FileInputStream(file));
314: props.load(in);
315: in.close();
316: } catch (FileNotFoundException fnfex) {
317: // nothing
318: } catch (IOException ioex) {
319: // nothing to do
320: }
321: Enumeration e = props.propertyNames();
322: while (e.hasMoreElements()) {
323: String property = (String) e.nextElement();
324: if (property.startsWith("servlet.")) {
325: String value = props.getProperty(property);
326: property = property.substring(8); // remove "servlet."
327: int idx = property.indexOf('.');
328: if (idx != -1) {
329: String name = property.substring(0, idx);
330: property = property.substring(idx + 1);
331: if (idx != -1) {
332: LookupTable lt = (LookupTable) servlets
333: .get(name);
334: if (lt == null) {
335: lt = new LookupTable();
336: servlets.put(name, lt);
337: }
338: lt.put(property, value);
339: }
340: }
341: } else if (property.startsWith("servlets.")) {
342: String value = props.getProperty(property);
343: String name = property.substring(9); // remove "servlets."
344: general.put(name, value);
345: }
346: }
347: }
348:
349: }
|