Blame SOURCES/0006-Simplify-shared-lib-extraction.patch

c6cb01
From 3ea58b7374f5451e693f09f9295e210006d1e7ff Mon Sep 17 00:00:00 2001
c6cb01
From: Hiram Chirino <hiram@hiramchirino.com>
c6cb01
Date: Mon, 6 May 2013 09:49:55 -0400
c6cb01
Subject: [PATCH 6/6] Simplify shared lib extraction.
c6cb01
c6cb01
---
c6cb01
 .../org/fusesource/hawtjni/runtime/Library.java    | 79 ++++++++--------------
c6cb01
 1 file changed, 29 insertions(+), 50 deletions(-)
c6cb01
c6cb01
diff --git a/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime/Library.java b/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime/Library.java
c6cb01
index c23081d..422bd2f 100755
c6cb01
--- a/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime/Library.java
c6cb01
+++ b/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime/Library.java
c6cb01
@@ -9,13 +9,11 @@
c6cb01
  *******************************************************************************/
c6cb01
 package org.fusesource.hawtjni.runtime;
c6cb01
 
c6cb01
-import java.io.File;
c6cb01
-import java.io.FileOutputStream;
c6cb01
-import java.io.IOException;
c6cb01
-import java.io.InputStream;
c6cb01
+import java.io.*;
c6cb01
 import java.net.MalformedURLException;
c6cb01
 import java.net.URL;
c6cb01
 import java.util.ArrayList;
c6cb01
+import java.util.Random;
c6cb01
 import java.util.regex.Pattern;
c6cb01
 
c6cb01
 /**
c6cb01
@@ -206,16 +204,19 @@ public class Library {
c6cb01
     private boolean exractAndLoad(ArrayList<String> errors, String version, String customPath, String resourcePath) {
c6cb01
         URL resource = classLoader.getResource(resourcePath);
c6cb01
         if( resource !=null ) {
c6cb01
-            
c6cb01
+
c6cb01
             String libName = name + "-" + getBitModel();
c6cb01
             if( version !=null) {
c6cb01
                 libName += "-" + version;
c6cb01
             }
c6cb01
-            
c6cb01
+            String []libNameParts = map(libName).split("\\.");
c6cb01
+            String prefix = libNameParts[0]+"-";
c6cb01
+            String suffix = "."+libNameParts[1];
c6cb01
+
c6cb01
             if( customPath!=null ) {
c6cb01
                 // Try to extract it to the custom path...
c6cb01
-                File target = file(customPath, map(libName));
c6cb01
-                if( extract(errors, resource, target) ) {
c6cb01
+                File target = extract(errors, resource, prefix, suffix, file(customPath));
c6cb01
+                if( target!=null ) {
c6cb01
                     if( load(errors, target) ) {
c6cb01
                         return true;
c6cb01
                     }
c6cb01
@@ -224,8 +225,8 @@ public class Library {
c6cb01
             
c6cb01
             // Fall back to extracting to the tmp dir
c6cb01
             customPath = System.getProperty("java.io.tmpdir");
c6cb01
-            File target = file(customPath, map(libName));
c6cb01
-            if( extract(errors, resource, target) ) {
c6cb01
+            File target = extract(errors, resource, prefix, suffix, file(customPath));
c6cb01
+            if( target!=null ) {
c6cb01
                 if( load(errors, target) ) {
c6cb01
                     return true;
c6cb01
                 }
c6cb01
@@ -259,67 +260,45 @@ public class Library {
c6cb01
         return libName;
c6cb01
     }
c6cb01
 
c6cb01
-    private boolean extract(ArrayList<String> errors, URL source, File target) {
c6cb01
-        FileOutputStream os = null;
c6cb01
-        InputStream is = null;
c6cb01
-        boolean extracting = false;
c6cb01
+    private File extract(ArrayList<String> errors, URL source, String prefix, String suffix, File directory) {
c6cb01
+        File target = null;
c6cb01
         try {
c6cb01
-            if (!target.exists() || isStale(source, target) ) {
c6cb01
+            FileOutputStream os = null;
c6cb01
+            InputStream is = null;
c6cb01
+            try {
c6cb01
+                target = File.createTempFile(prefix, suffix, directory);
c6cb01
                 is = source.openStream();
c6cb01
                 if (is != null) {
c6cb01
                     byte[] buffer = new byte[4096];
c6cb01
                     os = new FileOutputStream(target);
c6cb01
-                    extracting = true;
c6cb01
                     int read;
c6cb01
                     while ((read = is.read(buffer)) != -1) {
c6cb01
                         os.write(buffer, 0, read);
c6cb01
                     }
c6cb01
-                    os.close();
c6cb01
-                    is.close();
c6cb01
                     chmod("755", target);
c6cb01
                 }
c6cb01
+                target.deleteOnExit();
c6cb01
+                return target;
c6cb01
+            } finally {
c6cb01
+                close(os);
c6cb01
+                close(is);
c6cb01
             }
c6cb01
         } catch (Throwable e) {
c6cb01
-            try {
c6cb01
-                if (os != null)
c6cb01
-                    os.close();
c6cb01
-            } catch (IOException e1) {
c6cb01
-            }
c6cb01
-            try {
c6cb01
-                if (is != null)
c6cb01
-                    is.close();
c6cb01
-            } catch (IOException e1) {
c6cb01
-            }
c6cb01
-            if (extracting && target.exists())
c6cb01
+            if( target!=null ) {
c6cb01
                 target.delete();
c6cb01
+            }
c6cb01
             errors.add(e.getMessage());
c6cb01
-            return false;
c6cb01
         }
c6cb01
-        return true;
c6cb01
+        return null;
c6cb01
     }
c6cb01
 
c6cb01
-    private boolean isStale(URL source, File target) {
c6cb01
-        
c6cb01
-        if( source.getProtocol().equals("jar") ) {
c6cb01
-            // unwrap the jar protocol...
c6cb01
+    static private void close(Closeable file) {
c6cb01
+        if(file!=null) {
c6cb01
             try {
c6cb01
-                String parts[] = source.getFile().split(Pattern.quote("!"));
c6cb01
-                source = new URL(parts[0]);
c6cb01
-            } catch (MalformedURLException e) {
c6cb01
-                return false;
c6cb01
-            }
c6cb01
-        }
c6cb01
-        
c6cb01
-        File sourceFile=null;
c6cb01
-        if( source.getProtocol().equals("file") ) {
c6cb01
-            sourceFile = new File(source.getFile());
c6cb01
-        }
c6cb01
-        if( sourceFile!=null && sourceFile.exists() ) {
c6cb01
-            if( sourceFile.lastModified() > target.lastModified() ) {
c6cb01
-                return true;
c6cb01
+                file.close();
c6cb01
+            } catch (Exception ignore) {
c6cb01
             }
c6cb01
         }
c6cb01
-        return false;
c6cb01
     }
c6cb01
 
c6cb01
     private void chmod(String permision, File path) {
c6cb01
-- 
c6cb01
1.8.1.4
c6cb01