001: // BasicCommandRegistry.java
002: // $Id: BasicCommandRegistry.java,v 1.4 2000/08/16 21:37:46 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.ssi.commands;
007:
008: import java.util.Dictionary;
009: import java.util.Hashtable;
010:
011: import java.io.PrintStream;
012:
013: import org.w3c.www.http.HTTP;
014:
015: import org.w3c.util.ArrayDictionary;
016:
017: import org.w3c.jigsaw.http.Reply;
018: import org.w3c.jigsaw.http.Request;
019:
020: import org.w3c.jigsaw.ssi.SSIFrame;
021:
022: /**
023: * An implementation of CommandRegistry that uses a hash table
024: * to store the commands.
025: * @author Antonio Ramirez <anto@mit.edu>
026: */
027: public class BasicCommandRegistry extends CommandRegistry {
028:
029: Hashtable /*<String,Command>*/commands = null;
030:
031: public BasicCommandRegistry() {
032: commands = new Hashtable(23);
033: }
034:
035: public void registerCommand(Command cmd) {
036: commands.put(cmd.getName().toLowerCase(),
037: SSIFrame.debug ? getDebugWrapperCommand(cmd) : cmd);
038: }
039:
040: protected Command getDebugWrapperCommand(Command cmd) {
041: if (cmd instanceof ControlCommand)
042: return new DebugWrapperControlCommand(cmd);
043: else
044: return new DebugWrapperCommand(cmd);
045: }
046:
047: public Command lookupCommand(String name) {
048: Command cmd = (Command) commands.get(name.toLowerCase());
049: if (cmd != null)
050: return cmd;
051: else
052: return new DefaultCommand(name);
053: }
054:
055: public Dictionary initVariables(SSIFrame ssiframe, Request request,
056: Dictionary variables) {
057: return variables;
058: }
059: }
060:
061: class DefaultCommand implements Command {
062: private String badCommand;
063:
064: /**
065: * return true if reply can be cached.
066: * @return a boolean.
067: */
068: public boolean acceptCaching() {
069: return true;
070: }
071:
072: DefaultCommand(String badCommand) {
073: this .badCommand = badCommand;
074: }
075:
076: public String getName() {
077: return null;
078: }
079:
080: public Reply execute(SSIFrame ssiframe, Request request,
081: ArrayDictionary parameters, Dictionary variables) {
082: Reply reply = ssiframe.createCommandReply(request, HTTP.OK);
083:
084: reply.setContent("[SSIFrame: unknown command \"" + badCommand
085: + "\"]");
086:
087: return reply;
088: }
089:
090: public boolean modifiedSince(long date, SSIFrame ssiframe,
091: Request request, ArrayDictionary parameters,
092: Dictionary variables) {
093: return false;
094: }
095:
096: public String getValue(Dictionary variables, String variable,
097: Request request) {
098: return "null";
099: }
100:
101: }
102:
103: class DebugWrapperCommand implements Command {
104:
105: Command cmd;
106:
107: /**
108: * return true if reply can be cached.
109: * @return a boolean.
110: */
111: public boolean acceptCaching() {
112: System.out.println("@@@@ command accept caching : "
113: + cmd.acceptCaching());
114: return cmd.acceptCaching();
115: }
116:
117: public DebugWrapperCommand(Command cmd) {
118: this .cmd = cmd;
119: System.out.println("@@@@ Added command: " + cmd.getName());
120: }
121:
122: public final Reply execute(SSIFrame ssiframe, Request request,
123: ArrayDictionary parameters, Dictionary variables) {
124: System.out.println("@@@@ Executing command: " + cmd.getName()
125: + " " + parameters);
126: return cmd.execute(ssiframe, request, parameters, variables);
127: }
128:
129: public final String getName() {
130: return cmd.getName();
131: }
132:
133: public String getValue(Dictionary variables, String variable,
134: Request request) {
135: String value = cmd.getValue(variables, variable, request);
136: System.out.println("@@@@ Get Value " + cmd.getName() + " : "
137: + value);
138: return value;
139: }
140:
141: }
142:
143: class DebugWrapperControlCommand extends DebugWrapperCommand implements
144: ControlCommand {
145: /**
146: * register the command position in the structure
147: * witch store the SSIFrame.
148: */
149: public void setPosition(SSIFrame ssiframe, Request request,
150: CommandRegistry registry, ArrayDictionary parameters,
151: Dictionary variables, int position) {
152: System.out.println("@@@@ " + cmd.getName() + " SetPosition ["
153: + position + "]");
154: ((ControlCommand) cmd).setPosition(ssiframe, request, registry,
155: parameters, variables, position);
156: }
157:
158: /**
159: * Give the next position in the structure witch
160: * store the SSIFrame.
161: * @return An integer
162: * @exception ControlCommandException if action failed.
163: */
164: public int jumpTo(SSIFrame ssiframe, Request request,
165: CommandRegistry registry, ArrayDictionary parameters,
166: Dictionary variables) throws ControlCommandException {
167: int pos = ((ControlCommand) cmd).jumpTo(ssiframe, request,
168: registry, parameters, variables);
169: System.out.println("@@@@ " + cmd.getName() + " Jump to " + pos);
170: return pos;
171: }
172:
173: public DebugWrapperControlCommand(Command cmd) {
174: super(cmd);
175: }
176: }
|