package com.jogamp.common.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.Certificate;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import com.jogamp.common.os.NativeLibrary;
import com.jogamp.common.util.IOUtil;
import jogamp.common.Debug;
public class JarUtil {
private static final boolean DEBUG = Debug.debug("JarUtil");
static {
System.out.println("using modified JarUtil");
}
/**
* @param clazzBinName com.jogamp.common.util.cache.TempJarCache
* @param cl
* @return gluegen-rt.jar
* @throws IOException
* @see {@link IOUtil#getClassURL(String, ClassLoader)}
*/
public static String getJarName(String clazzBinName, ClassLoader cl) throws IOException {
URL url = IOUtil.getClassURL(clazzBinName, cl);
if(null != url) {
String urlS = url.toExternalForm();
// from
// jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class
// to
// gluegen-rt.jar
urlS = urlS.substring(0, urlS.lastIndexOf('!')); // exclude !/
return urlS.substring(urlS.lastIndexOf('/')+1); // just the jar name
}
return null;
}
/**
* @param clazzBinName com.jogamp.common.util.cache.TempJarCache
* @param cl
* @return jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/
* @throws IOException
* @see {@link IOUtil#getClassURL(String, ClassLoader)}
*/
// public static URL getJarURL(String clazzBinName, ClassLoader cl) throws IOException {
// URL url = IOUtil.getClassURL(clazzBinName, cl);
// if(null != url) {
// String urlS = url.toExternalForm();
// // from
// // jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class
// // to
// // jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/
// urlS = urlS.substring(0, urlS.lastIndexOf('!')+2); // include !/
// return new URL(urlS);
// }
// return null;
// }
public static URL getJarURL(String clazzBinName, ClassLoader cl) throws IOException {
URL url = IOUtil.getClassURL(clazzBinName, cl);
if(null != url) {
String urlS = url.toExternalForm();
System.out.println("extForm: "+urlS);
// from
// jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class
// to
// jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/
int idx = urlS.lastIndexOf('!');
if (idx == -1) {
} else {
urlS = urlS.substring(0, urlS.lastIndexOf('!')+2); // include !/
}
System.out.println(urlS);
return new URL(urlS);
}
return null;
}
/**
*
* @param jarURL jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class
* @return file:/usr/local/projects/JOGL/gluegen/build-x86_64/
* @throws IOException
*/
// public static URL getJarURLDirname(URL jarURL) throws IOException {
// String urlS = jarURL.toExternalForm();
// // from
// // jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class
// // to
// // jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar
// urlS = urlS.substring(0, urlS.lastIndexOf('!')); // exclude !/
//
// // from
// // jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar
// // to
// // file:/usr/local/projects/JOGL/gluegen/build-x86_64/
// urlS = urlS.substring(4, urlS.lastIndexOf('/')+1); // include / exclude jar:
// return new URL(urlS);
// }
public static URL getJarURLDirname(URL jarURL) throws IOException {
String urlS = jarURL.toExternalForm();
// from
// jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class
// to
// jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar
int idx = urlS.lastIndexOf('!');
if (idx == -1) {
} else {
urlS = urlS.substring(0, idx); // exclude !/
}
// from
// jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar
// to
// file:/usr/local/projects/JOGL/gluegen/build-x86_64/
idx = urlS.lastIndexOf('/');
if (idx == -1) {
} else {
urlS = urlS.substring(0, idx+1); // include / exclude jar:
}
urlS.replace("jar:file:", "file:");
return new URL(urlS);
}
/**
*
* @param baseUrl file:/usr/local/projects/JOGL/gluegen/build-x86_64/
* @param jarFileName gluegen-rt.jar
* @return jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/
* @throws IOException
*/
public static URL getJarURL(URL baseUrl, String jarFileName) throws IOException {
return new URL("jar:"+baseUrl.toExternalForm()+jarFileName+"!/");
}
/**
*
* @param clazzBinName com.jogamp.common.util.cache.TempJarCache
* @param cl domain
* @return JarFile containing the named class within the given ClassLoader
* @throws IOException
* @see {@link #getJarURL(String, ClassLoader)}
*/
public static JarFile getJarFile(String clazzBinName, ClassLoader cl) throws IOException {
return getJarFile(getJarURL(clazzBinName, cl), cl);
}
/**
*
* @param jarURL jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/
* @param cl domain
* @return JarFile as named by URL within the given ClassLoader
* @throws IOException
*/
public static JarFile getJarFile(URL jarUrl, ClassLoader cl) throws IOException {
if(null != jarUrl) {
URLConnection urlc = jarUrl.openConnection();
if(urlc instanceof JarURLConnection) {
JarURLConnection jarConnection = (JarURLConnection)jarUrl.openConnection();
JarFile jarFile = jarConnection.getJarFile();
return jarFile;
}
}
return null;
}
/**
* Return a map from native-lib-base-name to entry-name.
*/
public static Map
* If extractNativeLibraries
is true,
* native libraries are added to the given nativeLibMap
* with the base name to temp file location.
* A file is identified as a native library,
* if it's name complies with the running platform's native library naming scheme.
* Root entries are favored over non root entries in case of naming collisions.
* Example on a Unix like machine:
*
* mylib.jar!/sub1/libsour.so -> sour (mapped, unique name)
* mylib.jar!/sub1/libsweet.so (dropped, root entry favored)
* mylib.jar!/libsweet.so -> sweet (mapped, root entry favored)
* mylib.jar!/sweet.dll -> (dropped, not a unix library name)
*
*
* In order to be compatible with Java Web Start, we need
* to extract all root entries from the jar file.
* In this case, set all flags to true extractNativeLibraries
.
* extractClassFiles
, extractOtherFiles
.
*
Certificate[] rootCerts = Something.class.getProtectionDomain(). getCodeSource().getCertificates();*/ public static final void validateCertificates(Certificate[] rootCerts, JarFile jarFile) throws IOException, SecurityException { if (DEBUG) { System.err.println("JarUtil: validateCertificates: "+jarFile.getName()); } if (rootCerts == null || rootCerts.length == 0) { throw new IllegalArgumentException("Null certificates passed"); } byte[] buf = new byte[1024]; Enumeration