Skip to content

Commit

Permalink
Modify gluegen src to load all native libs from main JAR automatically.
Browse files Browse the repository at this point in the history
Main JAR must have the following files in root:

gluegen-rt-<os_and_platform>.[dll,so,jnilib]

And "/native/<os_and_platform>/<required native libraries>"

Then it should all Just Work (TM).
  • Loading branch information
daviesian committed Jul 31, 2012
1 parent 4eafd00 commit 2a36f93
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 96 deletions.
55 changes: 2 additions & 53 deletions com/jogamp/common/jvm/JNILibLoaderBase.java
Expand Up @@ -149,29 +149,7 @@ public static synchronized void setLoadingAction(LoaderAction action) {
* @return true if the native JAR file loaded successful or were loaded already, false in case of an error
*/
public static final boolean addNativeJarLibs(Class<?> classFromJavaJar, String nativeJarBaseName) {
if(TempJarCache.isInitialized()) {
final String nativeJarName = nativeJarBaseName+"-natives-"+PlatformPropsImpl.os_and_arch+".jar";
final ClassLoader cl = classFromJavaJar.getClassLoader();
try {
URL jarUrlRoot = JarUtil.getURLDirname( JarUtil.getJarSubURL( classFromJavaJar.getName(), cl ) );
if(DEBUG) {
System.err.println("JNILibLoaderBase: addNativeJarLibs: "+nativeJarBaseName+": url-root "+jarUrlRoot);
}
URL nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, nativeJarName);
if(DEBUG) {
System.err.println("JNILibLoaderBase: addNativeJarLibs: "+nativeJarBaseName+": nativeJarURL "+nativeJarURL);
}
TempJarCache.addNativeLibs(classFromJavaJar, nativeJarURL, cl);
return true;
} catch (Exception e0) {
// IllegalArgumentException, IOException
System.err.println("Catched: "+e0.getMessage());
if(DEBUG) {
e0.printStackTrace();
}
}
}
return false;
return true;
}

/**
Expand All @@ -182,36 +160,7 @@ public static final boolean addNativeJarLibs(Class<?> classFromJavaJar, String n
* false in case of an error
*/
public static boolean addNativeJarLibs(Class<?> classFromJavaJar, String allNativeJarBaseName, String[] atomicNativeJarBaseNames) {
boolean res = false;
if(TempJarCache.isInitialized()) {
final ClassLoader cl = classFromJavaJar.getClassLoader();
try {
final String jarName = JarUtil.getJarBasename(classFromJavaJar.getName(), cl);
if(jarName!=null) {
if(!res && null != allNativeJarBaseName) {
// all-in-one variant 1st
res = JNILibLoaderBase.addNativeJarLibs(classFromJavaJar, allNativeJarBaseName);
}
if(!res && null != atomicNativeJarBaseNames) {
// atomic variant
res = true;
for(int i=0; res && i<atomicNativeJarBaseNames.length; i++) {
final String atomicNativeJarBaseName = atomicNativeJarBaseNames[i];
if(null != atomicNativeJarBaseName && atomicNativeJarBaseName.length()>0) {
res = JNILibLoaderBase.addNativeJarLibs(classFromJavaJar, atomicNativeJarBaseName);
}
}
}
}
} catch (Exception e0) {
// IllegalArgumentException, IOException
System.err.println("Catched: "+e0.getMessage());
if(DEBUG) {
e0.printStackTrace();
}
}
}
return res;
return true;
}

/**
Expand Down
24 changes: 19 additions & 5 deletions com/jogamp/common/os/Platform.java
Expand Up @@ -28,6 +28,8 @@

package com.jogamp.common.os;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
Expand Down Expand Up @@ -165,9 +167,10 @@ public enum ABIType {
static {
PlatformPropsImpl.initSingleton(); // just documenting the order of static initialization

USE_TEMP_JAR_CACHE = (OS_TYPE != OSType.ANDROID) && isRunningFromJarURL() &&
Debug.getBooleanProperty(useTempJarCachePropName, true, true);
USE_TEMP_JAR_CACHE = true;//(OS_TYPE != OSType.ANDROID) && isRunningFromJarURL() &&
//Debug.getBooleanProperty(useTempJarCachePropName, true, true);


loadGlueGenRTImpl();

JVMUtil.initSingleton(); // requires gluegen-rt, one-time init.
Expand All @@ -188,6 +191,12 @@ public enum ABIType {
machineDescription = md;
is32Bit = machineDescription.is32Bit();

try {
TempJarCache.addNativeLibs(null, JarUtil.getJarFileURL(Platform.class.getName(), ClassLoader.getSystemClassLoader()), ClassLoader.getSystemClassLoader());
} catch (Exception e) {
e.printStackTrace();
}

{
final ClassLoader cl = Platform.class.getClassLoader();
AWT_AVAILABLE = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
Expand All @@ -204,6 +213,7 @@ public Boolean run() {
}
}).booleanValue();
}

}

private Platform() {}
Expand All @@ -228,10 +238,14 @@ public Object run() {
if(USE_TEMP_JAR_CACHE && TempJarCache.initSingleton()) {
final String nativeJarName = libBaseName+"-natives-"+os_and_arch+".jar";
try {
final URL jarUrlRoot = JarUtil.getURLDirname(
JarUtil.getJarSubURL(Platform.class.getName(), cl) );
final URL nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, nativeJarName);
//final URL jarUrlRoot = JarUtil.getURLDirname(
// JarUtil.getJarSubURL(Platform.class.getName(), cl) );
//final URL nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, nativeJarName);

//final URL nativeJarURL = JarUtil.getJarFileURL(new URL("file:/c:/dev/"),"natives.jar");
final URL nativeJarURL = JarUtil.getJarFileURL(Platform.class.getName(), cl);
TempJarCache.bootstrapNativeLib(Platform.class, libBaseName, nativeJarURL, cl);

} catch (Exception e0) {
// IllegalArgumentException, IOException
System.err.println("Catched: "+e0.getMessage());
Expand Down
79 changes: 46 additions & 33 deletions com/jogamp/common/util/JarUtil.java
Expand Up @@ -45,6 +45,7 @@
import java.util.jar.JarFile;

import com.jogamp.common.os.NativeLibrary;
import com.jogamp.common.os.Platform;

import jogamp.common.Debug;

Expand Down Expand Up @@ -429,6 +430,15 @@ public static final int extract(File dest, Map<String, String> nativeLibMap,
continue;
}

if(isNativeLib && !entryName.contains("native/" + Platform.os_and_arch + "/"))
{
// This is a native library for another platform
if (DEBUG) {
System.err.println("JarUtil: JarEntry : " + entryName + " native-lib is not for this platform. Skipping.");
}
continue;
}

final boolean isClassFile = entryName.endsWith(".class");
if(isClassFile && !extractClassFiles) {
if (DEBUG) {
Expand Down Expand Up @@ -456,39 +466,42 @@ public static final int extract(File dest, Map<String, String> nativeLibMap,
}

final File destFile = new File(dest, entryName);
if(isDir) {
if (DEBUG) {
System.err.println("JarUtil: MKDIR: " + entryName + " -> " + destFile );
}
destFile.mkdir();
} else {
final File destFolder = new File(destFile.getParent());
if(!destFolder.exists()) {
if (DEBUG) {
System.err.println("JarUtil: MKDIR (parent): " + entryName + " -> " + destFolder );
}
destFolder.mkdir();
}
final InputStream in = new BufferedInputStream(jarFile.getInputStream(entry));
final OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
int numBytes = -1;
try {
numBytes = IOUtil.copyStream2Stream(in, out, -1);
} finally {
in.close();
out.close();
}
boolean addedAsNativeLib = false;
if (numBytes>0) {
num++;
if (isNativeLib && ( isRootEntry || !nativeLibMap.containsKey(libBaseName) ) ) {
nativeLibMap.put(libBaseName, destFile.getAbsolutePath());
addedAsNativeLib = true;
}
}
if (DEBUG) {
System.err.println("JarUtil: EXTRACT["+num+"]: [" + libBaseName + " -> ] " + entryName + " -> " + destFile + ": "+numBytes+" bytes, addedAsNativeLib: "+addedAsNativeLib);
}
if (!destFile.exists()) {
if(isDir) {
if (DEBUG) {
System.err.println("JarUtil: MKDIR: " + entryName + " -> " + destFile );
}
destFile.mkdir();
} else {
final File destFolder = new File(destFile.getParent());
if(!destFolder.exists()) {
if (DEBUG) {
System.err.println("JarUtil: MKDIR (parent): " + entryName + " -> " + destFolder );
}
destFolder.mkdirs();
}
//System.out.println("Writing to " + destFile.toString());
final InputStream in = new BufferedInputStream(jarFile.getInputStream(entry));
final OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
int numBytes = -1;
try {
numBytes = IOUtil.copyStream2Stream(in, out, -1);
} finally {
in.close();
out.close();
}
boolean addedAsNativeLib = false;
if (numBytes>0) {
num++;
if (isNativeLib && ( isRootEntry || !nativeLibMap.containsKey(libBaseName) ) ) {
nativeLibMap.put(libBaseName, destFile.getAbsolutePath());
addedAsNativeLib = true;
}
}
if (DEBUG) {
System.err.println("JarUtil: EXTRACT["+num+"]: [" + libBaseName + " -> ] " + entryName + " -> " + destFile + ": "+numBytes+" bytes, addedAsNativeLib: "+addedAsNativeLib);
}
}
}
}
return num;
Expand Down
13 changes: 8 additions & 5 deletions com/jogamp/common/util/cache/TempJarCache.java
Expand Up @@ -48,6 +48,7 @@
import jogamp.common.Debug;

import com.jogamp.common.os.NativeLibrary;
import com.jogamp.common.os.Platform;
import com.jogamp.common.util.IOUtil;
import com.jogamp.common.util.JarUtil;
import com.jogamp.common.util.SecurityUtil;
Expand Down Expand Up @@ -202,8 +203,9 @@ public static final void addNativeLibs(Class<?> certClass, URL jarURL, ClassLoad
if(DEBUG) {
System.err.println("TempJarCache: addNativeLibs: "+jarURL+": nativeJar "+jarFile.getName());
}
validateCertificates(certClass, jarFile);
JarUtil.extract(tmpFileCache.getTempDir(), nativeLibMap, jarFile,
//validateCertificates(certClass, jarFile);
File s = tmpFileCache.getTempDir();
JarUtil.extract(s, nativeLibMap, jarFile,
true, false, false);
nativeLibJars.add(jarURL);
}
Expand Down Expand Up @@ -376,9 +378,10 @@ public static final void bootstrapNativeLib(Class<?> certClass, String libBaseNa

if( entryName.indexOf('/') == -1 &&
entryName.indexOf(File.separatorChar) == -1 &&
entryName.indexOf(libBaseName) >= 0 )
entryName.indexOf(libBaseName) >= 0 &&
entryName.indexOf(Platform.os_and_arch) >= 0)
{
final File destFile = new File(tmpFileCache.getTempDir(), entryName);
final File destFile = new File(tmpFileCache.getTempDir(), entryName.replace("-" + Platform.os_and_arch, ""));
final InputStream in = new BufferedInputStream(jarFile.getInputStream(entry));
final OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
int numBytes = 0;
Expand All @@ -393,7 +396,7 @@ public static final void bootstrapNativeLib(Class<?> certClass, String libBaseNa
} finally { in.close(); out.close(); }
if (numBytes>0) {
nativeLibMap.put(libBaseName, destFile.getAbsolutePath());
nativeLibJars.add(jarURL);
//nativeLibJars.add(jarURL);
}
}
}
Expand Down

0 comments on commit 2a36f93

Please sign in to comment.