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 import org.apache.jk.core.JkChannel; 25 26 import org.apache.coyote.Request; 27 31 public class ChannelJni extends JniHandler implements JkChannel { 32 int receivedNote=1; 33 34 public ChannelJni() { 35 } 38 39 public void init() throws IOException { 40 super.initNative("channel.jni:jni"); 41 42 if( apr==null ) return; 43 44 apr.addJkHandler( "channelJni", this ); 46 log.info("JK: listening on channel.jni:jni" ); 47 48 if( next==null ) { 49 if( nextName!=null ) 50 setNext( wEnv.getHandler( nextName ) ); 51 if( next==null ) 52 next=wEnv.getHandler( "dispatch" ); 53 if( next==null ) 54 next=wEnv.getHandler( "request" ); 55 if( log.isDebugEnabled() ) 56 log.debug("Setting default next " + next.getClass().getName()); 57 } 58 } 59 60 63 public int receive( Msg msg, MsgContext ep ) 64 throws IOException 65 { 66 Msg sentResponse=(Msg)ep.getNote( receivedNote ); 67 ep.setNote( receivedNote, null ); 68 69 if( sentResponse == null ) { 70 if( log.isDebugEnabled() ) 71 log.debug("No send() prior to receive(), no data buffer"); 72 msg.reset(); 74 msg.end(); 75 sentResponse = msg; 76 } 77 78 sentResponse.processHeader(); 79 80 if( log.isTraceEnabled() ) 81 sentResponse.dump("received response "); 82 83 if( msg != sentResponse ) { 84 log.error( "Error, in JNI mode the msg used for receive() must be identical with the one used for send()"); 85 } 86 87 return 0; 88 } 89 90 94 public int send( Msg msg, MsgContext ep ) 95 throws IOException 96 { 97 ep.setNote( receivedNote, null ); 98 if( log.isDebugEnabled() ) log.debug("ChannelJni.send: " + msg ); 99 100 int rc=super.nativeDispatch( msg, ep, JK_HANDLE_JNI_DISPATCH, 0); 101 102 ep.setNote( receivedNote, msg ); 106 107 return rc; 108 } 109 110 public int flush(Msg msg, MsgContext ep) throws IOException { 111 ep.setNote( receivedNote, null ); 112 return OK; 113 } 114 115 public boolean isSameAddress(MsgContext ep) { 116 return true; 117 } 118 119 public void registerRequest(Request req, MsgContext ep, int count) { 120 } 122 123 public String getChannelName() { 124 return getName(); 125 } 126 145 public int invoke(Msg msg, MsgContext ep ) throws IOException { 146 if( apr==null ) return -1; 147 148 long xEnv=ep.getJniEnv(); 149 long cEndpointP=ep.getJniContext(); 150 151 int type=ep.getType(); 152 if( log.isDebugEnabled() ) log.debug("ChannelJni.invoke: " + ep + " " + type); 153 154 switch( type ) { 155 case JkHandler.HANDLE_RECEIVE_PACKET: 156 return receive( msg, ep ); 157 case JkHandler.HANDLE_SEND_PACKET: 158 return send( msg, ep ); 159 case JkHandler.HANDLE_FLUSH: 160 return flush(msg, ep); 161 } 162 163 ep.setNote( receivedNote, null ); 165 166 try { 168 if( log.isDebugEnabled() ) log.debug("Received request " + xEnv); 171 172 msg.processHeader(); 174 175 if( log.isTraceEnabled() ) msg.dump("Incoming msg "); 176 177 int status= next.invoke( msg, ep ); 178 179 if( log.isDebugEnabled() ) log.debug("after processCallbacks " + status); 180 181 return status; 182 } catch( Exception ex ) { 183 ex.printStackTrace(); 184 } 185 return 0; 186 } 187 188 private static org.apache.commons.logging.Log log= 189 org.apache.commons.logging.LogFactory.getLog( ChannelJni.class ); 190 191 } 192 | Popular Tags |