You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/08/23 14:45:09 UTC
[commons-crypto] 02/02: Javadoc. Better logging.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-crypto.git
commit 07a9746cab567397751f4a0357320f2ad6d80737
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 23 10:45:01 2020 -0400
Javadoc. Better logging.
---
.../java/org/apache/commons/crypto/Crypto.java | 7 +-
.../apache/commons/crypto/NativeCodeLoader.java | 330 +++++++++++----------
2 files changed, 174 insertions(+), 163 deletions(-)
diff --git a/src/main/java/org/apache/commons/crypto/Crypto.java b/src/main/java/org/apache/commons/crypto/Crypto.java
index bf92e32..ed49610 100644
--- a/src/main/java/org/apache/commons/crypto/Crypto.java
+++ b/src/main/java/org/apache/commons/crypto/Crypto.java
@@ -34,7 +34,7 @@ import org.apache.commons.crypto.random.CryptoRandomFactory;
public final class Crypto {
/**
- * The prefix of all crypto configuration keys
+ * The prefix of all crypto configuration keys.
*/
public static final String CONF_PREFIX = "commons.crypto.";
@@ -156,8 +156,9 @@ public final class Crypto {
final Properties props = new Properties();
props.setProperty(CryptoCipherFactory.CLASSES_KEY,
CryptoCipherFactory.CipherProvider.OPENSSL.getClassName());
- try (CryptoCipher cryptoCipher = CryptoCipherFactory.getCryptoCipher("AES/CTR/NoPadding", props)) {
- System.out.println("Cipher instance created OK: " + cryptoCipher);
+ final String transformation = "AES/CTR/NoPadding";
+ try (CryptoCipher cryptoCipher = CryptoCipherFactory.getCryptoCipher(transformation, props)) {
+ System.out.printf("Cipher %s instance created OK: %s%n", transformation, cryptoCipher);
}
}
System.out.println("Additional OpenSSL_version(n) details:");
diff --git a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
index 6c76a8d..b466e68 100644
--- a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
+++ b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
@@ -36,124 +36,123 @@ import org.apache.commons.crypto.utils.Utils;
*/
final class NativeCodeLoader {
- private static final Throwable loadingError;
+ private static final Throwable loadingError;
- private final static boolean nativeCodeLoaded;
+ private final static boolean nativeCodeLoaded;
- static {
- loadingError = loadLibrary(); // will be null if loaded OK
+ static {
+ loadingError = loadLibrary(); // will be null if loaded OK
- nativeCodeLoaded = loadingError == null;
- }
-
- /**
- * 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);
- }
+ nativeCodeLoaded = loadingError == null;
+ }
- int ch = in1.read();
- while (ch != -1) {
- final int ch2 = in2.read();
- if (ch != ch2) {
- return false;
- }
- ch = in1.read();
- }
- final int ch2 = in2.read();
- return ch2 == -1;
- }
+ /**
+ * 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);
+ }
- /**
- * 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(final String libFolderForCurrentOS,
- final String libraryFileName, final String targetFolder) {
- final String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
+ int ch = in1.read();
+ while (ch != -1) {
+ final int ch2 = in2.read();
+ if (ch != ch2) {
+ return false;
+ }
+ ch = in1.read();
+ }
+ final int ch2 = in2.read();
+ return ch2 == -1;
+ }
- // Attach UUID to the native library file to ensure multiple class
- // loaders
- // can read the libcommons-crypto multiple times.
- final String uuid = UUID.randomUUID().toString();
- final String extractedLibFileName = String.format("commons-crypto-%s-%s", uuid, libraryFileName);
- final File extractedLibFile = new File(targetFolder, extractedLibFileName);
+ /**
+ * 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(final String libFolderForCurrentOS, final String libraryFileName,
+ final String targetFolder) {
+ final String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
- InputStream inputStream = null;
- try {
- // Extract a native library file into the target directory
- inputStream = NativeCodeLoader.class.getResourceAsStream(nativeLibraryFilePath);
- try (FileOutputStream writer = new FileOutputStream(extractedLibFile)) {
- final byte[] buffer = new byte[8192];
- int bytesRead;
- while ((bytesRead = inputStream.read(buffer)) != -1) {
- writer.write(buffer, 0, bytesRead);
- }
- } finally {
- // Delete the extracted lib file on JVM exit.
- extractedLibFile.deleteOnExit();
+ // Attach UUID to the native library file to ensure multiple class
+ // loaders
+ // can read the libcommons-crypto multiple times.
+ final String uuid = UUID.randomUUID().toString();
+ final String extractedLibFileName = String.format("commons-crypto-%s-%s", uuid, libraryFileName);
+ final File extractedLibFile = new File(targetFolder, extractedLibFileName);
- IoUtils.closeQuietly(inputStream);
- inputStream = null;
- }
+ InputStream inputStream = null;
+ try {
+ // Extract a native library file into the target directory
+ inputStream = NativeCodeLoader.class.getResourceAsStream(nativeLibraryFilePath);
+ try (FileOutputStream writer = new FileOutputStream(extractedLibFile)) {
+ final byte[] buffer = new byte[8192];
+ int bytesRead;
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
+ writer.write(buffer, 0, bytesRead);
+ }
+ } finally {
+ // Delete the extracted lib file on JVM exit.
+ extractedLibFile.deleteOnExit();
- // 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");
- }
+ IoUtils.closeQuietly(inputStream);
+ inputStream = null;
+ }
- // 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();
- }
- }
- }
+ // 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");
+ }
- return extractedLibFile;
- } catch (final IOException e) {
- return null;
- } finally {
- IoUtils.closeQuietly(inputStream);
- }
- }
+ // 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();
+ }
+ }
+ }
+ debug("Extracted '%s' to '%s'", nativeLibraryFilePath, extractedLibFile);
+ return extractedLibFile;
+ } catch (final IOException e) {
+ return null;
+ } finally {
+ IoUtils.closeQuietly(inputStream);
+ }
+ }
- /**
- * Finds the native library.
- *
- * @return the jar file.
- */
+ /**
+ * Finds the native library.
+ *
+ * @return the jar file.
+ */
private static File findNativeLibrary() {
// Get the properties once
final Properties props = Utils.getDefaultProperties();
@@ -186,13 +185,13 @@ final class NativeCodeLoader {
}
if (!hasNativeLib) {
- final String errorMessage = String.format("no native library is found for os.name=%s and os.arch=%s",
+ final 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
+ // Crypto.LIB_TEMPDIR_KEY or java.io.tmpdir
final String tempFolder = new File(
props.getProperty(Crypto.LIB_TEMPDIR_KEY, System.getProperty("java.io.tmpdir"))).getAbsolutePath();
@@ -200,60 +199,71 @@ final class NativeCodeLoader {
return extractLibraryFile(nativeLibraryPath, nativeLibraryName, tempFolder);
}
- /**
- * Gets the error cause if loading failed.
- *
- * @return null, unless loading failed
- */
- static Throwable getLoadingError() {
- return loadingError;
- }
+ /**
+ * Gets the error cause if loading failed.
+ *
+ * @return null, unless loading failed
+ */
+ static Throwable getLoadingError() {
+ return loadingError;
+ }
- /**
- * Checks whether the given path has resource.
- *
- * @param path the path.
- * @return the boolean.
- */
- private static boolean hasResource(final String path) {
- return NativeCodeLoader.class.getResource(path) != null;
- }
+ /**
+ * Checks whether the given path has resource.
+ *
+ * @param path the path.
+ * @return the boolean.
+ */
+ private static boolean hasResource(final String path) {
+ return NativeCodeLoader.class.getResource(path) != null;
+ }
+
+ /**
+ * Checks whether native code is loaded for this platform.
+ *
+ * @return {@code true} if native is loaded, else {@code false}.
+ */
+ static boolean isNativeCodeLoaded() {
+ return nativeCodeLoaded;
+ }
- /**
- * Checks whether native code is loaded for this platform.
- *
- * @return {@code true} if native is loaded, else {@code false}.
- */
- static boolean isNativeCodeLoaded() {
- return nativeCodeLoaded;
- }
+ /**
+ * Loads the library if possible.
+ *
+ * @return null if successful, otherwise the Throwable that was caught
+ */
+ static Throwable loadLibrary() {
+ try {
+ final 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");
+ }
+ return null; // OK
+ } catch (final Exception t) {
+ return t;
+ } catch (final UnsatisfiedLinkError t) {
+ return t;
+ }
+ }
- /**
- * Loads the library if possible.
- *
- * @return null if successful, otherwise the Throwable that was caught
- */
- static Throwable loadLibrary() {
- try {
- final 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");
- }
- return null; // OK
- } catch (final Exception t) {
- return t;
- } catch (final UnsatisfiedLinkError t) {
- return t;
- }
- }
+ /**
+ * Logs debug information.
+ *
+ * @param format See {@link String#format(String, Object...)}.
+ * @param args See {@link String#format(String, Object...)}.
+ */
+ static void debug(final String format, Object... args) {
+ // TODO Find a better way to do this later.
+ // System.out.println(String.format(format, args));
+ }
- /**
- * The private constructor of {@link NativeCodeLoader}.
- */
- private NativeCodeLoader() {
- }
+ /**
+ * The private constructor of {@link NativeCodeLoader}.
+ */
+ private NativeCodeLoader() {
+ }
}