26package org.jau.pkg.cache;
29import java.io.IOException;
30import java.net.URISyntaxException;
31import java.security.cert.Certificate;
32import java.util.HashMap;
34import java.util.jar.JarFile;
36import org.jau.net.Uri;
37import org.jau.pkg.JarUtil;
38import org.jau.sec.SecurityUtil;
39import org.jau.sys.Debug;
40import org.jau.sys.JNILibrary;
49 private static final boolean DEBUG =
Debug.
debug(
"TempJarCache");
56 private static Map<String, String> nativeLibMap;
62 return null != o2 ? compareTo(o2) >= 0 :
false;
65 private static boolean testLoadState(
final LoadState has,
final LoadState exp) {
73 private static Map<Uri, LoadState> nativeLibJars;
74 private static Map<Uri, LoadState> classFileJars;
75 private static Map<Uri, LoadState> resourceFileJars;
77 private static TempFileCache tmpFileCache;
79 private static volatile boolean staticInitError =
false;
80 private static volatile boolean staticTempIsExecutable =
true;
81 private static volatile boolean isInit =
false;
94 if(!staticInitError) {
96 staticInitError = !tmpFileCache.isValid(
false);
97 staticTempIsExecutable = tmpFileCache.isValid(
true);
100 if(!staticInitError) {
102 nativeLibMap =
new HashMap<String, String>();
103 nativeLibJars =
new HashMap<Uri, LoadState>();
104 classFileJars =
new HashMap<Uri, LoadState>();
105 resourceFileJars =
new HashMap<Uri, LoadState>();
108 final File tempDir =
null != tmpFileCache ? tmpFileCache.getTempDir() :
null;
109 final String tempDirAbsPath =
null != tempDir ? tempDir.getAbsolutePath() :
null;
110 System.err.println(
"TempJarCache.initSingleton(): ok "+(
false==staticInitError)+
", "+ tempDirAbsPath+
", executable "+staticTempIsExecutable);
116 return !staticInitError;
159 private static boolean isInitializedImpl() {
175 return isInitializedImpl() && !staticInitError && ( !forExecutables || staticTempIsExecutable );
181 static void checkInitialized(
final boolean forExecutables) {
182 if(!isInitializedImpl()) {
183 throw new RuntimeException(
"initSingleton() has to be called first.");
185 if(staticInitError) {
186 throw new RuntimeException(
"initSingleton() failed.");
188 if( forExecutables && !staticTempIsExecutable ) {
189 final File tempDir =
null != tmpFileCache ? tmpFileCache.getTempDir() :
null;
190 final String tempDirAbsPath =
null != tempDir ? tempDir.getAbsolutePath() :
null;
191 throw new RuntimeException(
"TempJarCache folder not suitable for executables: "+tempDirAbsPath);
200 checkInitialized(
false);
212 checkInitialized(
false);
214 throw new IllegalArgumentException(
"jarUri is null");
216 return testLoadState(nativeLibJars.get(jarUri), exp);
227 checkInitialized(
false);
229 throw new IllegalArgumentException(
"jarUri is null");
231 return testLoadState(classFileJars.get(jarUri), exp);
243 checkInitialized(
false);
245 throw new IllegalArgumentException(
"jarUri is null");
247 return testLoadState(resourceFileJars.get(jarUri), exp);
263 public synchronized static final boolean addNativeLibs(
final Class<?> certClass,
final Uri jarUri,
final String nativeLibraryPath)
throws IOException, SecurityException, IllegalArgumentException, URISyntaxException {
264 checkInitialized(
true);
265 final LoadState nativeLibJarsLS = nativeLibJars.get(jarUri);
270 System.err.println(
"TempJarCache: addNativeLibs: "+jarUri+
": nativeJar "+jarFile.getName()+
" (NEW)");
272 validateCertificates(certClass, jarFile);
273 final int num =
JarUtil.
extract(tmpFileCache.getTempDir(), nativeLibMap, jarFile, nativeLibraryPath,
true,
false,
false);
278 System.err.println(
"TempJarCache: addNativeLibs: "+jarUri+
": nativeJar "+jarUri+
" (REUSE)");
282 throw new IOException(
"TempJarCache: addNativeLibs: "+jarUri+
", previous load attempt failed");
299 public synchronized static final void addClasses(
final Class<?> certClass,
final Uri jarUri)
throws IOException, SecurityException, IllegalArgumentException, URISyntaxException {
300 checkInitialized(
false);
301 final LoadState classFileJarsLS = classFileJars.get(jarUri);
306 System.err.println(
"TempJarCache: addClasses: "+jarUri+
": nativeJar "+jarFile.getName());
308 validateCertificates(certClass, jarFile);
310 null ,
false,
true,
false);
313 throw new IOException(
"TempJarCache: addClasses: "+jarUri+
", previous load attempt failed");
329 public synchronized static final void addResources(
final Class<?> certClass,
final Uri jarUri)
throws IOException, SecurityException, IllegalArgumentException, URISyntaxException {
330 checkInitialized(
false);
331 final LoadState resourceFileJarsLS = resourceFileJars.get(jarUri);
336 System.err.println(
"TempJarCache: addResources: "+jarUri+
": nativeJar "+jarFile.getName());
338 validateCertificates(certClass, jarFile);
340 null ,
false,
false,
true);
343 throw new IOException(
"TempJarCache: addResources: "+jarUri+
", previous load attempt failed");
362 public synchronized static final void addAll(
final Class<?> certClass,
final Uri jarUri)
throws IOException, SecurityException, IllegalArgumentException, URISyntaxException {
363 checkInitialized(
false);
365 throw new IllegalArgumentException(
"jarUri is null");
367 final LoadState nativeLibJarsLS = nativeLibJars.get(jarUri);
368 final LoadState classFileJarsLS = classFileJars.get(jarUri);
369 final LoadState resourceFileJarsLS = resourceFileJars.get(jarUri);
374 final boolean extractNativeLibraries = staticTempIsExecutable && !testLoadState(nativeLibJarsLS,
LoadState.
LOADED);
375 final boolean extractClassFiles = !testLoadState(classFileJarsLS,
LoadState.
LOADED);
376 final boolean extractOtherFiles = !testLoadState(resourceFileJarsLS,
LoadState.
LOOKED_UP);
379 if(extractNativeLibraries) {
382 if(extractClassFiles) {
385 if(extractOtherFiles) {
391 System.err.println(
"TempJarCache: addAll: "+jarUri+
": nativeJar "+jarFile.getName());
393 validateCertificates(certClass, jarFile);
395 null , extractNativeLibraries, extractClassFiles, extractOtherFiles);
398 if(extractNativeLibraries) {
401 if(extractClassFiles) {
404 if(extractOtherFiles) {
410 throw new IOException(
"TempJarCache: addAll: "+jarUri+
", previous load attempt failed");
421 public synchronized static final String
findLibrary(
final String libName) {
422 checkInitialized(
false);
423 if( !staticTempIsExecutable ) {
427 String path = nativeLibMap.get(libName);
431 final File f =
new File(tmpFileCache.getTempDir(), libName);
433 path = f.getAbsolutePath();
461 public synchronized static final String
findResource(
final String name) {
462 checkInitialized(
false);
463 final File f =
new File(tmpFileCache.getTempDir(), name);
465 return f.getAbsolutePath();
477 public synchronized static final Uri getResourceUri(
final String name)
throws URISyntaxException {
478 checkInitialized(
false);
479 final File f =
new File(tmpFileCache.getTempDir(), name);
486 private static void validateCertificates(
final Class<?> certClass,
final JarFile jarFile)
throws IOException, SecurityException {
487 if(
null == certClass) {
488 throw new IllegalArgumentException(
"certClass is null");
491 if(
null != rootCerts ) {
498 System.err.println(
"TempJarCache: validateCertificates: OK - Matching rootCerts in given class "+certClass.getName()+
", nativeJar "+jarFile.getName());
501 System.err.println(
"TempJarCache: validateCertificates: OK - No rootCerts in given class "+certClass.getName()+
", nativeJar "+jarFile.getName());
This class implements an immutable Uri as defined by RFC 2396.
static Uri valueOf(final File file)
Creates a new Uri instance using the given File instance.
static JarFile getJarFile(final String clazzBinName, final ClassLoader cl)
static final int extract(final File dest, final Map< String, String > nativeLibMap, final JarFile jarFile, final String nativeLibraryPath, final boolean extractNativeLibraries, final boolean extractClassFiles, final boolean extractOtherFiles)
Extract the files of the given jar file.
static final void validateCertificates(final Certificate[] rootCerts, final JarFile jarFile)
Validate the certificates for each native Lib in the jar file.
static boolean initSingleton()
Documented way to kick off static initialization.
Static Jar file cache handler using an underlying instance of TempFileCache, see getTempFileCache().
static boolean isInitialized(final boolean forExecutables)
static synchronized final void addAll(final Class<?> certClass, final Uri jarUri)
Adds all types, native libraries, class files and other files (resources) if not yet added.
static synchronized final void addClasses(final Class<?> certClass, final Uri jarUri)
Adds native classes, if not yet added.
static synchronized final String findResource(final String name)
TODO class access pending needs Classloader.defineClass(..) access, ie.
static synchronized final boolean addNativeLibs(final Class<?> certClass, final Uri jarUri, final String nativeLibraryPath)
Adds native libraries, if not yet added.
static synchronized boolean checkClasses(final Uri jarUri, final LoadState exp)
static boolean initSingleton()
Documented way to kick off static initialization.
static synchronized boolean checkResources(final Uri jarUri, final LoadState exp)
static synchronized final String findLibrary(final String libName)
If isInitialized(true) is false due to lack of executable support only, this method always returns fa...
static synchronized boolean checkNativeLibs(final Uri jarUri, final LoadState exp)
static synchronized final Uri getResourceUri(final String name)
Similar to ClassLoader#getResource(String).
static synchronized final void addResources(final Class<?> certClass, final Uri jarUri)
Adds native resources, if not yet added.
static TempFileCache getTempFileCache()
static final Certificate[] getCerts(final Class<?> clz)
Helper routines for logging and debugging.
static final boolean debug(final String subcomponent)
Static JNI Native Libraries handler.
static final String isValidNativeLibraryName(final String libName, final boolean isLowerCaseAlready)
Comparison of prefix and suffix of the given libName's basename is performed case insensitive
boolean compliesWith(final LoadState o2)