1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jk.common;
import java.io.IOException;
import javax.management.ObjectName;
import org.apache.jk.apr.AprImpl;
import org.apache.jk.core.JkHandler;
import org.apache.jk.core.Msg;
import org.apache.jk.core.MsgContext;
import org.apache.jk.core.JkChannel;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.buf.C2BConverter;
import org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.modeler.Registry;
/**
* Base class for components using native code ( libjkjni.so ).
* It allows to access the jk_env and wrap ( 'box' ? ) a native
* jk component, and call it's methods.
*
* Note that get/setAttribute are expensive ( Strings, etc ),
* invoke() is were all optimizations are done. We do recycle
* all memory on both C and java sides ( the only exception is
* when we attempt pinning but the VM doesn't support it ). The
* low level optimizations from ByteBuffer, etc are used to
* reduce the overhead of passing strings.
*
* @author Costin Manolache
*/
public class JniHandler extends JkHandler {
protected AprImpl apr;
// The native side handler
protected long nativeJkHandlerP;
protected String jkHome;
// Dispatch table codes. Hardcoded for now, will change when we have more handlers.
public static final int JK_HANDLE_JNI_DISPATCH=0x15;
public static final int JK_HANDLE_SHM_DISPATCH=0x16;
public static final int MSG_NOTE=0;
public static final int MB_NOTE=2;
private boolean paused = false;
public JniHandler() {
}
/**
*/
public void setJkHome( String s ) {
jkHome=s;
}
public String getJkHome() {
return jkHome;
}
/** You must call initNative() inside the component init()
*/
public void init() throws IOException {
// static field init, temp
}
protected void initNative(String nativeComponentName) {
apr=(AprImpl)wEnv.getHandler("apr");
if( apr==null ) {
// In most cases we can just load it automatically.
// that requires all libs to be installed in standard places
// ( LD_LIBRARY_PATH, /usr/lib
try {
apr=new AprImpl();
wEnv.addHandler("apr", apr);
apr.init();
if( oname != null ) {
ObjectName aprname=new ObjectName(oname.getDomain() +
":type=JkHandler, name=apr");
Registry.getRegistry(null, null).registerComponent(apr, aprname, null);
}
} catch( Throwable t ) {
log.debug("Can't load apr", t);
apr=null;
}
}
if( apr==null || ! apr.isLoaded() ) {
if( log.isDebugEnabled() )
log.debug("No apr, disabling jni proxy ");
apr=null;
return;
}
try {
long xEnv=apr.getJkEnv();
nativeJkHandlerP=apr.getJkHandler(xEnv, nativeComponentName );
if( nativeJkHandlerP==0 ) {
log.debug("Component not found, creating it " + nativeComponentName );
nativeJkHandlerP=apr.createJkHandler(xEnv, nativeComponentName);
}
log.debug("Native proxy " + nativeJkHandlerP );
apr.releaseJkEnv(xEnv);
} catch( Throwable t ) {
apr=null;
log.info("Error calling apr ", t);
}
}
public void appendString( Msg msg, String s, C2BConverter charsetDecoder)
throws IOException
{
ByteChunk bc=charsetDecoder.getByteChunk();
charsetDecoder.recycle();
charsetDecoder.convert( s );
charsetDecoder.flushBuffer();
msg.appendByteChunk( bc );
}
public void pause() throws Exception {
synchronized(this) {
paused = true;
}
}
public void resume() throws Exception {
synchronized(this) {
paused = false;
notifyAll();
}
}
/** Create a msg context to be used with the shm channel
*/
public MsgContext createMsgContext() {
if( nativeJkHandlerP==0 || apr==null )
return null;
synchronized(this) {
try{
while(paused) {
wait();
}
}catch(InterruptedException ie) {
// Ignore, since it can't happen
}
}
try {
MsgContext msgCtx=new MsgContext();
MsgAjp msg=new MsgAjp();
msgCtx.setSource( (JkChannel)this );
msgCtx.setWorkerEnv( wEnv );
msgCtx.setNext( this );
msgCtx.setMsg( MSG_NOTE, msg); // XXX Use noteId
C2BConverter c2b=new C2BConverter( "iso-8859-1" );
msgCtx.setConverter( c2b );
MessageBytes tmpMB= MessageBytes.newInstance();
msgCtx.setNote( MB_NOTE, tmpMB );
return msgCtx;
} catch( Exception ex ) {
log.error("Can't create endpoint", ex);
return null;
}
}
public void setNativeAttribute(String name, String val) throws IOException {
if( apr==null ) return;
if( nativeJkHandlerP == 0 ) {
log.error( "Unitialized component " + name+ " " + val );
return;
}
long xEnv=apr.getJkEnv();
apr.jkSetAttribute( xEnv, nativeJkHandlerP, name, val );
apr.releaseJkEnv( xEnv );
}
public void initJkComponent() throws IOException {
if( apr==null ) return;
if( nativeJkHandlerP == 0 ) {
log.error( "Unitialized component " );
return;
}
long xEnv=apr.getJkEnv();
apr.jkInit( xEnv, nativeJkHandlerP );
apr.releaseJkEnv( xEnv );
}
public void destroyJkComponent() throws IOException {
if( apr==null ) return;
if( nativeJkHandlerP == 0 ) {
log.error( "Unitialized component " );
return;
}
long xEnv=apr.getJkEnv();
apr.jkDestroy( xEnv, nativeJkHandlerP );
apr.releaseJkEnv( xEnv );
}
protected void setNativeEndpoint(MsgContext msgCtx) {
long xEnv=apr.getJkEnv();
msgCtx.setJniEnv( xEnv );
long epP=apr.createJkHandler(xEnv, "endpoint");
log.debug("create ep " + epP );
if( epP == 0 ) return;
apr.jkInit( xEnv, epP );
msgCtx.setJniContext( epP );
}
protected void recycleNative(MsgContext ep) {
apr.jkRecycle(ep.getJniEnv(), ep.getJniContext());
}
/** send and get the response in the same buffer. This calls the
* method on the wrapped jk_bean object.
*/
protected int nativeDispatch( Msg msg, MsgContext ep, int code, int raw )
throws IOException
{
if( log.isDebugEnabled() )
log.debug( "Sending packet " + code + " " + raw);
if( raw == 0 ) {
msg.end();
if( log.isTraceEnabled() ) msg.dump("OUT:" );
}
// Create ( or reuse ) the jk_endpoint ( the native pair of
// MsgContext )
long xEnv=ep.getJniEnv();
long nativeContext=ep.getJniContext();
if( nativeContext==0 || xEnv==0 ) {
setNativeEndpoint( ep );
xEnv=ep.getJniEnv();
nativeContext=ep.getJniContext();
}
if( xEnv==0 || nativeContext==0 || nativeJkHandlerP==0 ) {
log.error("invokeNative: Null pointer ");
return -1;
}
// Will process the message in the current thread.
// No wait needed to receive the response, if any
int status=AprImpl.jkInvoke( xEnv,
nativeJkHandlerP,
nativeContext,
code, msg.getBuffer(), 0, msg.getLen(), raw );
if( status != 0 && status != 2 ) {
log.error( "nativeDispatch: error " + status, new Throwable() );
}
if( log.isDebugEnabled() ) log.debug( "Sending packet - done " + status);
return status;
}
/** Base implementation for invoke. Dispatch the action to the native
* code, where invoke() is called on the wrapped jk_bean.
*/
public int invoke(Msg msg, MsgContext ep )
throws IOException
{
long xEnv=ep.getJniEnv();
int type=ep.getType();
int status=nativeDispatch(msg, ep, type, 0 );
apr.jkRecycle(xEnv, ep.getJniContext());
apr.releaseJkEnv( xEnv );
return status;
}
private static org.apache.juli.logging.Log log=
org.apache.juli.logging.LogFactory.getLog( JniHandler.class );
}
|