01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.jk.common;
18:
19: import java.io.IOException;
20:
21: import org.apache.jk.core.JkHandler;
22: import org.apache.jk.core.Msg;
23: import org.apache.jk.core.MsgContext;
24:
25: /**
26: * Dispatch based on the message type. ( XXX make it more generic,
27: * now it's specific to ajp13 ).
28: *
29: * @author Costin Manolache
30: */
31: public class HandlerDispatch extends JkHandler {
32: private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
33: .getLog(HandlerDispatch.class);
34:
35: public HandlerDispatch() {
36: }
37:
38: public void init() {
39: }
40:
41: JkHandler handlers[] = new JkHandler[MAX_HANDLERS];
42: String handlerNames[] = new String[MAX_HANDLERS];
43:
44: static final int MAX_HANDLERS = 32;
45: static final int RESERVED = 16; // reserved names, backward compat
46: int currentId = RESERVED;
47:
48: public int registerMessageType(int id, String name, JkHandler h,
49: String sig[]) {
50: if (log.isDebugEnabled())
51: log.debug("Register message " + id + " " + h.getName()
52: + " " + h.getClass().getName());
53: if (id < 0) {
54: // try to find it by name
55: for (int i = 0; i < handlerNames.length; i++) {
56: if (handlerNames[i] == null)
57: continue;
58: if (name.equals(handlerNames[i]))
59: return i;
60: }
61: handlers[currentId] = h;
62: handlerNames[currentId] = name;
63: currentId++;
64: return currentId;
65: }
66: handlers[id] = h;
67: handlerNames[currentId] = name;
68: return id;
69: }
70:
71: // -------------------- Incoming message --------------------
72:
73: public int invoke(Msg msg, MsgContext ep) throws IOException {
74: int type = msg.peekByte();
75: ep.setType(type);
76:
77: if (type > handlers.length || handlers[type] == null) {
78: if (log.isDebugEnabled())
79: log.debug("Invalid handler " + type);
80: return ERROR;
81: }
82:
83: if (log.isDebugEnabled())
84: log.debug("Received " + type + " "
85: + handlers[type].getName());
86:
87: JkHandler handler = handlers[type];
88:
89: return handler.invoke(msg, ep);
90: }
91:
92: }
|