1 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 27 28 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 handlerNames[]=new String [MAX_HANDLERS]; 48 49 static final int MAX_HANDLERS=32; 50 static final int RESERVED=16; int currentId=RESERVED; 52 53 public int registerMessageType( int id, String name, JkHandler h, 54 String sig[] ) 55 { 56 if( log.isDebugEnabled() ) 57 log.debug( "Register message " + id + " " + h.getName() + 58 " " + h.getClass().getName()); 59 if( id < 0 ) { 60 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 79 public int invoke(Msg msg, MsgContext ep ) 80 throws IOException 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 |