net.xoetrope.os
Class LibraryLoader

java.lang.Object
  extended by net.xoetrope.os.LibraryLoader

public class LibraryLoader
extends java.lang.Object

    

Loads a native library file. The class can load the library by attempting to have the Sytem class load the library from the library path. If this fails the class can attempt to load the file by extracting it from the classpath, including the Jar files on the classpath and then loading it with the full file path. A temporary directory is used to temporarily extract the file. If the file is loaded successfully the library name is placed in a hashtable so that subsequent calls to load the library to not actually attempt to load the library.

Java Webstart and JNLP can also fullfill a similar role to this utility class using the 'native' library element in the JNLP file.

Copyright (c) Xoetrope Ltd., 2002-2004

$Revision: 2.6 $

License: see License.txt


Field Summary
protected static int BUF_LEN
          The internal buffer length
protected static java.lang.String extractDir
          The directory to which files are extracted.
 
Constructor Summary
LibraryLoader()
           
 
Method Summary
static java.lang.String extractFile(java.lang.ClassLoader classLoader, java.lang.String resName)
          Extract a file from a jar file to the specified location
 java.lang.String extractFile(java.lang.ClassLoader classLoader, java.lang.String resName, boolean overwrite)
          Extract a file from a jar file to the specified location
static java.lang.String getExtractDirectory()
          Get the directory name to which the library will be extracted.
protected  boolean isUpToDate(java.net.URL url, java.io.File targetFile)
          Check that the temporary file exists and is up-to-date
static void load(java.lang.ClassLoader clazz, java.lang.String resName)
          Load a library resource (native library)
 void load(java.lang.ClassLoader classLoader, java.lang.String resName, boolean checkLibPath, boolean overwrite)
          Load a native library
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUF_LEN

protected static final int BUF_LEN
The internal buffer length

See Also:
Constant Field Values

extractDir

protected static java.lang.String extractDir
The directory to which files are extracted.

Constructor Detail

LibraryLoader

public LibraryLoader()
Method Detail

load

public static void load(java.lang.ClassLoader clazz,
                        java.lang.String resName)
Load a library resource (native library)

Parameters:
clazz - the class loader
resName - the resource to load

extractFile

public static java.lang.String extractFile(java.lang.ClassLoader classLoader,
                                           java.lang.String resName)
                                    throws java.io.IOException
Extract a file from a jar file to the specified location

Parameters:
classLoader - the classloder to use to load the file
resName - the resource/file name
Returns:
the path to which the file is extracted
Throws:
java.io.IOException - IO problems reading or extracting the file.

load

public void load(java.lang.ClassLoader classLoader,
                 java.lang.String resName,
                 boolean checkLibPath,
                 boolean overwrite)
Load a native library

Parameters:
classLoader - the classLoader used to find the library/resource
resName - the library/resource name
checkLibPath - true to check the system library path first before checking the resource path
overwrite - true to overwrite any existing temporary file, if false the the timestamp is checked to see if the file needs to be extracted to the temporary directory

getExtractDirectory

public static java.lang.String getExtractDirectory()
Get the directory name to which the library will be extracted.

Returns:
the path

isUpToDate

protected boolean isUpToDate(java.net.URL url,
                             java.io.File targetFile)
Check that the temporary file exists and is up-to-date

Parameters:
url - the URL of the source file
targetFile - the temporary file
Returns:
true if the file exists and is up-to-date

extractFile

public java.lang.String extractFile(java.lang.ClassLoader classLoader,
                                    java.lang.String resName,
                                    boolean overwrite)
                             throws java.io.IOException
Extract a file from a jar file to the specified location

Parameters:
classLoader - the classloder to use to load the file
overwrite - true to overwrite any existing file
resName - the resource to extract
Returns:
the path to which the file is extracted
Throws:
java.io.IOException - IO problems reding or extracting the file