Adding support for tcnative fedora flavour in uber jar

Motivation:

We want to allow the use of an uber jar that contains shared dynamic libraries for all platforms (including fedora).

Modifications:

Modified OpenSsl to try and load the fedora library if the OS is Linux and the platform specified library fails before using the default lib.

Result:

True uber support.
This commit is contained in:
johnou 2016-03-02 20:18:45 +01:00 committed by Norman Maurer
parent 6a2425b846
commit 26811b53ab
2 changed files with 36 additions and 13 deletions

View File

@ -24,6 +24,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
/** /**
@ -147,6 +148,26 @@ public final class NativeLibraryLoader {
return OSNAME.startsWith("macosx") || OSNAME.startsWith("osx"); return OSNAME.startsWith("macosx") || OSNAME.startsWith("osx");
} }
/**
* Loads the first available library in the collection with the specified
* {@link ClassLoader}.
*
* @throws IllegalArgumentException
* if none of the given libraries load successfully.
*/
public static void loadFirstAvailable(ClassLoader loader, String... names) {
for (String name : names) {
try {
NativeLibraryLoader.load(name, loader);
return;
} catch (Throwable t) {
logger.debug("Unable to load the library: " + name + '.', t);
}
}
throw new IllegalArgumentException("Failed to load any of the given libraries: "
+ Arrays.toString(names));
}
/** /**
* Load the given library with the specified {@link java.lang.ClassLoader} * Load the given library with the specified {@link java.lang.ClassLoader}
*/ */

View File

@ -39,6 +39,7 @@ import java.util.Set;
public final class OpenSsl { public final class OpenSsl {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(OpenSsl.class); private static final InternalLogger logger = InternalLoggerFactory.getInstance(OpenSsl.class);
private static final String LINUX = "linux";
private static final String UNKNOWN = "unknown"; private static final String UNKNOWN = "unknown";
private static final Throwable UNAVAILABILITY_CAUSE; private static final Throwable UNAVAILABILITY_CAUSE;
@ -62,19 +63,20 @@ public final class OpenSsl {
try { try {
String os = normalizeOs(SystemPropertyUtil.get("os.name", "")); String os = normalizeOs(SystemPropertyUtil.get("os.name", ""));
String arch = normalizeArch(SystemPropertyUtil.get("os.arch", "")); String arch = normalizeArch(SystemPropertyUtil.get("os.arch", ""));
String libName = "netty-tcnative-" + os + '-' + arch;
try {
// First, try loading the platform-specific library. Platform-specific
// libraries will be available if using a tcnative uber jar.
NativeLibraryLoader.load(libName, SSL.class.getClassLoader());
} catch (Throwable t) {
// Either the uber jar isn't on classpath, or it doesn't contain a library
// for this platform. Load the default library.
logger.debug("Unable to load platform-specific netty-tcnative library: " +
libName + ". Will attempt to load the default netty-tcnative library.", t);
NativeLibraryLoader.load("netty-tcnative", SSL.class.getClassLoader()); Set<String> libNames = new LinkedHashSet<String>(3);
// First, try loading the platform-specific library. Platform-specific
// libraries will be available if using a tcnative uber jar.
libNames.add("netty-tcnative-" + os + '-' + arch);
if (LINUX.equalsIgnoreCase(os)) {
// Fedora SSL lib so naming (libssl.so.10 vs libssl.so.1.0.0)..
libNames.add("netty-tcnative-" + os + '-' + arch + "-fedora");
} }
// finally the default library.
libNames.add("netty-tcnative");
NativeLibraryLoader.loadFirstAvailable(SSL.class.getClassLoader(),
libNames.toArray(new String[libNames.size()]));
Library.initialize("provided"); Library.initialize("provided");
SSL.initialize(null); SSL.initialize(null);
@ -231,8 +233,8 @@ public final class OpenSsl {
return "os400"; return "os400";
} }
} }
if (value.startsWith("linux")) { if (value.startsWith(LINUX)) {
return "linux"; return LINUX;
} }
if (value.startsWith("macosx") || value.startsWith("osx")) { if (value.startsWith("macosx") || value.startsWith("osx")) {
return "osx"; return "osx";