01: package pygmy.handlers;
02:
03: import pygmy.core.*;
04:
05: import java.io.IOException;
06: import java.util.StringTokenizer;
07: import java.util.List;
08: import java.util.ArrayList;
09: import java.util.Iterator;
10: import java.util.logging.Logger;
11: import java.util.logging.Level;
12:
13: /**
14: * This is the default implementation of a chain of handlers. The .chain parameter defines the names of the
15: * handlers in the chain, and it defines the order in which those handlers will be called. Each handler name is
16: * seperated by either a ' ' (space) or a ',' (comma). This handler will then try to create a handler for each of
17: * the handler names by looking at configuration property {handler-name}.class. This handler also has a .url-prefix
18: * parameter it uses to know when this handler should pass the request to the chain.
19: *
20: * <table class="inner">
21: * <tr class="header"><td>Parameter Name</td><td>Explanation</td><td>Default Value</td><td>Required</td></tr>
22: * <tr class="row"><td>url-prefix</td><td>The prefix to filter request urls.</td><td>None</td><td>Yes</td></tr>
23: * <tr class="altrow"><td>chain</td><td>A space or comma seperated list of the names of the handlers within the chain.</td><td>None</td><td>Yes</td></tr>
24: * <tr class="row"><td>class</td><td>For each of the names in the chain property, this is appended the name to find the classname to instatiate.</td><td>None</td><td>Yes</td></tr>
25: * </table>
26: */
27: public class DefaultChainHandler extends AbstractHandler implements
28: Handler {
29: private static final Logger log = Logger
30: .getLogger(DefaultChainHandler.class.getName());
31:
32: public static String CHAIN = ".chain";
33:
34: public static final ConfigOption CHAIN_OPTION = new ConfigOption(
35: "chain", true,
36: "A comma seperated list of handler names to chain together.");
37:
38: private List chain;
39:
40: public boolean initialize(String handlerName, Server server) {
41: super .initialize(handlerName, server);
42: this .chain = new ArrayList();
43: initializeChain(server);
44: return true;
45: }
46:
47: private void initializeChain(Server server) {
48: StringTokenizer tokenizer = new StringTokenizer(CHAIN_OPTION
49: .getProperty(server, handlerName), " ,");
50: while (tokenizer.hasMoreTokens()) {
51: String chainChildName = tokenizer.nextToken();
52: try {
53: Handler handler = (Handler) server
54: .constructPygmyObject(chainChildName);
55: if (handler.initialize(chainChildName, server)) {
56: chain.add(handler);
57: } else {
58: log.severe(chainChildName + " was not initialized");
59: }
60: } catch (ClassCastException e) {
61: log
62: .log(
63: Level.SEVERE,
64: chainChildName
65: + " class does not implement the Handler interface.",
66: e);
67: }
68: }
69: }
70:
71: public boolean handle(Request request, Response response)
72: throws IOException {
73: boolean hasBeenHandled = false;
74: for (Iterator i = chain.iterator(); i.hasNext()
75: && !hasBeenHandled;) {
76: Handler handler = (Handler) i.next();
77: hasBeenHandled = handler.handle(request, response);
78: }
79: return hasBeenHandled;
80: }
81:
82: public boolean shutdown(Server server) {
83: boolean success = true;
84: if (chain != null) {
85: for (Iterator i = chain.iterator(); i.hasNext();) {
86: Handler current = (Handler) i.next();
87: boolean currentSuccess = current.shutdown(server);
88: success = success && currentSuccess;
89: }
90: }
91:
92: return success;
93: }
94: }
|