LibLoader.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006 package glguerin.util;
00007
00008 import java.lang.reflect.*;
00009 import java.io.*;
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00076 public class LibLoader
00077 {
00083 public static boolean isVerbose = Boolean.getBoolean( "glguerin.util.LibLoader.debug" );
00084
00085
00087 private static LibLoader knownLoader;
00088
00089
00094 public static synchronized LibLoader
00095 getLoader()
00096 {
00097 if ( knownLoader == null )
00098 {
00099 knownLoader = makeLoader();
00100
00101 }
00102 return ( knownLoader );
00103 }
00104
00105
00110 public static LibLoader
00111 makeLoader()
00112 {
00113
00114
00115
00116 String className = System.getProperty( "glguerin.util.LibLoader.imp", "" );
00117
00118 if ( className != null && className.length() > 0 )
00119 {
00120 try
00121 {
00122
00123 return ( (LibLoader) Class.forName( className ).newInstance() );
00124 }
00125 catch ( ThreadDeath mustRethrow )
00126 { throw mustRethrow; }
00127 catch ( Throwable everythingElse )
00128 { }
00129 }
00130
00131
00132 return ( new LibLoader() );
00133 }
00134
00135
00136
00137
00142 protected final File dirFallback;
00143
00145 public
00146 LibLoader()
00147 {
00148 dirFallback = makeFallbackDir();
00149 blab();
00150 }
00151
00153 protected void
00154 blab()
00155 {
00156 if ( isVerbose )
00157 System.err.println( "# LibLoader dir: " + dirFallback );
00158 }
00159
00160
00169 public void
00170 loadLibrary( String libName )
00171 {
00172
00173
00174 try
00175 { System.loadLibrary( libName ); }
00176 catch ( UnsatisfiedLinkError failure )
00177 { this.load( libName, dirFallback ); }
00178 }
00179
00200 protected void
00201 load( String libName, File location )
00202 {
00203 if ( location == null )
00204 throw new UnsatisfiedLinkError( "Don't know how to load JNI library: " + libName );
00205
00206
00207
00208 String libPath = new File( location, "lib" + libName + ".jnilib" ).getAbsolutePath();
00209
00210 if ( isVerbose )
00211 System.err.println( "# LibLoader: trying " + libPath );
00212
00213 System.load( libPath );
00214 }
00215
00216
00226 protected File
00227 makeFallbackDir()
00228 {
00229 try
00230 {
00231
00232
00233
00234 Class[] noArgs = new Class[ 0 ];
00235 Object[] nothing = new Object[ 0 ];
00236
00237
00238
00239
00240 Class bundleClass = Class.forName( "com.apple.cocoa.foundation.NSBundle" );
00241
00242 if ( isVerbose )
00243 System.err.println( "# LibLoader got NSBundle: " + bundleClass );
00244
00245
00246
00247 Object theNSBundle = bundleClass.getMethod( "mainBundle", noArgs ).invoke( null, nothing );
00248 Object thePath = bundleClass.getMethod( "resourcePath", noArgs ).invoke( theNSBundle, nothing );
00249
00250
00251
00252 return ( new File( thePath.toString(), "Java" ) );
00253 }
00254 catch ( ClassNotFoundException why )
00255 { }
00256 catch ( NoSuchMethodException why )
00257 { }
00258 catch ( IllegalAccessException why )
00259 { }
00260 catch ( IllegalArgumentException why )
00261 { }
00262 catch ( InvocationTargetException why )
00263 { }
00264
00265
00266
00267
00268
00269
00270
00271 return ( null );
00272 }
00273
00274
00276 public String
00277 toString()
00278 { return ( "System.loadLibrary()" + File.pathSeparator + dirFallback ); }
00279
00280 }