forked from LupusNobilis/java-org.hwo
Native Bibliotheken reorganisiert
parent
29188d7adb
commit
28920e2286
Binary file not shown.
|
@ -1,135 +0,0 @@
|
||||||
package org.hwo.nativeloader;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.ObjectInputStream.GetField;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
|
|
||||||
import javax.management.RuntimeErrorException;
|
|
||||||
|
|
||||||
import org.hwo.os.OsDetect;
|
|
||||||
import org.hwo.os.OsDetect.Bitness;
|
|
||||||
import org.hwo.os.OsDetect.OsType;
|
|
||||||
|
|
||||||
public class NativeLoader {
|
|
||||||
|
|
||||||
private static boolean patchedLinux = false;
|
|
||||||
|
|
||||||
private static String calculateLibraryName(String libName){
|
|
||||||
|
|
||||||
String platformLibName;
|
|
||||||
|
|
||||||
if (OsDetect.getBitness()==Bitness.B32){
|
|
||||||
libName = libName + "32";
|
|
||||||
}
|
|
||||||
if (OsDetect.getBitness()==Bitness.B64){
|
|
||||||
libName = libName + "64";
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (OsDetect.getOperatingSystem())
|
|
||||||
{
|
|
||||||
case LINUX:
|
|
||||||
platformLibName = String.format("lib%s.so",libName);
|
|
||||||
break;
|
|
||||||
case OSX:
|
|
||||||
platformLibName = String.format("lib%s.dylib",libName);
|
|
||||||
break;
|
|
||||||
case WINDOWS:
|
|
||||||
platformLibName = String.format("%s.dll",libName);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new RuntimeException("Betriebssystem nicht unterst<73>tzt!");
|
|
||||||
}
|
|
||||||
|
|
||||||
return platformLibName;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean tryLoad(String libName){
|
|
||||||
try
|
|
||||||
{
|
|
||||||
System.loadLibrary(libName);
|
|
||||||
return true;
|
|
||||||
} catch (UnsatisfiedLinkError e)
|
|
||||||
{
|
|
||||||
System.err.println("");
|
|
||||||
System.err.println("");
|
|
||||||
System.err.println("** tryLoad(...) A *****************************");
|
|
||||||
System.err.println("");
|
|
||||||
e.printStackTrace();
|
|
||||||
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
System.load(libName);
|
|
||||||
return true;
|
|
||||||
} catch (UnsatisfiedLinkError ee)
|
|
||||||
{
|
|
||||||
System.err.println("");
|
|
||||||
System.err.println("");
|
|
||||||
System.err.println("** tryLoad(...) B *****************************");
|
|
||||||
System.err.println("");
|
|
||||||
ee.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void loadLibrary(String libName)
|
|
||||||
{
|
|
||||||
//if (OsDetect.getOperatingSystem()==OsType.LINUX){
|
|
||||||
// System.err.println("Linux erkannt. Patche java.library.path");
|
|
||||||
System.setProperty("java.library.path",String.format(".:%s",System.getProperty("java.library.path")));
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
if (tryLoad(libName))
|
|
||||||
return;
|
|
||||||
|
|
||||||
String platformLibName = calculateLibraryName(libName);
|
|
||||||
if (tryLoad(platformLibName))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (tryLoad(new File("").getAbsolutePath() + File.separator + platformLibName))
|
|
||||||
return;
|
|
||||||
|
|
||||||
String resourcePath;
|
|
||||||
|
|
||||||
System.err.println(String.format("NativeLoader: Platform dependent Name: %s",platformLibName));
|
|
||||||
|
|
||||||
resourcePath = "/native/" + platformLibName;
|
|
||||||
System.err.println(String.format("NativeLoader: Resource name: %s",resourcePath));
|
|
||||||
|
|
||||||
InputStream libInputStream = NativeLoader.class.getResourceAsStream(resourcePath);
|
|
||||||
if (libInputStream == null)
|
|
||||||
{
|
|
||||||
System.err.println("Resource wurde nicht gefunden!");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
File tempLibFile = File.createTempFile("native-", platformLibName);
|
|
||||||
FileOutputStream fos = new FileOutputStream(tempLibFile);
|
|
||||||
|
|
||||||
System.err.println(String.format("Using Temp File: %s",tempLibFile.getAbsoluteFile()));
|
|
||||||
System.err.println(String.format("Size: %d",libInputStream.available()));
|
|
||||||
|
|
||||||
byte[] buffer = new byte[libInputStream.available()];
|
|
||||||
libInputStream.read(buffer);
|
|
||||||
fos.write(buffer);
|
|
||||||
fos.close();
|
|
||||||
|
|
||||||
System.err.println(String.format("Loading: %s", tempLibFile.getAbsolutePath()));
|
|
||||||
System.load(tempLibFile.getAbsolutePath());
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue