jpathwatch-java.src.name.pachler.nio.file.impl.NativeLibLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpathwatch Show documentation
Show all versions of jpathwatch Show documentation
jpathwatch is a Java library for monitoring directories for changes. It
uses the host platform's native OS functions to achive this to avoid
polling.
The newest version!
/*
* This file has been derived from the NatviveLibLoader class in rxtx.
* Please see the licence attached below.
*/
/*-------------------------------------------------------------------------
| rxtx is a native interface to serial ports in java.
| Copyright (c) 2008 by Trent Jarvi [email protected].
|
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
--------------------------------------------------------------------------*/
package name.pachler.nio.file.impl;
import java.io.*;
import java.nio.channels.FileLock;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class loads native libraries packaged in the JAR file that this class resides in.
* The NativeLibLoader supports a number of preconfigured native libraries,
* which it looks up by their name to find, extract and load their binary file.
*
* For each platform there exists a PlatformArchLibSet, which defines a list of
* native libraries available for that platform in LibraryImplementation instances.
*
* When a library "foo" is requested through the loadLibrary() method,
* all LibraryImplementation instances for the current platform's PlatformArchLibSet
* is searched.
*
* If a matching LibraryImplementation is found, first the library extraction and
* loading is attempted with a default library name. If that fails, extraction and
* loading is attempted with a unique temporary file name for the library.
* the steps are the following:
*
* - It is attempted to open and lock file indicated by the library
* path for reading. If successful, the integrity if the file is checked (if
* it is the same as the one stored in the JAR file. If successful the library
* is loaded.
* - If the step above fails, it is attempted to open and lock the default
* library file for writing. If successful, the library file is extracted to
* the path and the library is loaded from there.
*
* If all of that fails, an attempt is to load the library with
* System.loadLibrary(), which requires the library to be installed in the
* system's native library path. This is the old (but common) way to load
* native libraries in Java, but requires the library to be installed
* separately in a native library directory accessible by the JVM.
*
* The Default Library Path
* The default library path is something like "/tmp/bar-1-0-mylib.so". Because
* it remains the same on multiple invocations of the same program, one program
* might find a library file from a previously running program that hasn't been
* cleaned up yet. The library file might even be there from a program that is
* still running, creating the potential for race conditions, so
* the implementation is careful to avoid clashes in such cases (such as two
* JVMs trying to extract the same library to the same file at the same time).
*
* The default library path is formed as
* {tempdir}/{productname}-{major}-{minor}-{binaryname}, where {productname}
* is the product name that's configured in NativeLibLoader's PRODUCTNAME
* static variable, {major} and {minor} are the major and minor versions
* configured for the LibraryImplementation, and {binaryname} is the name of
* the binary as it is stored in the JAR (without the path), like mylib.so.
* So for a library "foo" stored in the JAR as "Windows/mylib.dll" with version
* 1.0, for PRODUCTNAME being "bar", the default library path would be
* "c:\Documents and Settings\myuser\temp\bar-1-0-mylib.dll"
*
* @author Uwe Pachler, Trent Jarvi
* @version %I%, %G%
*/
public class NativeLibLoader
{
private static void loadDefaultLibrary(String name) {
System.loadLibrary(name);
}
private static class LibraryImplementation
{
public String libraryName;
public String libraryResource;
LibraryImplementation(String libraryName, String libraryResource)
{
this.libraryName = libraryName;
this.libraryResource = libraryResource;
}
}
private static class PlatformArchLibSet
{
public String osName;
public String archName;
public LibraryImplementation[] implementations;
private String[] command;
PlatformArchLibSet(String osName, String archName, LibraryImplementation[] implementations, String[] command)
{
this.osName = osName;
this.archName = archName;
this.implementations = implementations;
this.command = command;
}
LibraryImplementation findImplementation(String name)
{
for(int i=0; i loadedLibraries = new HashSet();
private static PlatformArchLibSet libSet = findPlatformArchLibSet();
private static String PRODUCTNAME = "jpathwatch";
private static int VERSION_MAJOR=0;
private static int VERSION_MINOR=95;
private static String stripPathFromResourceName(String s)
{
int slashPos = s.lastIndexOf('/');
if(slashPos == -1)
return s;
return s.substring(slashPos+1);
}
private static PlatformArchLibSet findPlatformArchLibSet()
{
String osName = System.getProperty("os.name");
String archName = System.getProperty("os.arch");
for(int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy