FileDocCategorySizeDatePackage
AprImpl.javaAPI DocApache Tomcat 6.0.1410386Fri Jul 20 04:20:32 BST 2007org.apache.jk.apr

AprImpl

public class AprImpl extends org.apache.jk.core.JkHandler
Implements the interface with the APR library. This is for internal-use only. The goal is to use 'natural' mappings for user code - for example java.net.Socket for unix-domain sockets, etc.

Fields Summary
static AprImpl
aprSingleton
String
baseDir
String
aprHome
String
soExt
static boolean
ok
boolean
initialized
Hashtable
jkHandlers
String
jniModeSo
String
nativeSo
static boolean
jniMode
private static org.apache.juli.logging.Log
log
Constructors Summary
public AprImpl()

    
      
        aprSingleton=this;
    
Methods Summary
public voidaddJkHandler(java.lang.String type, org.apache.jk.core.JkHandler cb)
Add a Handler for jni callbacks.

        jkHandlers.put( type, cb );
    
public static java.lang.ObjectcreateJavaContext(java.lang.String type, long cContext)

        // XXX will be an instance method, fields accessible directly
        AprImpl apr=aprSingleton;
        JkChannel jkH=(JkChannel)apr.jkHandlers.get( type );
        if( jkH==null ) return null;

        MsgContext ep=jkH.createMsgContext();

        ep.setSource( jkH );
        
        ep.setJniContext( cContext );
        return ep;
    
public native longcreateJkHandler(long xEnv, java.lang.String compName)

public static byte[]getBuffer(java.lang.Object ctx, int id)
Return a buffer associated with the ctx.

        return ((MsgContext)ctx).getBuffer(  id );
    
public native longgetJkEnv()

public native longgetJkHandler(long xEnv, java.lang.String compName)
Get a native component

return
0 if the component is not found.

public voidinit()

        try {
            initialized=true;
            loadNative();

            initialize();
            jkSetAttribute(0, 0, "channel:jni", "starting");
            
            log.info("JK: Initialized apr" );
            
        } catch( Throwable t ) {
            throw new IOException( t.toString() );
        }
        ok=true;
    
public native intinitialize()
Initialize APR

public booleanisLoaded()

        if( ! initialized ) {
            try {
                init();
            } catch( Throwable t ) {
                log.info("Apr not loaded: " + t);
            }
        }
        return ok;
    
public native intjkDestroy(long xEnv, long componentP)

public native java.lang.StringjkGetAttribute(long xEnv, long componentP, java.lang.String name)

public native intjkInit(long xEnv, long componentP)

public static native intjkInvoke(long xEnv, long componentP, long endpointP, int code, byte[] data, int off, int len, int raw)
Send the packet to the C side. On return it contains the response or indication there is no response. Asymetrical because we can't do things like continuations.

public native voidjkRecycle(long xEnv, long endpointP)
Recycle an endpoint after use.

public native intjkSetAttribute(long xEnv, long componentP, java.lang.String name, java.lang.String val)

public static intjniInvoke(long jContext, java.lang.Object ctx)

        try {
            MsgContext ep=(MsgContext)ctx;
            ep.setJniEnv(  jContext );
            ep.setType( 0 );
            return ((MsgContext)ctx).execute();
        } catch( Throwable ex ) {
            ex.printStackTrace();
            return -1;
        }
    
public static voidjniMode()


    
        
        jniMode=true;
    
public voidloadNative()
This method of loading the libs doesn't require setting LD_LIBRARY_PATH. Assuming a 'right' binary distribution, or a correct build all files will be in their right place. The burden is on our code to deal with platform specific extensions and to keep the paths consistent - not easy, but worth it if it avoids one extra step for the user. Of course, this can change to System.load() and putting the libs in LD_LIBRARY_PATH.

        if( aprHome==null )
            aprHome=baseDir;

        // XXX Update for windows
        if( jniMode ) {
            /* In JNI mode we use mod_jk for the native functions.
               This seems the cleanest solution that works with multiple
               VMs.
            */
            if (jniModeSo.equals("inprocess")) {
                ok=true;
                return;                                
            }
            try {
                log.info("Loading " + jniModeSo);
                if( jniModeSo!= null ) System.load( jniModeSo );
            } catch( Throwable ex ) {
                // ignore
                //ex.printStackTrace();
                return;
            }
            ok=true;
            return;
        }
        
            /*
              jkjni _must_ be linked with apr and crypt -
              this seem the only ( decent ) way to support JDK1.4 and
              JDK1.3 at the same time
              try {
                  System.loadLibrary( "crypt" );
              } catch( Throwable ex ) {
                  // ignore
                  ex.printStackTrace();
              }
              try {
                  System.loadLibrary( "apr" );
              } catch( Throwable ex ) {
                  System.out.println("can't load apr, that's fine");
                  ex.printStackTrace();
              }
            */
        try {
            if( nativeSo == null ) {
                // This will load libjkjni.so or jkjni.dll in LD_LIBRARY_PATH
                log.debug("Loading jkjni from " + System.getProperty("java.library.path"));
                System.loadLibrary( "jkjni" );
            } else {
                System.load( nativeSo );
            }
        } catch( Throwable ex ) {
            ok=false;
            //ex.printStackTrace();
            throw ex;
        }
    
public voidloadNative(java.lang.String libPath)

        try {
            System.load( libPath );
        } catch( Throwable ex ) {
            ok=false;
            if( log.isDebugEnabled() ) 
                log.debug( "Error loading native library ", ex);
        }
    
public native voidreleaseJkEnv(long xEnv)
Clean the temp pool, put back the env in the pool

public voidsetAprHome(java.lang.String s)

        aprHome=s;
    
public voidsetBaseDir(java.lang.String s)
Native libraries are located based on base dir. XXX Add platform, version, etc

        baseDir=s;
    
public static voidsetErr(java.lang.String filename)
Sets the System.err stream

        try{ 
            if( filename !=null ){
                System.setErr( new PrintStream(new FileOutputStream(filename )));
            }                                                 
        }catch (Throwable th){
        }
    
public voidsetJniModeSo(java.lang.String jniModeSo)
Name of the so used in inprocess mode

        this.jniModeSo=jniModeSo;
    
public voidsetNativeSo(java.lang.String nativeSo)
name of the so used by java. If not set we'll loadLibrary("jkjni" ), if set we load( nativeSo )

        this.nativeSo=nativeSo;
    
public static voidsetOut(java.lang.String filename)
Sets the System.out stream

        try{ 
            if( filename !=null ){
                System.setOut( new PrintStream(new FileOutputStream(filename )));
            }
        }catch (Throwable th){
        }
    
public voidsetSoExt(java.lang.String s)

        soExt=s;
    
public native intterminate()