You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/09/22 15:11:35 UTC

svn commit: r817647 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/ java/org/apache/commons/runtime/util/ native/include/ native/os/unix/ native/os/win32/

Author: mturk
Date: Tue Sep 22 13:11:34 2009
New Revision: 817647

URL: http://svn.apache.org/viewvc?rev=817647&view=rev
Log:
Fix library load from resource

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Library.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Loader.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Native.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/subproc.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Library.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Library.java?rev=817647&r1=817646&r2=817647&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Library.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Library.java Tue Sep 22 13:11:34 2009
@@ -70,7 +70,7 @@
     private static File extractResource(InputStream is, String name)
         throws IllegalArgumentException, IOException
     {
-        File f = new File(Loader.getPath(), System.mapLibraryName(name));
+        File f = Loader.libFile(System.mapLibraryName(name));
         try {
             byte [] buf = new byte[4096];
             FileOutputStream os = new FileOutputStream(f);
@@ -83,12 +83,23 @@
                     break;
             }
             os.close();
-            /* TODO: For windows add this to cleanup subproc
+            /* Mark the extracted file for delete on JVM exit.
+             * Although windows platform cannot delete this file
+             * if library is successfully loaded it will succeed if
+             * load fails.
              */
             f.deleteOnExit();
             return f;
         } catch (Exception ex) {
-            return null;
+            if (f.exists()) {
+                /* File already exists in the specified location.
+                 * Usually this happens if the library is already
+                 * in use.
+                 */
+                return f;
+            }
+             else
+                return null;
         }
     }
 
@@ -111,7 +122,8 @@
             return true;
         }
         /* Step 1.
-         * Try to load from acr.library.path
+         * Try to load from the acr.library.path
+         * property (eg. -Dacr.library.path=/some/location)
          */
         String path = System.getProperty("acr.library.path");
         if (path != null) {
@@ -134,7 +146,7 @@
             // Ignore
         }
         /* Step 3.
-         * Try to load from classpath
+         * Try to load from classpath.
          */
         String resname;
         InputStream is = null;

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Loader.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Loader.java?rev=817647&r1=817646&r2=817647&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Loader.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Loader.java Tue Sep 22 13:11:34 2009
@@ -33,12 +33,14 @@
 {
 
     private static HashMap<String, File> libs;
-    private static File   path = null;
+    private static File     path     = null;
+    private static boolean  isLoaded = false;
 
     private Loader()
     {
         // Static class. No instance.
     }
+
     static {
         libs = new HashMap<String, File>();
     }
@@ -47,15 +49,31 @@
      * Get the path to temporary location for extracting the
      * libraries from resource.
      */
-    public static File getPath()
+    public static File libFile(String name)
     {
         synchronized(Loader.class) {
             if (path == null) {
                 try {
-                    path = Utils.createTempDirectory("loader-");
-                    /* TODO: For windows add this to cleanup subproc
-                     */
-                    path.deleteOnExit();
+                    if (SystemId.getSysname().equals("windows")) {
+                        String acr = "ApacheCommonsRuntime_" +
+                                      Properties.VERSION_MAJOR + "_"  +
+                                      Properties.VERSION_MINOR + "_"  +
+                                      Properties.VERSION_PATCH + "_" +
+                                      SystemId.getProcessor()  + "_libpath";
+
+                        path = new File(Utils.getTempPath(), acr);
+                        if (path.mkdir()) {
+                            /* Delete on exit will work only if library load
+                             * fails. In all other cases the directory will
+                             * remain open.
+                             */
+                            path.deleteOnExit();
+                        }
+                    }
+                    else {
+                        path = Utils.createTempDirectory("loader-");
+                        path.deleteOnExit();
+                    }
                 } catch (IOException io) {
                     // Cannot create temp dir
                     return null;
@@ -71,7 +89,7 @@
                     // SecurityException ?
                 }
             }
-            return path;
+            return new File(path, name);
         }
     }
 
@@ -81,7 +99,7 @@
      *
      * @param libname The name of the library
      */
-    public static void add(String libname, File file)
+    protected static void add(String libname, File file)
     {
         libs.put(libname, file);
     }
@@ -117,8 +135,10 @@
     public static boolean load()
         throws UnsupportedOperatingSystemException
     {
+        if (isLoaded)
+            return true;
+
         boolean rc = false;
-        String lib = null;
         if (Properties.NATIVE_LIBS != null) {
             for (int i = 0; i < Properties.NATIVE_LIBS.length; i++) {
                 boolean s = Library.load(Properties.NATIVE_LIBS[i]);
@@ -126,15 +146,15 @@
                     /* Record the result of the last entry in the
                      * list of the libraries
                      */
-                    lib = Properties.NATIVE_LIBS[i];
                     rc  = s;
                 }
             }
         }
         if (rc) {
-            /* TODO: Register Windows cleanup
+            /* Initialize Native library.
              */
-            return Native.initialize();
+            isLoaded = Native.initialize();
+            return isLoaded;
         }
         else
             return false;

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Native.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Native.java?rev=817647&r1=817646&r2=817647&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Native.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Native.java Tue Sep 22 13:11:34 2009
@@ -51,7 +51,8 @@
      * of the library properly initialize.
      * </p>
      * @return {@code true} if the library was properly initialized.
-     * @throws Throwable in case of error.
+     * @throws UnsupportedOperatingSystemException in case of
+     *         unsupported operating system
      */
     public static boolean initialize()
         throws UnsupportedOperatingSystemException

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java?rev=817647&r1=817646&r2=817647&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java Tue Sep 22 13:11:34 2009
@@ -38,6 +38,7 @@
     private static long     counter                = 1024L;
     private static long     timer                  = System.currentTimeMillis();
     private static Random   rnd                    = new Random(timer);
+    private static File     tmpdir                 = null;
 
     private static final char[] hc = {
                 '0', '1', '2', '3', '4', '5', '6', '7',
@@ -186,18 +187,19 @@
         return null;
     }
 
-    private static File findTempDir()
+    public static File getTempPath()
     {
-        File dir;
         String [] try_path;
 
-        dir = checkTempDir(System.getProperty("java.io.tmpdir"));
-        if (dir != null)
-            return dir;
+        if (tmpdir != null)
+            return tmpdir;
+        tmpdir = checkTempDir(System.getProperty("java.io.tmpdir"));
+        if (tmpdir != null)
+            return tmpdir;
         for (int i = 0; i < try_envs.length; i++) {
-            dir = checkTempDir(System.getenv(try_envs[i]));
-            if (dir != null)
-                return dir;
+            tmpdir = checkTempDir(System.getenv(try_envs[i]));
+            if (tmpdir != null)
+                return tmpdir;
         }
         if (SystemId.getSysname().equals("windows"))
             try_path = win_trys;
@@ -205,9 +207,9 @@
             try_path = psx_trys;
 
         for (int i = 0; i < try_path.length; i++) {
-            dir = checkTempDir(System.getenv(try_path[i]));
-            if (dir != null)
-                return dir;
+            tmpdir = checkTempDir(System.getenv(try_path[i]));
+            if (tmpdir != null)
+                return tmpdir;
         }
         return null;
     }
@@ -229,7 +231,7 @@
         int tries  = 0;
         byte [] rb = new byte[12];
         char [] ec = new char[48];
-        File td    = findTempDir();
+        File td    = getTempPath();
 
         if (td == null) {
             throw new IOException("Cannot find writable temporary directory");

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h?rev=817647&r1=817646&r2=817647&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h Tue Sep 22 13:11:34 2009
@@ -231,7 +231,8 @@
  * @return File descriptor or -1 o failure.
  */
 ACR_DECLARE(int) ACR_TempFileMake(JNIEnv *env, const acr_pchar_t *tmpath,
-                                  const acr_pchar_t *prefix, int preserve);
+                                  const acr_pchar_t *prefix,
+                                  const acr_pchar_t *sufix,  int preserve);
 
 /** Create temporary unique directory.
  * @param env JNI environment to use. If NULL no exception will be thrown

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c?rev=817647&r1=817646&r2=817647&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c Tue Sep 22 13:11:34 2009
@@ -62,7 +62,8 @@
 }
 
 ACR_DECLARE(int) ACR_TempFileMake(JNIEnv *_E, const char *tmpath,
-                                  const char *prefix, int preserve)
+                                  const char *prefix,
+                                  const char *sufix, int preserve)
 {
     int  rc = 0;
     int  fd;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c?rev=817647&r1=817646&r2=817647&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c Tue Sep 22 13:11:34 2009
@@ -414,7 +414,7 @@
         }
         else {
             tlsd->id = (unsigned int)InterlockedIncrement(&acr_thread_id_cur);
-            ACR_RING_INIT(&tlsd->data_ring, acr_tlsd_data_t, link);                
+            ACR_RING_INIT(&tlsd->data_ring, acr_tlsd_data_t, link);
             TlsSetValue(dll_tls_index, tlsd);
             return tlsd;
         }
@@ -488,3 +488,4 @@
     }
     return tlsd->env;
 }
+

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/subproc.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/subproc.c?rev=817647&r1=817646&r2=817647&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/subproc.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/subproc.c Tue Sep 22 13:11:34 2009
@@ -53,7 +53,8 @@
  * the short name for loading the .dll.
  * Usage: Rundll32.exe <ShortPath\[lib]acr.dll>,SubprocMainW ...
  */
- 
+
+
  /*
   * Instead in Makefile exports could go here as well
   *
@@ -63,5 +64,7 @@
                                                   LPWSTR lpszCmdLine,
                                                   int nCmdShow)
 {
-    /* Just a stub for now */
+    OutputDebugStringW(L"SubprocMainW init");
+    OutputDebugStringW(GetCommandLineW());
+    OutputDebugStringW(L"SubprocMainW done");
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c?rev=817647&r1=817646&r2=817647&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c Tue Sep 22 13:11:34 2009
@@ -53,6 +53,7 @@
     if (type != ACR_DT_FILE) {
         return ACR_EBADF;
     }
+    x_free(fp->name);
     if (IS_VALID_HANDLE(fp->fd)) {
         if (!CloseHandle(fp->fd))
             return ACR_GET_OS_ERROR();
@@ -81,6 +82,13 @@
         ;
     suffp = trv;
     --trv;
+    while (*trv != L'X') {
+        --trv;
+        if (trv < path) {
+            ACR_SET_OS_ERROR(ACR_EINVAL);
+            return INVALID_HANDLE_VALUE;
+        }
+    }
     if (trv < path) {
         ACR_SET_OS_ERROR(ACR_EINVAL);
         return INVALID_HANDLE_VALUE;
@@ -124,7 +132,7 @@
     for (;;) {
         fh = CreateFileW(path,
                          GENERIC_READ | GENERIC_WRITE,
-                         0,     /* Do not share the temporary file */
+                         0,
                          &sa,
                          CREATE_NEW,
                          flags | FILE_ATTRIBUTE_TEMPORARY,
@@ -251,7 +259,8 @@
 }
 
 ACR_DECLARE(int) ACR_TempFileMake(JNIEnv *_E, const wchar_t *tmpath,
-                                  const wchar_t *prefix, int preserve)
+                                  const wchar_t *prefix,
+                                  const wchar_t *sufix, int preserve)
 {
     int     rc;
     DWORD   ff = preserve ? 0 : FILE_FLAG_DELETE_ON_CLOSE;
@@ -275,6 +284,13 @@
         ACR_THROW_IO_IF_ERR(ACR_E2BIG);
         return -1;
     }
+    if (sufix && wcslcat(name, sufix, ACR_MBUFF_SIZ) >= ACR_MBUFF_SIZ) {
+        /* Truncation occurred. We dont have enough space
+         * to create temp file template
+         */
+        ACR_THROW_IO_IF_ERR(ACR_E2BIG);
+        return -1;
+    }
 
     fd = getftemp(name, ff);
     if (IS_INVALID_HANDLE(fd)) {