[sikuli] 131/385: moved Bridj usage to new class Basics.SysUtil and revised error handling

Gilles Filippini pini at moszumanska.debian.org
Sun Jun 29 19:25:59 UTC 2014


This is an automated email from the git hooks/post-receive script.

pini pushed a commit to tag upstream/1.1.0_beta1
in repository sikuli.

commit 4129ea4a1fb4a00e0b418a21ea7d4b1b243ce5f2
Author: Raimund Hocke <rmhdevelop at me.com>
Date:   Tue Jan 7 11:57:33 2014 +0100

    moved Bridj usage to new class Basics.SysUtil and revised error handling
---
 .../java/org/sikuli/basics/ResourceLoader.java     | 85 ++++++----------------
 .../src/main/java/org/sikuli/basics/SysUtil.java   | 66 +++++++++++++++++
 2 files changed, 89 insertions(+), 62 deletions(-)

diff --git a/Basics/src/main/java/org/sikuli/basics/ResourceLoader.java b/Basics/src/main/java/org/sikuli/basics/ResourceLoader.java
index ada54ef..1a740fd 100755
--- a/Basics/src/main/java/org/sikuli/basics/ResourceLoader.java
+++ b/Basics/src/main/java/org/sikuli/basics/ResourceLoader.java
@@ -6,10 +6,6 @@
  */
 package org.sikuli.basics;
 
-import org.bridj.BridJ;
-import org.bridj.Pointer;
-import org.bridj.ann.Library;
-
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
@@ -338,15 +334,15 @@ public class ResourceLoader implements IResourceLoader {
       log(-2, "Trying to extract libs to: " + libPath);
       if (!FileManager.deleteFileOrFolder(libPath,
               new FileManager.fileFilter() {
-        @Override
-        public boolean accept(File entry) {
-          if (entry.getPath().contains("tessdata")
+                @Override
+                public boolean accept(File entry) {
+                  if (entry.getPath().contains("tessdata")
                   || entry.getPath().contains("Lib")) {
-            return false;
-          }
-          return true;
-        }
-      })) {
+                    return false;
+                  }
+                  return true;
+                }
+              })) {
         log(-1, "Fatal Error 102: not possible to empty libs dir");
         RunSetup.popError("Problem with SikuliX libs folder - see error log");
         SikuliX.terminate(102);
@@ -433,53 +429,11 @@ public class ResourceLoader implements IResourceLoader {
       }
 //TODO check wether needed here (plain Maven usage)
       doSomethingSpecial("exportTessdata", new String[]{});
-      log(lvl, "If OCR/Text activated: Using as OCR directory (tessdata): " + Settings.OcrDataPath);
+      log(lvl, "Using as Tesseract data folder: " 
+              + new File(Settings.OcrDataPath, "tessdata").getAbsolutePath());
     }
   }
 
-  /**
-   * Environment variable access to load native libraries.
-   * We can't put it into WinUtil.dll as it's not loaded yet, so we use BridJ.
-   */
-  @Library("kernel32")
-  public static class Kernel32 {
-      static {
-          BridJ.register();
-      }
-
-      // http://msdn.microsoft.com/en-us/library/windows/desktop/ms683188(v=vs.85).aspx
-      public static native int GetEnvironmentVariableW(
-              Pointer<Character> lpName,
-              Pointer<Character> lpBuffer,
-              int nSize
-      );
-
-      /**
-       *
-       * @return specified environment variable; unlike {@link System#getenv(String)} it's the real WinAPI environment,
-       *    not a JVM-local copy
-       */
-      public static String getEnvironmentVariable(String name) {
-          final int BUFFER_SIZE = 16384;
-          Pointer<Character> buffer = Pointer.allocateArray(Character.class, BUFFER_SIZE);
-          int result = GetEnvironmentVariableW(Pointer.pointerToWideCString(name), buffer, BUFFER_SIZE);
-          if(result == 0)
-              throw new RuntimeException("Unable to get environment variable " + name);
-          return buffer.getWideCString();
-      }
-
-      // http://msdn.microsoft.com/en-us/library/windows/desktop/ms686206(v=vs.85).aspx
-      public static native boolean SetEnvironmentVariableW(
-              Pointer<Character> lpName,
-              Pointer<Character> lpValue
-      );
-
-      public static void setEnvironmentVariable(String name, String value) {
-          if(!SetEnvironmentVariableW(Pointer.pointerToWideCString(name), Pointer.pointerToWideCString(value)))
-              throw new RuntimeException("Unable to set environment variable " + name);
-      }
-  }
-
   private File checkLibsDir(String path) {
     String memx = mem;
     mem = "checkLibsDir";
@@ -488,11 +442,17 @@ public class ResourceLoader implements IResourceLoader {
       log(lvl, path);
       if (!Settings.runningSetup && Settings.isWindows()) {
         // is on system path?
-        String syspath = Kernel32.getEnvironmentVariable("PATH");
-        path = (new File(path).getAbsolutePath()).replaceAll("/", "\\");
-        if (!syspath.toUpperCase().contains(path.toUpperCase())) {
-          log(-1, "libs dir is not on system path: " + path + "; Adding it to path");
-          Kernel32.setEnvironmentVariable("PATH", syspath + ";" + path);
+        String syspath = SysUtil.WinKernel32.getEnvironmentVariable("PATH");
+        if (syspath == null) {
+          SikuliX.terminate(1);
+        } else {
+          path = (new File(path).getAbsolutePath()).replaceAll("/", "\\");
+          if (!syspath.toUpperCase().contains(path.toUpperCase())) {
+            log(lvl, "Adding libs dir to path: " + path);
+            if (!SysUtil.WinKernel32.setEnvironmentVariable("PATH", syspath + ";" + path)) {
+              SikuliX.terminate(1);
+            }
+          }
         }
       }
       if (System.getProperty("sikuli.DoNotExport") != null) {
@@ -933,7 +893,8 @@ public class ResourceLoader implements IResourceLoader {
   }
 
   /**
-   * Extract files from a jar using a list of files in a file (def. filelist.txt)
+   * Extract files from a jar using a list of files in a file (def.
+   * filelist.txt)
    *
    * @param srcPath from here
    * @param localPath to there (if null, create a default in temp folder)
diff --git a/Basics/src/main/java/org/sikuli/basics/SysUtil.java b/Basics/src/main/java/org/sikuli/basics/SysUtil.java
new file mode 100644
index 0000000..83f6279
--- /dev/null
+++ b/Basics/src/main/java/org/sikuli/basics/SysUtil.java
@@ -0,0 +1,66 @@
+package org.sikuli.basics;
+
+import org.bridj.BridJ;
+import org.bridj.Pointer;
+import org.bridj.ann.Library;
+
+/**
+ * Direct access to system functions via JNI, JNA, BridJ, ...
+ */
+public class SysUtil {
+  /**
+   * Direct access to Windows API kernel32.dll via BridJ
+   */
+  @Library("kernel32")
+  public static class WinKernel32 {
+
+    static {
+      BridJ.register();
+    }
+
+    // http://msdn.microsoft.com/en-us/library/windows/desktop/ms683188(v=vs.85).aspx
+    private static native int GetEnvironmentVariableW(
+            Pointer<Character> lpName,
+            Pointer<Character> lpBuffer,
+            int nSize
+    );
+
+    // http://msdn.microsoft.com/en-us/library/windows/desktop/ms686206(v=vs.85).aspx
+    private static native boolean SetEnvironmentVariableW(
+            Pointer<Character> lpName,
+            Pointer<Character> lpValue
+    );
+
+    /**
+     * get the current value of a variable from real Windows environment
+     *
+     * @param name of the environment variable
+     * @return current content
+     */
+    public static String getEnvironmentVariable(String name) {
+      final int BUFFER_SIZE = 16384;
+      Pointer<Character> buffer = Pointer.allocateArray(Character.class, BUFFER_SIZE);
+      int result = GetEnvironmentVariableW(Pointer.pointerToWideCString(name), buffer, BUFFER_SIZE);
+      if (result == 0) {
+        Debug.error("WinKernel32: getEnvironmentVariable: does not work for: %s", name);
+        return null;
+      }
+      return buffer.getWideCString();
+    }
+
+    /**
+     * set the value of a variable in real Windows environment
+     *
+     * @param name of the environment variable
+     * @param value of the environment variable
+     */
+    public static boolean setEnvironmentVariable(String name, String value) {
+      if (!SetEnvironmentVariableW(Pointer.pointerToWideCString(name), Pointer.pointerToWideCString(value))) {
+        Debug.error("WinKernel32: setEnvironmentVariable: does not work for: %s = %s", name, value);
+        return false;
+      }
+      return true;
+    }
+  }
+
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/sikuli.git



More information about the pkg-java-commits mailing list