001: /*
002: * DeferredHandler.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): suhler.
022: *
023: * Version: 1.3
024: * Created by suhler on 00/05/12
025: * Last modified by suhler on 00/12/11 13:28:51
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import java.util.StringTokenizer;
031: import java.io.IOException;
032: import sunlabs.brazil.server.Handler;
033: import sunlabs.brazil.server.Request;
034: import sunlabs.brazil.server.Server;
035:
036: /**
037: * Wrap another handler, deferring its initialization until request time.
038: * This allows handlers to be configured, but not initialized until
039: * a first use is attempted.
040: *<p>
041: * Normally, when a handler's class is first resolved, if any of the
042: * dependent classes are not available, an error will occur, terminating the
043: * server. Using this handler, other handlers can be conditionally configured
044: * based on the availability of other specified classes at run time.
045: * <p>
046: * NOTE: This functionallity should be integrated into the <code>
047: * ChainHandler</code>, eliminating the need for this one.
048: * <p>
049: * Request Properties
050: * <dl class=props>
051: *<dt>handler<dd>The token representing the handler to conditionally configure.
052: * This is used as the handler's prefix
053: *<dt>requires<dd>The names of classes required to be resolvable before
054: * configuring the handler
055: *<dt>[handler].prefix<dd>Used to trigger the configuration
056: *<dt>[handler].class<dd>The name of the handler class.
057: *</dl>
058: */
059:
060: public class DeferredHandler implements Handler {
061: String prefix; // our prefix
062: String token; // our handlers token
063: Server server; // the server to init the handler with
064: String trigger; // prefix to trigger initialization
065: Handler handler; // the handler to start
066: boolean initialized = false;// we tried to initialize this
067: boolean installed = false; // already initialized
068:
069: /**
070: * Remember the server for deferred initialization.
071: */
072:
073: public boolean init(Server server, String prefix) {
074: this .server = server;
075: this .prefix = prefix;
076: handler = null;
077: token = server.props.getProperty(prefix + "handler");
078: return (token != null);
079: }
080:
081: /**
082: * Dispatch to the handler, installing it if needed
083: */
084:
085: public boolean respond(Request request) throws IOException {
086: if (installed) {
087: return handler.respond(request);
088: } else if (!initialized) {
089: String urlPrefix = request.props.getProperty(token
090: + ".prefix", "/");
091: if (!request.url.startsWith(urlPrefix)) {
092: return false;
093: }
094: initialized = true;
095: request.log(Server.LOG_DIAGNOSTIC, prefix,
096: "Attempting to install handler");
097:
098: /* make sure we can find all required classes */
099:
100: StringTokenizer st = new StringTokenizer(request.props
101: .getProperty(prefix + "requires", ""));
102: while (st.hasMoreTokens()) {
103: String require = null;
104: try {
105: require = st.nextToken();
106: request.log(Server.LOG_DIAGNOSTIC, prefix,
107: "locating " + require);
108: Class.forName(require);
109: } catch (Exception e) {
110: request.log(Server.LOG_WARNING, prefix,
111: "Can't locate " + require);
112: request.log(Server.LOG_DIAGNOSTIC, prefix, e
113: .toString());
114: return false;
115: }
116: }
117:
118: /* Now init (and run) the handler */
119:
120: try {
121: String name = request.props.getProperty(token
122: + ".class");
123: request.log(Server.LOG_DIAGNOSTIC, prefix,
124: "Instantiating " + name);
125: handler = (Handler) Class.forName(name).newInstance();
126: if (handler.init(server, token + ".")) {
127: installed = true;
128: request.log(Server.LOG_DIAGNOSTIC, prefix,
129: "Handler " + name + " installed");
130: return respond(request);
131: } else {
132: request.log(Server.LOG_DIAGNOSTIC, prefix,
133: "Handler " + name + " NOT installed");
134: }
135: } catch (Exception e) {
136: request.log(Server.LOG_WARNING, prefix, e.toString());
137: }
138: return false;
139: } else { // didn't install
140: return false;
141: }
142: }
143: }
|