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