Skip to content

Commit

Permalink
Use constant strings for property names
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbASF committed Nov 21, 2023
1 parent 57b16ad commit a3d4e44
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 13 deletions.
31 changes: 29 additions & 2 deletions src/main/java/org/apache/commons/crypto/Crypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,33 @@ private static Properties getComponentProperties() {
*/
public static final String LIB_TEMPDIR_KEY = Crypto.CONF_PREFIX + "lib.tempdir";

// property names related to SSL crypto library loading

/**
* Where to find the SSL crypto library when using JNI
* This is used in Utils.libraryPath()
*/
public static final String JNI_LIBRARY_PATH = "jni.library.path";

/**
* Override property for the default SSL crypto library name when using JNI
*/
public static final String JNI_LIBRARY_NAME = "jni.library.name";

/**
* Where to find the SSL crypto library when using JNA
* This is used by the JNA library code
*/
public static final String JNA_LIBRARY_PATH = "jna.library.path";

/**
* Override property for the default SSL crypto library name when using JNA
*/
public static final String JNA_LIBRARY_NAME = Crypto.CONF_PREFIX + "OpenSslNativeJna";

/** Default name for loading SSL crypto library using JNA */
public static final String JNA_LIBRARY_NAME_DEFAULT = "crypto";

private static boolean quiet;

/**
Expand Down Expand Up @@ -152,8 +179,8 @@ public static boolean isNativeCodeLoaded() {
*/
public static void main(final String[] args) throws Exception {
quiet = args.length == 1 && args[0].equals("-q");
info("jni.library.path=%s", System.getProperty("jni.library.path"));
info("jni.library.name=%s", System.getProperty("jni.library.name"));
info("%s=%s", JNI_LIBRARY_PATH, System.getProperty(JNI_LIBRARY_PATH));
info("%s=%s", JNI_LIBRARY_NAME, System.getProperty(JNI_LIBRARY_NAME));
info("%s %s", getComponentName(), getComponentVersion());
if (isNativeCodeLoaded()) {
info("Native code loaded OK: %s", OpenSslInfoNative.NativeVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class LibreSsl20XNativeJna implements OpenSslInterfaceNativeJna {
boolean ok = false;
Throwable thrown = null;
try {
final String libName = System.getProperty(Crypto.CONF_PREFIX + OpenSslNativeJna.class.getSimpleName(), "crypto");
final String libName = System.getProperty(Crypto.JNA_LIBRARY_NAME, Crypto.JNA_LIBRARY_NAME_DEFAULT);
OpenSslJna.debug("Native.register('%s')", libName);
Native.register(libName);
ok = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class OpenSsl11XNativeJna implements OpenSslInterfaceNativeJna {
boolean ok = false;
Throwable thrown = null;
try {
final String libName = System.getProperty(Crypto.CONF_PREFIX + OpenSslNativeJna.class.getSimpleName(), "crypto");
final String libName = System.getProperty(Crypto.JNA_LIBRARY_NAME, Crypto.JNA_LIBRARY_NAME_DEFAULT);
OpenSslJna.debug("Native.register('%s')", libName);
Native.register(libName);
ok = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class OpenSsl30XNativeJna implements OpenSslInterfaceNativeJna {
boolean ok = false;
Throwable thrown = null;
try {
final String libName = System.getProperty(Crypto.CONF_PREFIX + OpenSslNativeJna.class.getSimpleName(), "crypto");
final String libName = System.getProperty(Crypto.JNA_LIBRARY_NAME, Crypto.JNA_LIBRARY_NAME_DEFAULT);
OpenSslJna.debug("Native.register('%s')", libName);
Native.register(libName);
ok = true;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public static boolean isEnabled() {
*/
public static void main(final String[] args) throws Throwable {
// These are used by JNA code if defined:
info("jna.library.path=%s", System.getProperty("jna.library.path"));
info("%s=%s", Crypto.JNA_LIBRARY_PATH, System.getProperty(Crypto.JNA_LIBRARY_PATH));
info("jna.platform.library.path=%s", System.getProperty("jna.platform.library.path"));
info("commons.crypto.OpenSslNativeJna=%s\n", System.getProperty("commons.crypto.OpenSslNativeJna"));
info("%s=%s\n", Crypto.JNA_LIBRARY_NAME, System.getProperty(Crypto.JNA_LIBRARY_NAME));
// can set jna.debug_load=true for loading info
info(Crypto.getComponentName() + " OpenSslJna: enabled = %s, version = 0x%08X", isEnabled(), OpenSslNativeJna.VERSION);
final Throwable initialisationError = initialisationError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class OpenSslNativeJna {

static {
OpenSslJna.debug("OpenSslNativeJna static init start");
final String libraryName = System.getProperty(Crypto.CONF_PREFIX + OpenSslNativeJna.class.getSimpleName(), "crypto");
final String libraryName = System.getProperty(Crypto.JNA_LIBRARY_NAME, Crypto.JNA_LIBRARY_NAME_DEFAULT);
OpenSslJna.debug("OpenSslNativeJna NativeLibrary.getInstance('%s')", libraryName);
@SuppressWarnings("resource") // NativeLibrary.getInstance returns a singleton
final NativeLibrary crypto = NativeLibrary.getInstance(libraryName);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/apache/commons/crypto/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ public static Properties getProperties(final Properties newProp) {
}

/*
* Override the default DLL name if jni.library.path is a valid directory
* If jni.library.name is defined, this overrides the default name.
* Override the default DLL name if Crypto.JNI_LIBRARY_PATH is a valid directory
* If Crypto.JNI_LIBRARY_NAME is defined, this overrides the default name.
*
* @param name - the default name, passed from native code
* @return the updated library path
Expand All @@ -207,13 +207,13 @@ public static Properties getProperties(final Properties newProp) {
* In each case, there is a link from the canonical name (libcrypto.xx) to the versioned name (libcrypto-1.2.3.xx)
* However on Windows, all the DLL versions seem to be stored in the same directory.
* This means that Windows code needs to be given the versioned name (e.g. libcrypto-1_1-x64)
* This is done by defining jni.library.name.
* This is done by defining Crypto.JNI_LIBRARY_NAME.
*
* Do not change the method name or its signature!
*/
static String libraryPath(final String name) {
final String overridename = System.getProperty("jni.library.name", name);
final String override = System.getProperty("jni.library.path");
final String overridename = System.getProperty(Crypto.JNI_LIBRARY_NAME, name);
final String override = System.getProperty(Crypto.JNI_LIBRARY_PATH);
if (override != null && new File(override).isDirectory()) {
return new File(override, overridename).getPath();
}
Expand Down

0 comments on commit a3d4e44

Please sign in to comment.