public class Librarian extends Object implements CleanupClient
Librarian
per Java runtime, therefore this class is implemented as a 'Singleton'.
Librarian
is capable of loading native libraries from a variety of different
source locations. However, you should place your library files in the 'native' directory. The
primary reason for supporting different source locations is to facilitate testing in a
development environment, without the need to actually packing the application into a *.jar file.Modifier and Type | Field and Description |
---|---|
private static int |
BLOCK_SIZE
The block size used for reading and writing data, 4k.
|
private static String |
CLIENT_EXTENSION
The extension appended to the client name when searching for it as a resource.
|
private Vector |
clients
A list of references to clients that use libraries that were extracted from a *.jar file.
|
private String |
extension
The extension to use for native libraries.
|
private static String |
FILE_PROTOCOL
Used to identify file URL protocols
|
private static String |
JAR_PROTOCOL
Used to identify jar URL protocols
|
private Vector |
libraryNames
A list of library names as they appear in the temporary directory.
|
private static Librarian |
me
The reference to the single instance of
Librarian . |
private static String |
NATIVE
The default directory for native library files.
|
private String |
nativeDirectory
The directory that is used to hold all native libraries.
|
private static String |
TEMP_LOCATION_KEY
The key used to retrieve the location of temporary files form the system properties.
|
private Vector |
temporaryFileNames
A list of fully qualified library names.
|
private Vector |
trackList
A list that is used to track all libraries that have been loaded.
|
Modifier | Constructor and Description |
---|---|
private |
Librarian()
This class is implemented as a 'Singleton'.
|
Modifier and Type | Method and Description |
---|---|
void |
cleanUp()
This method attempts to remove all native libraries that have been temporarily created from
the system.
|
private void |
extractFromJar(String name,
String destination,
NativeLibraryClient client)
Makes an attempt to extract the named library from the jar file and to store it on the local
file system for temporary use.
|
private String |
getClientPath(String name,
URL clientURL)
Returns the complete path (including file name) for the native library, assuming the native
library is located in the same directory from which the client was loaded.
|
static Librarian |
getInstance()
Returns an instance of
Librarian to use. |
private String |
getNativePath(String name,
NativeLibraryClient client)
Returns the complete path (including file name) for the native library, assuming the native
library is located in a directory where native libraries are ordinarily expected.
|
private String |
getTempFileName(String name)
Builds a temporary file name for the native library.
|
private boolean |
loaded(String name)
Verifies if the library has already been loaded and keeps track of all libraries that are
verified.
|
void |
loadLibrary(String name,
NativeLibraryClient client)
Loads the requested library.
|
private InputStream |
openInputStream(String name,
NativeLibraryClient client)
Opens an
InputStream to the native library. |
private String |
revisePath(String in)
Revises the given path to a file compatible path.
|
void |
setNativeDirectory(String directory)
Sets the directory where
Librarian will search for native files. |
private String |
strip(String name)
Strips the extension of the library name, if it has one.
|
private static final String JAR_PROTOCOL
private static final String FILE_PROTOCOL
private static final String TEMP_LOCATION_KEY
private static final String CLIENT_EXTENSION
private static final String NATIVE
private static final int BLOCK_SIZE
private static Librarian me
Librarian
. Used in static methods in
place of this
.private Vector trackList
private Vector clients
private Vector libraryNames
clients
list.private Vector temporaryFileNames
clients
list.private String nativeDirectory
private Librarian()
getInstance()
to obtain an instance for use.
public static Librarian getInstance()
Librarian
to use.Librarian
.public void loadLibrary(String name, NativeLibraryClient client) throws Exception
this
must be replaced by the reference
of the class that links with the library.
Librarian.getInstance ().loadLibrary ("MyLibrary", this);
name
- the name of the library. A file extension and path are not needed, in fact if
supplied, both is stripped off. A specific extension is appended.client
- the object that made the load requestException
- if all attempts to load the library fail.setNativeDirectory(java.lang.String)
private boolean loaded(String name)
name
- name of the library to verifytrue
if the library had already been loaded, otherwise
false
.private String strip(String name)
name
- the name of the libraryprivate void extractFromJar(String name, String destination, NativeLibraryClient client) throws Exception
name
- the simple name of the librarydestination
- the fully qualified name of the destination file.client
- the class that made the load request.Exception
- if the library can not be extracted from the *.jar file.FileNotFoundException
- if the *.jar file does not exist. The way things operate
here, this should actually never happen.private String getClientPath(String name, URL clientURL)
name
- the simple name of the libraryclientURL
- a URL that points to the client classprivate String getNativePath(String name, NativeLibraryClient client)
name
- the simple name of the libraryclient
- the class that made the load request.private String revisePath(String in)
in
- path to be revisedprivate InputStream openInputStream(String name, NativeLibraryClient client) throws Exception
InputStream
to the native library.name
- the simple name of the libraryclient
- the class that made the load request.InputStream
from which the library can be read.Exception
- if the library can not be located.private String getTempFileName(String name)
name
- the file name of the librarypublic void setNativeDirectory(String directory)
Librarian
will search for native files. Directories
are denoted relative to the root, where the root is the same location where the top level
Java package directory is located (usually called com
). The default directory
is native
.directory
- the directory where native files are located.public void cleanUp()
cleanUp
in interface CleanupClient
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses