You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sd...@apache.org on 2016/06/24 07:10:44 UTC

[1/4] commons-crypto git commit: Move NativeCodeLoader to top level package so that it can be made package private

Repository: commons-crypto
Updated Branches:
  refs/heads/master 7c328f25f -> 6529207de


Move NativeCodeLoader to top level package so that it can be made package private


Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/d94bd079
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/d94bd079
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/d94bd079

Branch: refs/heads/master
Commit: d94bd07944a73d9ba7d7ffc5053b7d0264818135
Parents: 617a8b8
Author: Benedikt Ritter <be...@gmail.com>
Authored: Tue Jun 21 20:18:34 2016 +0200
Committer: Benedikt Ritter <be...@gmail.com>
Committed: Fri Jun 24 08:53:21 2016 +0200

----------------------------------------------------------------------
 .../java/org/apache/commons/crypto/Crypto.java  |   2 -
 .../apache/commons/crypto/NativeCodeLoader.java | 290 +++++++++++++++++++
 .../apache/commons/crypto/cipher/Openssl.java   |   4 +-
 .../crypto/random/OpensslCryptoRandom.java      |   4 +-
 .../commons/crypto/utils/NativeCodeLoader.java  | 287 ------------------
 5 files changed, 294 insertions(+), 293 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/d94bd079/src/main/java/org/apache/commons/crypto/Crypto.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/Crypto.java b/src/main/java/org/apache/commons/crypto/Crypto.java
index 507e566..1da6d25 100644
--- a/src/main/java/org/apache/commons/crypto/Crypto.java
+++ b/src/main/java/org/apache/commons/crypto/Crypto.java
@@ -17,8 +17,6 @@
  */
 package org.apache.commons.crypto;
 
-import org.apache.commons.crypto.utils.NativeCodeLoader;
-
 /**
  * The Crypto class provides some diagnostic information about Commons Crypto.
  */

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/d94bd079/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
new file mode 100644
index 0000000..00e8766
--- /dev/null
+++ b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
@@ -0,0 +1,290 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.crypto;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Properties;
+import java.util.UUID;
+
+import org.apache.commons.crypto.utils.OSInfo;
+import org.apache.commons.crypto.utils.Utils;
+
+/**
+ * A helper to load the native code i.e. libcommons-crypto.so. This handles the
+ * fallback to either the bundled libcommons-crypto-Linux-i386-32.so or the
+ * default java implementations where appropriate.
+ */
+final class NativeCodeLoader {
+
+    private final static boolean nativeCodeLoaded;
+    /**
+     * The private constructor of {@link NativeCodeLoader}.
+     */
+    private NativeCodeLoader() {
+    }
+
+    static {
+        // Try to load native library and set fallback flag appropriately
+        boolean nativeLoaded = false;
+
+        //Trying to load the custom-built native-commons-crypto library...");
+        try {
+            File nativeLibFile = findNativeLibrary();
+            if (nativeLibFile != null) {
+                // Load extracted or specified native library.
+                System.load(nativeLibFile.getAbsolutePath());
+            } else {
+                // Load preinstalled library (in the path -Djava.library.path)
+                System.loadLibrary("commons-crypto");
+            }
+            // Loaded the native library
+            nativeLoaded = true;
+        } catch (Throwable t) {
+            ;// NOPMD: Ignore failure to load
+        }
+
+        nativeCodeLoaded = nativeLoaded;
+    }
+
+    /**
+     * Finds the native library.
+     *
+     * @return the jar file.
+     */
+    private static File findNativeLibrary() {
+        // Try to load the library in commons-crypto.lib.path */
+        String nativeLibraryPath = Utils.getLibPath();
+        String nativeLibraryName = Utils.getLibName();
+
+        // Resolve the library file name with a suffix (e.g., dll, .so, etc.)
+        if (nativeLibraryName == null) {
+            nativeLibraryName = System.mapLibraryName("commons-crypto");
+        }
+        if (nativeLibraryPath != null) {
+            File nativeLib = new File(nativeLibraryPath, nativeLibraryName);
+            if (nativeLib.exists()) {
+                return nativeLib;
+            }
+        }
+
+        // Load an OS-dependent native library inside a jar file
+        nativeLibraryPath = "/org/apache/commons/crypto/native/"
+                + OSInfo.getNativeLibFolderPathForCurrentOS();
+        boolean hasNativeLib = hasResource(nativeLibraryPath + "/"
+                + nativeLibraryName);
+        if (!hasNativeLib) {
+            String altName = "libcommons-crypto.jnilib";
+            if (OSInfo.getOSName().equals("Mac") && hasResource(nativeLibraryPath + "/" + altName)) {
+                // Fix for openjdk7 for Mac
+                nativeLibraryName = altName;
+                hasNativeLib = true;
+            }
+        }
+
+        if (!hasNativeLib) {
+            String errorMessage = String.format(
+                    "no native library is found for os.name=%s and os.arch=%s",
+                    OSInfo.getOSName(), OSInfo.getArchName());
+            throw new RuntimeException(errorMessage);
+        }
+
+        // Temporary folder for the native lib. Use the value of
+        // commons-crypto.tempdir or java.io.tmpdir
+        String tempFolder = new File(Utils.getTmpDir()).getAbsolutePath();
+
+        // Extract and load a native library inside the jar file
+        return extractLibraryFile(nativeLibraryPath, nativeLibraryName,
+                tempFolder);
+    }
+
+    /**
+     * Extracts the specified library file to the target folder.
+     *
+     * @param libFolderForCurrentOS the library in commons-crypto.lib.path.
+     * @param libraryFileName the library name.
+     * @param targetFolder Target folder for the native lib. Use the value of
+     *        commons-crypto.tempdir or java.io.tmpdir.
+     * @return the library file.
+     */
+    private static File extractLibraryFile(String libFolderForCurrentOS,
+            String libraryFileName, String targetFolder) {
+        String nativeLibraryFilePath = libFolderForCurrentOS + "/"
+                + libraryFileName;
+
+        // Attach UUID to the native library file to ensure multiple class
+        // loaders
+        // can read the libcommons-crypto multiple times.
+        String uuid = UUID.randomUUID().toString();
+        String extractedLibFileName = String.format("commons-crypto-%s-%s-%s",
+                getVersion(), uuid, libraryFileName);
+        File extractedLibFile = new File(targetFolder, extractedLibFileName);
+
+        InputStream reader = null;
+        try {
+            // Extract a native library file into the target directory
+            reader = NativeCodeLoader.class
+                    .getResourceAsStream(nativeLibraryFilePath);
+            FileOutputStream writer = new FileOutputStream(extractedLibFile);
+            try {
+                byte[] buffer = new byte[8192];
+                int bytesRead;
+                while ((bytesRead = reader.read(buffer)) != -1) {
+                    writer.write(buffer, 0, bytesRead);
+                }
+            } finally {
+                // Delete the extracted lib file on JVM exit.
+                extractedLibFile.deleteOnExit();
+
+                writer.close();
+
+                if (reader != null) {
+                    reader.close();
+                    reader = null;
+                }
+            }
+
+            // Set executable (x) flag to enable Java to load the native library
+            if (!extractedLibFile.setReadable(true)
+                    || !extractedLibFile.setExecutable(true)
+                    || !extractedLibFile.setWritable(true, true)) {
+                throw new RuntimeException("Invalid path for library path");
+            }
+
+            // Check whether the contents are properly copied from the resource
+            // folder
+            {
+                InputStream nativeIn = null;
+                InputStream extractedLibIn = null;
+                try {
+                    nativeIn = NativeCodeLoader.class
+                            .getResourceAsStream(nativeLibraryFilePath);
+                    extractedLibIn = new FileInputStream(extractedLibFile);
+                    if (!contentsEquals(nativeIn, extractedLibIn)) {
+                        throw new RuntimeException(String.format(
+                                "Failed to write a native library file at %s",
+                                extractedLibFile));
+                    }
+                } finally {
+                    if (nativeIn != null) {
+                        nativeIn.close();
+                    }
+                    if (extractedLibIn != null) {
+                        extractedLibIn.close();
+                    }
+                }
+            }
+
+            return new File(targetFolder, extractedLibFileName);
+        } catch (IOException e) {
+            e.printStackTrace(System.err);
+            return null;
+        } finally {
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+    /**
+     * Gets the version by reading pom.properties embedded in jar. This version
+     * data is used as a suffix of a dll file extracted from the jar.
+     *
+     * @return the version string
+     */
+    static String getVersion() {
+        URL versionFile = NativeCodeLoader.class
+                .getResource("/META-INF/maven/org.apache.commons.crypto/commons-crypto/pom.properties");
+        if (versionFile == null) {
+            versionFile = NativeCodeLoader.class
+                    .getResource("/org/apache/commons/crypto/VERSION");
+        }
+        String version = "unknown";
+        try {
+            if (versionFile != null) {
+                Properties versionData = new Properties();
+                versionData.load(versionFile.openStream());
+                version = versionData.getProperty("version", version);
+                if (version.equals("unknown")) {
+                    version = versionData.getProperty("VERSION", version);
+                }
+                version = version.trim().replaceAll("[^0-9M\\.]", "");
+            }
+        } catch (IOException e) {
+            System.err.println(e);
+        }
+        return version;
+    }
+
+    /**
+     * Checks whether in1 and in2 is equal.
+     *
+     * @param in1 the input1.
+     * @param in2 the input2.
+     * @return true if in1 and in2 is equal, else false.
+     * @throws IOException if an I/O error occurs.
+     */
+    private static boolean contentsEquals(InputStream in1, InputStream in2)
+            throws IOException {
+        if (!(in1 instanceof BufferedInputStream)) {
+            in1 = new BufferedInputStream(in1);
+        }
+        if (!(in2 instanceof BufferedInputStream)) {
+            in2 = new BufferedInputStream(in2);
+        }
+
+        int ch = in1.read();
+        while (ch != -1) {
+            int ch2 = in2.read();
+            if (ch != ch2) {
+                return false;
+            }
+            ch = in1.read();
+        }
+        int ch2 = in2.read();
+        return ch2 == -1;
+    }
+
+    /**
+     * Checks whether the given path has resource.
+     *
+     * @param path the path.
+     * @return the boolean.
+     */
+    private static boolean hasResource(String path) {
+        return NativeCodeLoader.class.getResource(path) != null;
+    }
+
+    /**
+     * Checks whether native code is loaded for this platform.
+     *
+     * @return <code>true</code> if native is loaded, else <code>false</code>.
+     */
+    static boolean isNativeCodeLoaded() {
+        return nativeCodeLoaded;
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/d94bd079/src/main/java/org/apache/commons/crypto/cipher/Openssl.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/cipher/Openssl.java b/src/main/java/org/apache/commons/crypto/cipher/Openssl.java
index 0953202..8625bf4 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/Openssl.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/Openssl.java
@@ -25,7 +25,7 @@ import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.NoSuchPaddingException;
 import javax.crypto.ShortBufferException;
 
-import org.apache.commons.crypto.utils.NativeCodeLoader;
+import org.apache.commons.crypto.Crypto;
 import org.apache.commons.crypto.utils.Utils;
 
 /**
@@ -91,7 +91,7 @@ final class Openssl {
     static {
         String loadingFailure = null;
         try {
-            if (NativeCodeLoader.isNativeCodeLoaded()) {
+            if (Crypto.isNativeCodeLoaded()) {
                 OpensslNative.initIDs();
             }
         } catch (Throwable t) {

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/d94bd079/src/main/java/org/apache/commons/crypto/random/OpensslCryptoRandom.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/random/OpensslCryptoRandom.java b/src/main/java/org/apache/commons/crypto/random/OpensslCryptoRandom.java
index af78ddd..22433ed 100644
--- a/src/main/java/org/apache/commons/crypto/random/OpensslCryptoRandom.java
+++ b/src/main/java/org/apache/commons/crypto/random/OpensslCryptoRandom.java
@@ -21,7 +21,7 @@ import java.security.NoSuchAlgorithmException;
 import java.util.Properties;
 import java.util.Random;
 
-import org.apache.commons.crypto.utils.NativeCodeLoader;
+import org.apache.commons.crypto.Crypto;
 import org.apache.commons.crypto.utils.Utils;
 
 /**
@@ -50,7 +50,7 @@ public class OpensslCryptoRandom extends Random implements CryptoRandom {
 
     static {
         boolean opensslLoaded = false;
-        if (NativeCodeLoader.isNativeCodeLoaded()) {
+        if (Crypto.isNativeCodeLoaded()) {
             try {
                 OpensslCryptoRandomNative.initSR();
                 opensslLoaded = true;

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/d94bd079/src/main/java/org/apache/commons/crypto/utils/NativeCodeLoader.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/utils/NativeCodeLoader.java b/src/main/java/org/apache/commons/crypto/utils/NativeCodeLoader.java
deleted file mode 100644
index f07a27c..0000000
--- a/src/main/java/org/apache/commons/crypto/utils/NativeCodeLoader.java
+++ /dev/null
@@ -1,287 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.crypto.utils;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.Properties;
-import java.util.UUID;
-
-/**
- * A helper to load the native code i.e. libcommons-crypto.so. This handles the
- * fallback to either the bundled libcommons-crypto-Linux-i386-32.so or the
- * default java implementations where appropriate.
- */
-public final class NativeCodeLoader {
-
-    private final static boolean nativeCodeLoaded;
-    /**
-     * The private constructor of {@link NativeCodeLoader}.
-     */
-    private NativeCodeLoader() {
-    }
-
-    static {
-        // Try to load native library and set fallback flag appropriately
-        boolean nativeLoaded = false;
-
-        //Trying to load the custom-built native-commons-crypto library...");
-        try {
-            File nativeLibFile = findNativeLibrary();
-            if (nativeLibFile != null) {
-                // Load extracted or specified native library.
-                System.load(nativeLibFile.getAbsolutePath());
-            } else {
-                // Load preinstalled library (in the path -Djava.library.path)
-                System.loadLibrary("commons-crypto");
-            }
-            // Loaded the native library
-            nativeLoaded = true;
-        } catch (Throwable t) {
-            ;// NOPMD: Ignore failure to load
-        }
-
-        nativeCodeLoaded = nativeLoaded;
-    }
-
-    /**
-     * Finds the native library.
-     *
-     * @return the jar file.
-     */
-    private static File findNativeLibrary() {
-        // Try to load the library in commons-crypto.lib.path */
-        String nativeLibraryPath = Utils.getLibPath();
-        String nativeLibraryName = Utils.getLibName();
-
-        // Resolve the library file name with a suffix (e.g., dll, .so, etc.)
-        if (nativeLibraryName == null) {
-            nativeLibraryName = System.mapLibraryName("commons-crypto");
-        }
-        if (nativeLibraryPath != null) {
-            File nativeLib = new File(nativeLibraryPath, nativeLibraryName);
-            if (nativeLib.exists()) {
-                return nativeLib;
-            }
-        }
-
-        // Load an OS-dependent native library inside a jar file
-        nativeLibraryPath = "/org/apache/commons/crypto/native/"
-                + OSInfo.getNativeLibFolderPathForCurrentOS();
-        boolean hasNativeLib = hasResource(nativeLibraryPath + "/"
-                + nativeLibraryName);
-        if (!hasNativeLib) {
-            String altName = "libcommons-crypto.jnilib";
-            if (OSInfo.getOSName().equals("Mac") && hasResource(nativeLibraryPath + "/" + altName)) {
-                // Fix for openjdk7 for Mac
-                nativeLibraryName = altName;
-                hasNativeLib = true;
-            }
-        }
-
-        if (!hasNativeLib) {
-            String errorMessage = String.format(
-                    "no native library is found for os.name=%s and os.arch=%s",
-                    OSInfo.getOSName(), OSInfo.getArchName());
-            throw new RuntimeException(errorMessage);
-        }
-
-        // Temporary folder for the native lib. Use the value of
-        // commons-crypto.tempdir or java.io.tmpdir
-        String tempFolder = new File(Utils.getTmpDir()).getAbsolutePath();
-
-        // Extract and load a native library inside the jar file
-        return extractLibraryFile(nativeLibraryPath, nativeLibraryName,
-                tempFolder);
-    }
-
-    /**
-     * Extracts the specified library file to the target folder.
-     *
-     * @param libFolderForCurrentOS the library in commons-crypto.lib.path.
-     * @param libraryFileName the library name.
-     * @param targetFolder Target folder for the native lib. Use the value of
-     *        commons-crypto.tempdir or java.io.tmpdir.
-     * @return the library file.
-     */
-    private static File extractLibraryFile(String libFolderForCurrentOS,
-            String libraryFileName, String targetFolder) {
-        String nativeLibraryFilePath = libFolderForCurrentOS + "/"
-                + libraryFileName;
-
-        // Attach UUID to the native library file to ensure multiple class
-        // loaders
-        // can read the libcommons-crypto multiple times.
-        String uuid = UUID.randomUUID().toString();
-        String extractedLibFileName = String.format("commons-crypto-%s-%s-%s",
-                getVersion(), uuid, libraryFileName);
-        File extractedLibFile = new File(targetFolder, extractedLibFileName);
-
-        InputStream reader = null;
-        try {
-            // Extract a native library file into the target directory
-            reader = NativeCodeLoader.class
-                    .getResourceAsStream(nativeLibraryFilePath);
-            FileOutputStream writer = new FileOutputStream(extractedLibFile);
-            try {
-                byte[] buffer = new byte[8192];
-                int bytesRead;
-                while ((bytesRead = reader.read(buffer)) != -1) {
-                    writer.write(buffer, 0, bytesRead);
-                }
-            } finally {
-                // Delete the extracted lib file on JVM exit.
-                extractedLibFile.deleteOnExit();
-
-                writer.close();
-
-                if (reader != null) {
-                    reader.close();
-                    reader = null;
-                }
-            }
-
-            // Set executable (x) flag to enable Java to load the native library
-            if (!extractedLibFile.setReadable(true)
-                    || !extractedLibFile.setExecutable(true)
-                    || !extractedLibFile.setWritable(true, true)) {
-                throw new RuntimeException("Invalid path for library path");
-            }
-
-            // Check whether the contents are properly copied from the resource
-            // folder
-            {
-                InputStream nativeIn = null;
-                InputStream extractedLibIn = null;
-                try {
-                    nativeIn = NativeCodeLoader.class
-                            .getResourceAsStream(nativeLibraryFilePath);
-                    extractedLibIn = new FileInputStream(extractedLibFile);
-                    if (!contentsEquals(nativeIn, extractedLibIn)) {
-                        throw new RuntimeException(String.format(
-                                "Failed to write a native library file at %s",
-                                extractedLibFile));
-                    }
-                } finally {
-                    if (nativeIn != null) {
-                        nativeIn.close();
-                    }
-                    if (extractedLibIn != null) {
-                        extractedLibIn.close();
-                    }
-                }
-            }
-
-            return new File(targetFolder, extractedLibFileName);
-        } catch (IOException e) {
-            e.printStackTrace(System.err);
-            return null;
-        } finally {
-            if (reader != null) {
-                try {
-                    reader.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-        }
-    }
-
-    /**
-     * Gets the version by reading pom.properties embedded in jar. This version
-     * data is used as a suffix of a dll file extracted from the jar.
-     *
-     * @return the version string
-     */
-    public static String getVersion() {
-        URL versionFile = NativeCodeLoader.class
-                .getResource("/META-INF/maven/org.apache.commons.crypto/commons-crypto/pom.properties");
-        if (versionFile == null) {
-            versionFile = NativeCodeLoader.class
-                    .getResource("/org/apache/commons/crypto/VERSION");
-        }
-        String version = "unknown";
-        try {
-            if (versionFile != null) {
-                Properties versionData = new Properties();
-                versionData.load(versionFile.openStream());
-                version = versionData.getProperty("version", version);
-                if (version.equals("unknown")) {
-                    version = versionData.getProperty("VERSION", version);
-                }
-                version = version.trim().replaceAll("[^0-9M\\.]", "");
-            }
-        } catch (IOException e) {
-            System.err.println(e);
-        }
-        return version;
-    }
-
-    /**
-     * Checks whether in1 and in2 is equal.
-     *
-     * @param in1 the input1.
-     * @param in2 the input2.
-     * @return true if in1 and in2 is equal, else false.
-     * @throws IOException if an I/O error occurs.
-     */
-    private static boolean contentsEquals(InputStream in1, InputStream in2)
-            throws IOException {
-        if (!(in1 instanceof BufferedInputStream)) {
-            in1 = new BufferedInputStream(in1);
-        }
-        if (!(in2 instanceof BufferedInputStream)) {
-            in2 = new BufferedInputStream(in2);
-        }
-
-        int ch = in1.read();
-        while (ch != -1) {
-            int ch2 = in2.read();
-            if (ch != ch2) {
-                return false;
-            }
-            ch = in1.read();
-        }
-        int ch2 = in2.read();
-        return ch2 == -1;
-    }
-
-    /**
-     * Checks whether the given path has resource.
-     *
-     * @param path the path.
-     * @return the boolean.
-     */
-    private static boolean hasResource(String path) {
-        return NativeCodeLoader.class.getResource(path) != null;
-    }
-
-    /**
-     * Checks whether native code is loaded for this platform.
-     *
-     * @return <code>true</code> if native is loaded, else <code>false</code>.
-     */
-    public static boolean isNativeCodeLoaded() {
-        return nativeCodeLoaded;
-    }
-}


[3/4] commons-crypto git commit: Move OSInfo to top level package an make it package private

Posted by sd...@apache.org.
Move OSInfo to top level package an make it package private


Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/363ec740
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/363ec740
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/363ec740

Branch: refs/heads/master
Commit: 363ec7404c632afaf93cf6e9290baccf99463541
Parents: d94bd07
Author: Benedikt Ritter <be...@gmail.com>
Authored: Tue Jun 21 20:22:35 2016 +0200
Committer: Benedikt Ritter <be...@gmail.com>
Committed: Fri Jun 24 08:55:01 2016 +0200

----------------------------------------------------------------------
 Makefile.common                                 |   4 +-
 .../apache/commons/crypto/NativeCodeLoader.java |   1 -
 .../java/org/apache/commons/crypto/OSInfo.java  | 216 +++++++++++++++++++
 .../org/apache/commons/crypto/utils/OSInfo.java | 216 -------------------
 4 files changed, 218 insertions(+), 219 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/363ec740/Makefile.common
----------------------------------------------------------------------
diff --git a/Makefile.common b/Makefile.common
index f861319..2c41b31 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -32,8 +32,8 @@ JAVA  := "$$JAVA_HOME/bin/java"
 JAVAC := "$$JAVA_HOME/bin/javac"
 JAVAH := "$$JAVA_HOME/bin/javah"
 
-OSINFO_CLASS := org.apache.commons.crypto.utils.OSInfo
-OSINFO_PROG := $(TARGET)/classes/org/apache/commons/crypto/utils/OSInfo.class
+OSINFO_CLASS := org.apache.commons.crypto.OSInfo
+OSINFO_PROG := $(TARGET)/classes/org/apache/commons/crypto/OSInfo.class
 
 OS_NAME := $(shell $(JAVA) -cp $(TARGET)/classes $(OSINFO_CLASS) --os)
 OS_ARCH := $(shell $(JAVA) -cp $(TARGET)/classes $(OSINFO_CLASS) --arch)

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/363ec740/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
index 00e8766..5c7d1f3 100644
--- a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
+++ b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
@@ -27,7 +27,6 @@ import java.net.URL;
 import java.util.Properties;
 import java.util.UUID;
 
-import org.apache.commons.crypto.utils.OSInfo;
 import org.apache.commons.crypto.utils.Utils;
 
 /**

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/363ec740/src/main/java/org/apache/commons/crypto/OSInfo.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/OSInfo.java b/src/main/java/org/apache/commons/crypto/OSInfo.java
new file mode 100644
index 0000000..08048b2
--- /dev/null
+++ b/src/main/java/org/apache/commons/crypto/OSInfo.java
@@ -0,0 +1,216 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.crypto;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Locale;
+
+/**
+ * Provides OS name and architecture name.
+ */
+final class OSInfo {
+    private final static HashMap<String, String> archMapping = new HashMap<>();
+
+    /**
+     * The constant string represents for X86 architecture, the value is:
+     * {@value #X86}.
+     */
+    static final String X86 = "x86";
+
+    /**
+     * The constant string represents for X86_64 architecture, the value is:
+     * {@value #X86_64}.
+     */
+    static final String X86_64 = "x86_64";
+
+    /**
+     * The constant string represents for IA64_32 architecture, the value is:
+     * {@value #IA64_32}.
+     */
+    static final String IA64_32 = "ia64_32";
+
+    /**
+     * The constant string represents for IA64 architecture, the value is:
+     * {@value #IA64}.
+     */
+    static final String IA64 = "ia64";
+
+    /**
+     * The constant string represents for PPC architecture, the value is:
+     * {@value #PPC}.
+     */
+    static final String PPC = "ppc";
+
+    /**
+     * The constant string represents for PPC64 architecture, the value is:
+     * {@value #PPC64}.
+     */
+    static final String PPC64 = "ppc64";
+
+    /**
+     * The private constructor of {@Link OSInfo}.
+     */
+    private OSInfo() {
+    }
+
+    static {
+        // x86 mappings
+        archMapping.put(X86, X86);
+        archMapping.put("i386", X86);
+        archMapping.put("i486", X86);
+        archMapping.put("i586", X86);
+        archMapping.put("i686", X86);
+        archMapping.put("pentium", X86);
+
+        // x86_64 mappings
+        archMapping.put(X86_64, X86_64);
+        archMapping.put("amd64", X86_64);
+        archMapping.put("em64t", X86_64);
+        archMapping.put("universal", X86_64); // Needed for openjdk7 in Mac
+
+        // Itenium 64-bit mappings
+        archMapping.put(IA64, IA64);
+        archMapping.put("ia64w", IA64);
+
+        // Itenium 32-bit mappings, usually an HP-UX construct
+        archMapping.put(IA64_32, IA64_32);
+        archMapping.put("ia64n", IA64_32);
+
+        // PowerPC mappings
+        archMapping.put(PPC, PPC);
+        archMapping.put("power", PPC);
+        archMapping.put("powerpc", PPC);
+        archMapping.put("power_pc", PPC);
+        archMapping.put("power_rs", PPC);
+
+        // TODO: PowerPC 64bit mappings
+        archMapping.put(PPC64, PPC64);
+        archMapping.put("power64", PPC64);
+        archMapping.put("powerpc64", PPC64);
+        archMapping.put("power_pc64", PPC64);
+        archMapping.put("power_rs64", PPC64);
+    }
+
+    /**
+     * The main method.
+     *
+     * @param args the argv.
+     */
+    public static void main(String[] args) {
+        if (args.length >= 1) {
+            if ("--os".equals(args[0])) {
+                System.out.print(getOSName());
+                return;
+            } else if ("--arch".equals(args[0])) {
+                System.out.print(getArchName());
+                return;
+            }
+        }
+
+        System.out.print(getNativeLibFolderPathForCurrentOS());
+    }
+
+    /**
+     * Gets the native lib folder.
+     *
+     * @return the current OS's native lib folder.
+     */
+    static String getNativeLibFolderPathForCurrentOS() {
+        return getOSName() + "/" + getArchName();
+    }
+
+    /**
+     * Gets the OS name.
+     *
+     * @return the OS name.
+     */
+    static String getOSName() {
+        return translateOSNameToFolderName(System.getProperty("os.name"));
+    }
+
+    /**
+     * Gets the architecture name.
+     *
+     * @return the architecture name.
+     */
+    static String getArchName() {
+        // if running Linux on ARM, need to determine ABI of JVM
+        String osArch = System.getProperty("os.arch");
+        if (osArch.startsWith("arm")
+                && System.getProperty("os.name").contains("Linux")) {
+            String javaHome = System.getProperty("java.home");
+            try {
+                // determine if first JVM found uses ARM hard-float ABI
+                String[] cmdarray = {
+                        "/bin/sh",
+                        "-c",
+                        "find '"
+                                + javaHome
+                                + "' -name 'libjvm.so' | head -1 | xargs readelf -A | "
+                                + "grep 'Tag_ABI_VFP_args: VFP registers'" };
+                int exitCode = Runtime.getRuntime().exec(cmdarray).waitFor();
+                if (exitCode == 0) {
+                    return "armhf";
+                }
+            } catch (IOException e) { //NOPMD
+                // ignored: fall back to "arm" arch (soft-float ABI)
+            } catch (InterruptedException e) { //NOPMD
+                // ignored: fall back to "arm" arch (soft-float ABI)
+            }
+        } else {
+            String lc = osArch.toLowerCase(Locale.US);
+            if (archMapping.containsKey(lc)) {
+                return archMapping.get(lc);
+            }
+        }
+        return translateArchNameToFolderName(osArch);
+    }
+
+    /**
+     * Translates the OS name to folder name.
+     *
+     * @param osName the OS name.
+     * @return the folder name.
+     */
+    private static String translateOSNameToFolderName(String osName) {
+        if (osName.contains("Windows")) {
+            return "Windows";
+        } else if (osName.contains("Mac")) {
+            return "Mac";
+        } else if (osName.contains("Linux")) {
+            return "Linux";
+        } else if (osName.contains("AIX")) {
+            return "AIX";
+        }
+
+        else {
+            return osName.replaceAll("\\W", "");
+        }
+    }
+
+    /**
+     * Translates the architecture name to folder name.
+     *
+     * @param archName the architecture name.
+     * @return the folder name.
+     */
+    private static String translateArchNameToFolderName(String archName) {
+        return archName.replaceAll("\\W", "");
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/363ec740/src/main/java/org/apache/commons/crypto/utils/OSInfo.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/utils/OSInfo.java b/src/main/java/org/apache/commons/crypto/utils/OSInfo.java
deleted file mode 100644
index 4cd4ef4..0000000
--- a/src/main/java/org/apache/commons/crypto/utils/OSInfo.java
+++ /dev/null
@@ -1,216 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.crypto.utils;
-
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Locale;
-
-/**
- * Provides OS name and architecture name.
- */
-public class OSInfo {
-    private final static HashMap<String, String> archMapping = new HashMap<>();
-
-    /**
-     * The constant string represents for X86 architecture, the value is:
-     * {@value #X86}.
-     */
-    public static final String X86 = "x86";
-
-    /**
-     * The constant string represents for X86_64 architecture, the value is:
-     * {@value #X86_64}.
-     */
-    public static final String X86_64 = "x86_64";
-
-    /**
-     * The constant string represents for IA64_32 architecture, the value is:
-     * {@value #IA64_32}.
-     */
-    public static final String IA64_32 = "ia64_32";
-
-    /**
-     * The constant string represents for IA64 architecture, the value is:
-     * {@value #IA64}.
-     */
-    public static final String IA64 = "ia64";
-
-    /**
-     * The constant string represents for PPC architecture, the value is:
-     * {@value #PPC}.
-     */
-    public static final String PPC = "ppc";
-
-    /**
-     * The constant string represents for PPC64 architecture, the value is:
-     * {@value #PPC64}.
-     */
-    public static final String PPC64 = "ppc64";
-
-    /**
-     * The private constructor of {@Link OSInfo}.
-     */
-    private OSInfo() {
-    }
-
-    static {
-        // x86 mappings
-        archMapping.put(X86, X86);
-        archMapping.put("i386", X86);
-        archMapping.put("i486", X86);
-        archMapping.put("i586", X86);
-        archMapping.put("i686", X86);
-        archMapping.put("pentium", X86);
-
-        // x86_64 mappings
-        archMapping.put(X86_64, X86_64);
-        archMapping.put("amd64", X86_64);
-        archMapping.put("em64t", X86_64);
-        archMapping.put("universal", X86_64); // Needed for openjdk7 in Mac
-
-        // Itenium 64-bit mappings
-        archMapping.put(IA64, IA64);
-        archMapping.put("ia64w", IA64);
-
-        // Itenium 32-bit mappings, usually an HP-UX construct
-        archMapping.put(IA64_32, IA64_32);
-        archMapping.put("ia64n", IA64_32);
-
-        // PowerPC mappings
-        archMapping.put(PPC, PPC);
-        archMapping.put("power", PPC);
-        archMapping.put("powerpc", PPC);
-        archMapping.put("power_pc", PPC);
-        archMapping.put("power_rs", PPC);
-
-        // TODO: PowerPC 64bit mappings
-        archMapping.put(PPC64, PPC64);
-        archMapping.put("power64", PPC64);
-        archMapping.put("powerpc64", PPC64);
-        archMapping.put("power_pc64", PPC64);
-        archMapping.put("power_rs64", PPC64);
-    }
-
-    /**
-     * The main method.
-     *
-     * @param args the argv.
-     */
-    public static void main(String[] args) {
-        if (args.length >= 1) {
-            if ("--os".equals(args[0])) {
-                System.out.print(getOSName());
-                return;
-            } else if ("--arch".equals(args[0])) {
-                System.out.print(getArchName());
-                return;
-            }
-        }
-
-        System.out.print(getNativeLibFolderPathForCurrentOS());
-    }
-
-    /**
-     * Gets the native lib folder.
-     *
-     * @return the current OS's native lib folder.
-     */
-    public static String getNativeLibFolderPathForCurrentOS() {
-        return getOSName() + "/" + getArchName();
-    }
-
-    /**
-     * Gets the OS name.
-     *
-     * @return the OS name.
-     */
-    public static String getOSName() {
-        return translateOSNameToFolderName(System.getProperty("os.name"));
-    }
-
-    /**
-     * Gets the architecture name.
-     *
-     * @return the architecture name.
-     */
-    public static String getArchName() {
-        // if running Linux on ARM, need to determine ABI of JVM
-        String osArch = System.getProperty("os.arch");
-        if (osArch.startsWith("arm")
-                && System.getProperty("os.name").contains("Linux")) {
-            String javaHome = System.getProperty("java.home");
-            try {
-                // determine if first JVM found uses ARM hard-float ABI
-                String[] cmdarray = {
-                        "/bin/sh",
-                        "-c",
-                        "find '"
-                                + javaHome
-                                + "' -name 'libjvm.so' | head -1 | xargs readelf -A | "
-                                + "grep 'Tag_ABI_VFP_args: VFP registers'" };
-                int exitCode = Runtime.getRuntime().exec(cmdarray).waitFor();
-                if (exitCode == 0) {
-                    return "armhf";
-                }
-            } catch (IOException e) { //NOPMD
-                // ignored: fall back to "arm" arch (soft-float ABI)
-            } catch (InterruptedException e) { //NOPMD
-                // ignored: fall back to "arm" arch (soft-float ABI)
-            }
-        } else {
-            String lc = osArch.toLowerCase(Locale.US);
-            if (archMapping.containsKey(lc)) {
-                return archMapping.get(lc);
-            }
-        }
-        return translateArchNameToFolderName(osArch);
-    }
-
-    /**
-     * Translates the OS name to folder name.
-     *
-     * @param osName the OS name.
-     * @return the folder name.
-     */
-    static String translateOSNameToFolderName(String osName) {
-        if (osName.contains("Windows")) {
-            return "Windows";
-        } else if (osName.contains("Mac")) {
-            return "Mac";
-        } else if (osName.contains("Linux")) {
-            return "Linux";
-        } else if (osName.contains("AIX")) {
-            return "AIX";
-        }
-
-        else {
-            return osName.replaceAll("\\W", "");
-        }
-    }
-
-    /**
-     * Translates the architecture name to folder name.
-     *
-     * @param archName the architecture name.
-     * @return the folder name.
-     */
-    static String translateArchNameToFolderName(String archName) {
-        return archName.replaceAll("\\W", "");
-    }
-}


[2/4] commons-crypto git commit: Create o.a.c.crypto.Crypto as main entry point for diagnostic information

Posted by sd...@apache.org.
Create o.a.c.crypto.Crypto as main entry point for diagnostic information


Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/617a8b8f
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/617a8b8f
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/617a8b8f

Branch: refs/heads/master
Commit: 617a8b8f108396ccf6f3b20ef3ad46486f37d079
Parents: 7c328f2
Author: Benedikt Ritter <be...@gmail.com>
Authored: Tue Jun 21 20:13:51 2016 +0200
Committer: Benedikt Ritter <be...@gmail.com>
Committed: Fri Jun 24 08:53:21 2016 +0200

----------------------------------------------------------------------
 .../java/org/apache/commons/crypto/Crypto.java  | 44 ++++++++++++++++++++
 1 file changed, 44 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/617a8b8f/src/main/java/org/apache/commons/crypto/Crypto.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/Crypto.java b/src/main/java/org/apache/commons/crypto/Crypto.java
new file mode 100644
index 0000000..507e566
--- /dev/null
+++ b/src/main/java/org/apache/commons/crypto/Crypto.java
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.crypto;
+
+import org.apache.commons.crypto.utils.NativeCodeLoader;
+
+/**
+ * The Crypto class provides some diagnostic information about Commons Crypto.
+ */
+public final class Crypto {
+
+    /**
+     * Accessor to the currently active version of Apache Commons Crypto.
+     * 
+     * @return the version
+     */
+    public static String getVersion() {
+        return NativeCodeLoader.getVersion();
+    }
+
+    /**
+     * Checks whether the native code has been successfully loaded for the platform.
+     * 
+     * @return true if the native code has been loaded successful.
+     */
+    public static boolean isNativeCodeLoaded() {
+        return NativeCodeLoader.isNativeCodeLoaded();
+    }
+}


[4/4] commons-crypto git commit: Merge branch 'CRYPTO-87'

Posted by sd...@apache.org.
Merge branch 'CRYPTO-87'


Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/6529207d
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/6529207d
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/6529207d

Branch: refs/heads/master
Commit: 6529207dee6a9cc72b936a5c907305ad3f70e03c
Parents: 7c328f2 363ec74
Author: Sun Dapeng <sd...@apache.org>
Authored: Fri Jun 24 15:06:33 2016 +0800
Committer: Sun Dapeng <sd...@apache.org>
Committed: Fri Jun 24 15:06:33 2016 +0800

----------------------------------------------------------------------
 Makefile.common                                 |   4 +-
 .../java/org/apache/commons/crypto/Crypto.java  |  42 +++
 .../apache/commons/crypto/NativeCodeLoader.java | 289 +++++++++++++++++++
 .../java/org/apache/commons/crypto/OSInfo.java  | 216 ++++++++++++++
 .../apache/commons/crypto/cipher/Openssl.java   |   4 +-
 .../crypto/random/OpensslCryptoRandom.java      |   4 +-
 .../commons/crypto/utils/NativeCodeLoader.java  | 287 ------------------
 .../org/apache/commons/crypto/utils/OSInfo.java | 216 --------------
 8 files changed, 553 insertions(+), 509 deletions(-)
----------------------------------------------------------------------