forked from eclipse-efx/efxclipse-drift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
org.eclipse.fx.drift/src/main/java/org/eclipse/fx/drift/util/ManifestUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.eclipse.fx.drift.util; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.jar.Attributes; | ||
import java.util.jar.Manifest; | ||
|
||
import org.eclipse.fx.drift.internal.Versioning; | ||
|
||
public class ManifestUtil { | ||
|
||
private static URL getManifestUrl(Class<?> cls) { | ||
try { | ||
String shortName = cls.getSimpleName() + ".class"; | ||
String fullPath = cls.getName().replace('/', '.') + ".class"; | ||
URL clsUrl = Versioning.class.getResource(shortName); | ||
return new URL(clsUrl.toString().replace(fullPath, "META-INF/MANIFEST.MF")); | ||
} | ||
catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static String getManifestEntry(Class<?> context, String name) { | ||
URL manifestUrl = getManifestUrl(context); | ||
try (InputStream in = manifestUrl.openStream()) { | ||
Manifest manifest = new Manifest(in); | ||
Attributes attrs = manifest.getMainAttributes(); | ||
return attrs.getValue(name); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
org.eclipse.fx.drift/src/main/java/org/eclipse/fx/drift/util/NativeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.eclipse.fx.drift.util; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import org.eclipse.fx.drift.internal.Log; | ||
import org.eclipse.fx.drift.internal.Versioning; | ||
|
||
public class NativeUtil { | ||
|
||
private static final boolean USE_JAVA_LIBRARY_PATH = Boolean.getBoolean("driftfx.use.java.library.path"); | ||
|
||
private static String OS = null; | ||
|
||
static boolean osgi = false; | ||
|
||
public static void useOsgiEnvironment() { | ||
osgi = true; | ||
} | ||
|
||
public static String getOsName() { | ||
if (OS == null) { | ||
OS = System.getProperty("os.name"); | ||
} | ||
return OS; | ||
} | ||
|
||
public static boolean isWindows() { | ||
return getOsName().toLowerCase().contains("windows"); | ||
} | ||
|
||
public static boolean isLinux() { | ||
return getOsName().toLowerCase().contains("linux"); | ||
} | ||
|
||
public static boolean isMacOs() { | ||
return getOsName().toLowerCase().contains("mac"); | ||
} | ||
|
||
public static void loadLibrary(Class<?> context, String libname) { | ||
if (USE_JAVA_LIBRARY_PATH || osgi) { | ||
// osgi will take care of it | ||
System.loadLibrary(libname); | ||
} | ||
else { | ||
// we need to make it happen | ||
|
||
String filename = getFilename(libname); | ||
Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")); | ||
Path extractPath = tmpDir.resolve("driftfx").resolve(ManifestUtil.getManifestEntry(context, "Bundle-Version")).resolve(filename); | ||
|
||
String resourceName = "/native/" + filename; | ||
URL url = context.getResource(resourceName); | ||
Log.debug("Resource Lookup: name: " + resourceName + ", context: " + context + " => " + url); | ||
|
||
try (InputStream in = context.getResourceAsStream("/native/" + filename)) { | ||
extract(in, extractPath); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
System.load(extractPath.toString()); | ||
} | ||
} | ||
|
||
private static Path extract(InputStream in, Path extractPath) throws IOException { | ||
Files.createDirectories(extractPath.getParent()); | ||
if (!Files.exists(extractPath)) { | ||
long bytes = Files.copy(in, extractPath); | ||
if (bytes == 0) { | ||
throw new IOException("library has no size!"); | ||
} | ||
} | ||
return extractPath; | ||
} | ||
|
||
private static String getFilename(String libname) { | ||
if (isWindows()) { | ||
return libname + ".dll"; | ||
} | ||
if (isLinux()) { | ||
return "lib" + libname + ".so"; | ||
} | ||
if (isMacOs()) { | ||
return "lib" + libname + ".dylib"; | ||
} | ||
throw new RuntimeException("os not supported: " + getOsName()); | ||
} | ||
} |