1 16 17 package org.apache.jk.apr; 18 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.io.PrintStream ; 22 import java.util.Hashtable ; 23 import org.apache.jk.core.JkHandler; 24 import org.apache.jk.core.MsgContext; 25 import org.apache.jk.core.JkChannel; 26 27 32 public class AprImpl extends JkHandler { static AprImpl aprSingleton=null; 34 35 String baseDir; 36 String aprHome; 37 String soExt="so"; 38 39 static boolean ok=true; 40 boolean initialized=false; 41 Hashtable jkHandlers=new Hashtable (); 43 44 String jniModeSo="inprocess"; 46 String nativeSo; 49 50 public AprImpl() { 51 aprSingleton=this; 52 } 53 54 56 59 public void setBaseDir(String s) { 60 baseDir=s; 61 } 62 63 public void setSoExt(String s ) { 64 soExt=s; 65 } 66 67 public void setAprHome( String s ) { 69 aprHome=s; 70 } 71 72 74 public void addJkHandler(String type, JkHandler cb) { 75 jkHandlers.put( type, cb ); 76 } 77 78 80 public void setJniModeSo(String jniModeSo ) { 81 this.jniModeSo=jniModeSo; 82 } 83 84 87 public void setNativeSo( String nativeSo ) { 88 this.nativeSo=nativeSo; 89 } 90 91 92 93 public static void setOut( String filename ) { 94 try{ 95 if( filename !=null ){ 96 System.setOut( new PrintStream (new FileOutputStream (filename ))); 97 } 98 }catch (Throwable th){ 99 } 100 } 101 102 103 public static void setErr( String filename ) { 104 try{ 105 if( filename !=null ){ 106 System.setErr( new PrintStream (new FileOutputStream (filename ))); 107 } 108 }catch (Throwable th){ 109 } 110 } 111 112 115 public native int initialize(); 116 117 public native int terminate(); 118 119 120 121 123 124 126 public native long getJkEnv(); 127 128 130 public native void releaseJkEnv(long xEnv); 131 132 133 136 137 140 public native long getJkHandler(long xEnv, String compName ); 141 142 public native long createJkHandler(long xEnv, String compName ); 143 144 public native int jkSetAttribute( long xEnv, long componentP, String name, String val ); 145 146 public native String jkGetAttribute( long xEnv, long componentP, String name ); 147 148 public native int jkInit( long xEnv, long componentP ); 149 150 public native int jkDestroy( long xEnv, long componentP ); 151 152 156 public static native int jkInvoke(long xEnv, long componentP, long endpointP, 157 int code, byte data[], int off, int len, int raw); 158 159 161 public native void jkRecycle(long xEnv, long endpointP); 162 163 168 public static Object createJavaContext(String type, long cContext) { 169 AprImpl apr=aprSingleton; 171 JkChannel jkH=(JkChannel)apr.jkHandlers.get( type ); 172 if( jkH==null ) return null; 173 174 MsgContext ep=jkH.createMsgContext(); 175 176 ep.setSource( jkH ); 177 178 ep.setJniContext( cContext ); 179 return ep; 180 } 181 182 184 public static byte[] getBuffer( Object ctx, int id ) { 185 return ((MsgContext)ctx).getBuffer( id ); 186 } 187 188 public static int jniInvoke( long jContext, Object ctx ) { 189 try { 190 MsgContext ep=(MsgContext)ctx; 191 ep.setJniEnv( jContext ); 192 ep.setType( 0 ); 193 return ((MsgContext)ctx).execute(); 194 } catch( Throwable ex ) { 195 ex.printStackTrace(); 196 return -1; 197 } 198 } 199 200 202 public void init() throws IOException { 203 try { 204 initialized=true; 205 loadNative(); 206 207 initialize(); 208 jkSetAttribute(0, 0, "channel:jni", "starting"); 209 210 log.info("JK: Initialized apr" ); 211 212 } catch( Throwable t ) { 213 throw new IOException ( t.toString() ); 214 } 215 ok=true; 216 } 217 218 public boolean isLoaded() { 219 if( ! initialized ) { 220 try { 221 init(); 222 } catch( Throwable t ) { 223 log.info("Apr not loaded: " + t); 224 } 225 } 226 return ok; 227 } 228 229 static boolean jniMode=false; 230 231 232 public static void jniMode() { 233 jniMode=true; 234 } 235 236 247 public void loadNative() throws Throwable { 248 if( aprHome==null ) 249 aprHome=baseDir; 250 251 if( jniMode ) { 253 257 if (jniModeSo.equals("inprocess")) { 258 ok=true; 259 return; 260 } 261 try { 262 log.info("Loading " + jniModeSo); 263 if( jniModeSo!= null ) System.load( jniModeSo ); 264 } catch( Throwable ex ) { 265 return; 268 } 269 ok=true; 270 return; 271 } 272 273 290 try { 291 if( nativeSo == null ) { 292 log.debug("Loading jkjni from " + System.getProperty("java.library.path")); 294 System.loadLibrary( "jkjni" ); 295 } else { 296 System.load( nativeSo ); 297 } 298 } catch( Throwable ex ) { 299 ok=false; 300 throw ex; 302 } 303 } 304 305 public void loadNative(String libPath) { 306 try { 307 System.load( libPath ); 308 } catch( Throwable ex ) { 309 ok=false; 310 if( log.isDebugEnabled() ) 311 log.debug( "Error loading native library ", ex); 312 } 313 } 314 private static org.apache.commons.logging.Log log= 315 org.apache.commons.logging.LogFactory.getLog( AprImpl.class ); 316 } 317 | Popular Tags |