KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jk > common > HandlerDispatch


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
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
27
28 /**
29  * Dispatch based on the message type. ( XXX make it more generic,
30  * now it's specific to ajp13 ).
31  *
32  * @author Costin Manolache
33  */

34 public class HandlerDispatch extends JkHandler
35 {
36     private static org.apache.commons.logging.Log log=
37         org.apache.commons.logging.LogFactory.getLog( HandlerDispatch.class );
38
39     public HandlerDispatch()
40     {
41     }
42
43     public void init() {
44     }
45
46     JkHandler handlers[]=new JkHandler[MAX_HANDLERS];
47     String JavaDoc handlerNames[]=new String JavaDoc[MAX_HANDLERS];
48     
49     static final int MAX_HANDLERS=32;
50     static final int RESERVED=16; // reserved names, backward compat
51
int currentId=RESERVED;
52
53     public int registerMessageType( int id, String JavaDoc name, JkHandler h,
54                                     String JavaDoc sig[] )
55     {
56         if( log.isDebugEnabled() )
57             log.debug( "Register message " + id + " " + h.getName() +
58                  " " + h.getClass().getName());
59     if( id < 0 ) {
60         // try to find it by name
61
for( int i=0; i< handlerNames.length; i++ ) {
62                 if( handlerNames[i]==null ) continue;
63                 if( name.equals( handlerNames[i] ) )
64                     return i;
65             }
66         handlers[currentId]=h;
67             handlerNames[currentId]=name;
68         currentId++;
69         return currentId;
70     }
71     handlers[id]=h;
72         handlerNames[currentId]=name;
73     return id;
74     }
75
76     
77     // -------------------- Incoming message --------------------
78

79     public int invoke(Msg msg, MsgContext ep )
80         throws IOException JavaDoc
81     {
82         int type=msg.peekByte();
83         ep.setType( type );
84         
85         if( type > handlers.length ||
86             handlers[type]==null ) {
87         if( log.isDebugEnabled() )
88                 log.debug( "Invalid handler " + type );
89         return ERROR;
90     }
91
92         if( log.isDebugEnabled() )
93             log.debug( "Received " + type + " " + handlers[type].getName());
94         
95     JkHandler handler=handlers[type];
96         
97         return handler.invoke( msg, ep );
98     }
99
100  }
101
Popular Tags