java-bootstrap-platform/src/bootstrap/platform/NativeLoader.java

120 lines
3.9 KiB
Java
Executable File

package bootstrap.platform;
import java.io.File;
import java.io.IOException;
import bootstrap.platform.Platform;
import bootstrap.platform.Ressource;
public class NativeLoader {
private static boolean patchedLinux = false;
public static String nativeLibraryName(String libName){
switch (Platform.getOperatingSystem())
{
case LINUX:
return String.format("lib%s.so",libName);
case OSX:
return String.format("lib%s.dylib",libName);
case WINDOWS:
return String.format("%s.dll",libName);
default:
throw new RuntimeException("Betriebssystem nicht unterstützt!");
}
}
public static String nativeResourceName(String libName){
return String.format("/native/%s/%s", Platform.PlatformName(), nativeLibraryName(libName));
}
public static String nativeLocalResourceName(String libName){
return String.format("native%s%s%s%s", File.separator, Platform.PlatformName(), File.separator, nativeLibraryName(libName));
}
/*
* loadLibrary()
*
* Versuche native Bibliothek zu laden
*
* 1. System.loadLibrary(...)
* 2. System.load(...)
* 3. /native/...
* 4. CLASSPATH:/native/...
*
*/
public static void loadLibrary(String libName)
{
try
{
System.loadLibrary(libName);
System.out.println(String.format("NativeLoader.loadLibrary(): System.loadLibrary('%s') erfolgreich",libName));
return;
} catch (UnsatisfiedLinkError e)
{
System.err.println(String.format("NativeLoader.loadLibrary(): System.loadLibrary('%s') erfolglos",libName));
};
String libFileName = new File("").getAbsolutePath() + File.separator + nativeLibraryName(libName);
try
{
System.load(libFileName);
System.out.println(String.format("NativeLoader.loadLibrary(): System.load('%s') erfolgreich",libFileName));
return;
} catch (UnsatisfiedLinkError e)
{
System.err.println(String.format("NativeLoader.loadLibrary(): System.load('%s') erfolglos",libFileName));
};
String nativeLocalResourceName = new File("").getAbsolutePath() + File.separator + nativeLocalResourceName(libName);
try
{
System.load(nativeLocalResourceName);
System.out.println(String.format("NativeLoader.loadLibrary(): System.load('%s') erfolgreich",nativeLocalResourceName));
return;
} catch (UnsatisfiedLinkError e)
{
System.err.println(String.format("NativeLoader.loadLibrary(): System.load('%s') erfolglos",nativeLocalResourceName));
};
String nativeResourceName = nativeResourceName(libName);
try
{
System.load(nativeResourceName);
System.out.println(String.format("NativeLoader.loadLibrary(): System.load('%s') erfolgreich",nativeResourceName));
return;
} catch (UnsatisfiedLinkError e)
{
System.err.println(String.format("NativeLoader.loadLibrary(): System.load('%s') erfolglos",nativeResourceName));
};
File tempLibFile;
try {
tempLibFile = File.createTempFile("extract-", nativeLibraryName(libName));
} catch (IOException ioe){
System.err.println(String.format("NativeLoader.loadLibrary(): File.createTempFile('extract-','%s') erfolglos",nativeLibraryName(libName)));
throw new RuntimeException(ioe);
}
try {
Ressource.extract(nativeResourceName, tempLibFile.getAbsolutePath());
System.load(tempLibFile.getAbsolutePath());
System.out.println(String.format("NativeLoader.loadLibrary(): System.load('%s') erfolgreich",tempLibFile.getAbsolutePath()));
return;
} catch (UnsatisfiedLinkError e)
{
System.err.println(String.format("NativeLoader.loadLibrary(): System.load('%s') erfolglos",tempLibFile.getAbsolutePath()));
} catch (Exception e){
System.err.println(String.format("NativeLoader.loadLibrary(): Resource.extract('%s','%s') erfolglos",nativeResourceName,tempLibFile.getAbsolutePath()));
throw new RuntimeException(e);
}
}
static {
/* Suchpfad um aktuelles Arbeitsberzeichnis erweitern */
System.setProperty("java.library.path",String.format(".:%s",System.getProperty("java.library.path")));
}
}