From 2ea5215d7f8c9e948b7e2a767ba80c184c17a996 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Fri, 1 Apr 2016 22:44:41 +0200 Subject: [PATCH] Initialer Commit --- .classpath | 6 + .gitignore | 4 + .project | 17 +++ .settings/org.eclipse.jdt.core.prefs | 11 ++ src/org/hwo/platform/Platform.java | 53 +++++++++ src/org/hwo/platform/Ressource.java | 40 +++++++ .../hwo/platform/natives/NativeLoader.java | 110 ++++++++++++++++++ 7 files changed, 241 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 src/org/hwo/platform/Platform.java create mode 100644 src/org/hwo/platform/Ressource.java create mode 100644 src/org/hwo/platform/natives/NativeLoader.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fb565a5 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6669990 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +bin/* + +/bin diff --git a/.project b/.project new file mode 100644 index 0000000..de22961 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + org.hwo.platform + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..7341ab1 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/src/org/hwo/platform/Platform.java b/src/org/hwo/platform/Platform.java new file mode 100644 index 0000000..3ac2f7b --- /dev/null +++ b/src/org/hwo/platform/Platform.java @@ -0,0 +1,53 @@ +package org.hwo.platform; + +public class Platform { + + public enum OsType { UNKNOWN, LINUX, OSX, WINDOWS}; + public enum Bitness { B32, B64 }; + + public static OsType getOperatingSystem() + { + if (System.getProperty("os.name").equals("Mac OS X")) + return OsType.OSX; + if (System.getProperty("os.name").equals("Linux")) + return OsType.LINUX; + if (System.getProperty("os.name").startsWith("Windows")) + return OsType.WINDOWS; + + return OsType.UNKNOWN; + } + + public static Bitness getBitness(){ + String arch = System.getProperty("os.arch"); + if (arch == null) { + return null; + } + + if (arch.equals("x86")){ + return Bitness.B32; + } else if (arch.equals("i386")){ + return Bitness.B32; + } else if (arch.equals("xmd64")){ + return Bitness.B64; + } else if (arch.equals("amd64")){ + return Bitness.B64; + } + return null; + } + + public static String PlatformName(){ + OsType ost = getOperatingSystem(); + Bitness bit = getBitness(); + + return String.format("%s%s", + (ost == OsType.LINUX) ? "linux" : + (ost == OsType.OSX) ? "osx" : + (ost == OsType.WINDOWS) ? "mswin" : + "unknown", + (bit == Bitness.B32) ? "32" : + (bit == Bitness.B64) ? "64" : + "XX" + ); + } + +} diff --git a/src/org/hwo/platform/Ressource.java b/src/org/hwo/platform/Ressource.java new file mode 100644 index 0000000..f4a0e9c --- /dev/null +++ b/src/org/hwo/platform/Ressource.java @@ -0,0 +1,40 @@ +package org.hwo.platform; + +import java.io.FileOutputStream; +import java.io.InputStream; + +import org.hwo.platform.natives.NativeLoader; + +public class Ressource { + + public static boolean extract(Class clazz,String resource,String filename){ + return extract( + clazz.getResourceAsStream(resource), + filename + ); + } + + public static boolean extract(String resource,String filename){ + return extract( + NativeLoader.class.getResourceAsStream(resource), + filename + ); + } + + private static boolean extract(InputStream in,String filename){ + try { + FileOutputStream fos = new FileOutputStream(filename); + byte[] buffer = new byte[in.available()]; + in.read(buffer); + fos.write(buffer); + fos.close(); + + return true; + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + +} diff --git a/src/org/hwo/platform/natives/NativeLoader.java b/src/org/hwo/platform/natives/NativeLoader.java new file mode 100644 index 0000000..ac66d0f --- /dev/null +++ b/src/org/hwo/platform/natives/NativeLoader.java @@ -0,0 +1,110 @@ +package org.hwo.platform.natives; + +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.platform.Platform; +import org.hwo.platform.Platform.Bitness; +import org.hwo.platform.Platform.OsType; +import org.hwo.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)); + } + + /* + * 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.err.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.err.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 nativeResourceName = nativeResourceName(libName); + try + { + System.load(nativeResourceName); + System.err.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.err.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"))); + } + +}