FileDocCategorySizeDatePackage
NativeLibraryFinder.javaAPI DocFobs4JMF API 0.4.13390Tue Dec 19 15:10:44 GMT 2006com.moesol.bindings

NativeLibraryFinder

public class NativeLibraryFinder extends Object

Fields Summary
private static final Logger
s_logger
private static final ArrayList
s_stategies
public static String[]
OS_PREFIXES
private static File
s_libraryDirectory
Constructors Summary
Methods Summary
public static java.io.FilegetLibraryDirectory()

		return s_libraryDirectory;
	
public static java.util.ListgetStrategies()

		return s_stategies;
	
public static voidloadLibrary(java.lang.Class classInJar, java.lang.String libraryName)
Finds and loads a native library using several different strategies. The default set of strategies tries to find the native library using the following search:
  1. tries System.loadLibrary
  2. tries to find the library in the same directory as the jar. If the URL to the jar is not the file: protocol this step is skipped.
  3. tries to find a library with the same name as the jar file with the .jar extension removed. This supports maven since maven will rename artifacts so that the name of the jar file and native library will include the version number. For example, on Windows a jar file of fobs4jmf-0.4.jar will cause a search for fobs4jmf-0.4.dll
  4. tries to find the library as a resource. If found as a resource the native library is extracted to the library directory property and loaded from there.

For both the file and resource searches it also searchs for derivations based on the os, cpu architecture, and os version. So on Windows a loadLibrary("foo") looks for

{jar or classes directory}\foo.dll
{jar or classes directory}\Windows XP\x86\foo.dll
{jar or classes directory}\Windows XP\x86\5.1\foo.dll
{resource}/foo.dll
{resource}/Windows XP/x86/foo.dll
{resource}/Windows XP/x86/5.1/foo.dll

param
classInJar a class that is in the jar to be located
param
libraryName name of library to load

	
	                                                                                                                                                                                                                        	 
	       
		try {
			System.loadLibrary(libraryName);
		} catch (UnsatisfiedLinkError e) {
			for (Iterator it = s_stategies.iterator(); it.hasNext(); ) {
				NativeLibraryFinderStrategy s = (NativeLibraryFinderStrategy)it.next();
				try {
					s.findAndLoad(classInJar, libraryName);
					return; // worked
				} catch (UnsatisfiedLinkError e1) {
					s_logger.log(Level.FINE, "strategy failed: " + s, e1);
				}
			}
			throw e; // Throw the original System error for consistency
		}
	
public static voidsetLibraryDirectory(java.io.File libraryDirectory)

		s_libraryDirectory = libraryDirectory;