Skip to content

Commit

Permalink
adding missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
redrezo committed Jul 28, 2020
1 parent 1ce0e51 commit 2daab29
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
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);
}
}

}
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());
}
}

0 comments on commit 2daab29

Please sign in to comment.