Fixed issues when deployed in a diffeerent (non-jar) container.

This commit is contained in:
nathan 2016-03-13 20:43:27 +01:00
parent b1d4d1fc77
commit a7a3fa6091
6 changed files with 105 additions and 123 deletions

View File

@ -91,7 +91,7 @@ This project is **kept in sync** with the utilities library, so "jar hell" is no
<dependency> <dependency>
<groupId>com.dorkbox</groupId> <groupId>com.dorkbox</groupId>
<artifactId>Network</artifactId> <artifactId>Network</artifactId>
<version>1.6</version> <version>1.8</version>
</dependency> </dependency>
``` ```

View File

@ -22,7 +22,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -143,150 +142,133 @@ class SocketUDT {
* @throws RuntimeException * @throws RuntimeException
*/ */
static { static {
boolean isNativeDeployed = false; // we are either from a jar or as source
try { ProtectionDomain pDomain = SocketUDT.class.getProtectionDomain();
Class<?> exitClass = Class.forName("dorkbox.exit.Exit"); CodeSource cSource = pDomain.getCodeSource();
if (exitClass != null) { // file:/X:/workspace/XYZ/classes/ when it's in ide/flat
Method nativeMethod = exitClass.getMethod("isNative"); // jar:/X:/workspace/XYZ/jarname.jar when it's jar
Object invoke = nativeMethod.invoke(null); URL loc = cSource.getLocation();
if (invoke != null) { final String path = loc.getPath();
isNativeDeployed = (Boolean) invoke; final boolean isContainer = path.endsWith(".jar") || path.endsWith(".box");
}
}
} catch (Throwable t) {
}
// if we are natively deployed, we automatically load the native libraries as part of the startup procedure final OsType os = OS.get();
// if we are not, we are either from a jar or as source String osName = os.getName();
if (!isNativeDeployed) { boolean loaded = false;
ProtectionDomain pDomain = SocketUDT.class.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
// file:/X:/workspace/XYZ/classes/ when it's in ide/flat if (isContainer) {
// jar:/X:/workspace/XYZ/jarname.jar when it's jar // have to extract our correct file to temp then load it, ONLY if we are not already loaded!
URL loc = cSource.getLocation();
final String path = loc.getPath();
final boolean isJar = path.endsWith(".jar");
final OsType os = OS.get(); try {
String osName = os.getName(); MessageDigest digest = MessageDigest.getInstance("MD5");
boolean loaded = false; digest.update(TypeUDT.class.getName()
.getBytes());
if (isJar) { // convert to alpha-numeric. see https://stackoverflow.com/questions/29183818/why-use-tostring32-and-not-tostring36
// have to extract our correct file to temp then load it, ONLY if we are not already loaded! final String outputFileName = "UDT_driver_" + new BigInteger(1, digest.digest()).toString(32)
.toUpperCase(Locale.US);
try { final String tempDir = System.getProperty("java.io.tmpdir");
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(TypeUDT.class.getName()
.getBytes());
// convert to alpha-numeric. see https://stackoverflow.com/questions/29183818/why-use-tostring32-and-not-tostring36 final File file = new File(tempDir, outputFileName);
final String outputFileName = "UDT_driver_" + new BigInteger(1, digest.digest()).toString(32) if (!file.canRead()) {
.toUpperCase(Locale.US); // we need to iterate this jar to get the files
final String tempDir = System.getProperty("java.io.tmpdir"); final String packageName = TypeUDT.class.getPackage()
.getName()
.replaceAll("\\.", "/");
final File file = new File(tempDir, outputFileName); final String prefix = packageName + "/natives/" + osName + "/";
if (!file.canRead()) { final List<String> list = Arrays.asList(os.getLibraryNames());
// we need to iterate this jar to get the files
final String packageName = TypeUDT.class.getPackage() final String jarFileName = URLDecoder.decode(loc.getPath(), "UTF-8");
.getName() JarFile jar = new JarFile(jarFileName);
.replaceAll("\\.", "/"); Enumeration<JarEntry> entries = jar.entries();
JAR_READ:
while (entries.hasMoreElements()) {
final JarEntry jarEntry = entries.nextElement();
String name = jarEntry.getName();
final String prefix = packageName + "/natives/" + osName + "/"; if (name.startsWith(prefix)) {
final List<String> list = Arrays.asList(os.getLibraryNames()); for (String s : list) {
if (name.endsWith(s)) {
// there is only one!
final String jarFileName = URLDecoder.decode(loc.getPath(), "UTF-8"); // now we copy it out
JarFile jar = new JarFile(jarFileName); final InputStream inputStream = jar.getInputStream(jarEntry);
Enumeration<JarEntry> entries = jar.entries();
JAR_READ:
while (entries.hasMoreElements()) {
final JarEntry jarEntry = entries.nextElement();
String name = jarEntry.getName();
if (name.startsWith(prefix)) { OutputStream outStream = null;
for (String s : list) { try {
if (name.endsWith(s)) { outStream = new FileOutputStream(file);
// there is only one!
// now we copy it out byte[] buffer = new byte[2048];
final InputStream inputStream = jar.getInputStream(jarEntry); int read;
while ((read = inputStream.read(buffer)) > 0) {
OutputStream outStream = null; outStream.write(buffer, 0, read);
try { }
outStream = new FileOutputStream(file); } catch (IOException e) {
log.error("Error extracting library from: " + jarFileName, e.getMessage());
byte[] buffer = new byte[2048]; } finally {
int read; try {
while ((read = inputStream.read(buffer)) > 0) { inputStream.close();
outStream.write(buffer, 0, read); } catch (Exception ignored) {
} }
} catch (IOException e) { try {
log.error("Error extracting library from: " + jarFileName, e.getMessage()); if (outStream != null) {
} finally { outStream.close();
try { }
inputStream.close(); } catch (Exception ignored) {
} catch (Exception ignored) {
}
try {
if (outStream != null) {
outStream.close();
}
} catch (Exception ignored) {
}
} }
break JAR_READ;
} }
break JAR_READ;
} }
} }
} }
jar.close();
} }
log.info("Loading release libraries."); jar.close();
}
System.load(file.getAbsolutePath()); log.info("Loading release libraries.");
System.load(file.getAbsolutePath());
log.info("Release libraries loaded.");
loaded = true;
} catch (NoSuchAlgorithmException e) {
log.error("Error loading MD5 checksum.", e.getMessage());
} catch (UnsupportedEncodingException e) {
log.error("Error parsing text.", e.getMessage());
} catch (IOException e) {
log.error("Error extracting library.", e.getMessage());
}
}
else {
try {
log.info("Loading release libraries.");
final URI uri = TypeUDT.class.getResource("natives/" + osName + "/")
.toURI();
final String host = uri.getPath();
File libPath = new File(host).getAbsoluteFile();
if (libPath.canRead()) {
List<File> libs = FileUtil.parseDir(libPath, os.getLibraryNames());
for (File lib : libs) {
// load the libs in that dir (there will be only one)
System.load(lib.getAbsolutePath());
break;
}
log.info("Release libraries loaded."); log.info("Release libraries loaded.");
loaded = true; loaded = true;
} catch (NoSuchAlgorithmException e) {
log.error("Error loading MD5 checksum.", e.getMessage());
} catch (UnsupportedEncodingException e) {
log.error("Error parsing text.", e.getMessage());
} catch (IOException e) {
log.error("Error extracting library.", e.getMessage());
} }
} catch (final Throwable e) {
log.error("Release libraries missing: {}", e.getMessage());
} }
else { }
try {
log.info("Loading release libraries.");
final URI uri = TypeUDT.class.getResource("natives/" + osName + "/") if (!loaded) {
.toURI(); log.error("Failed to load UDT native library");
final String host = uri.getPath(); throw new RuntimeException("Failed to load UDT native library");
File libPath = new File(host).getAbsoluteFile();
if (libPath.canRead()) {
List<File> libs = FileUtil.parseDir(libPath, os.getLibraryNames());
for (File lib : libs) {
// load the libs in that dir (there will be only one)
System.load(lib.getAbsolutePath());
break;
}
log.info("Release libraries loaded.");
loaded = true;
}
} catch (final Throwable e) {
log.error("Release libraries missing: {}", e.getMessage());
}
}
if (!loaded) {
log.error("Failed to load UDT native library");
throw new RuntimeException("Failed to load UDT native library");
}
} }
try { try {

View File

@ -48,7 +48,7 @@ class Broadcast {
*/ */
public static public static
String getVersion() { String getVersion() {
return "1.6"; return "1.8";
} }
/** /**

View File

@ -66,7 +66,7 @@ class Client<C extends Connection> extends EndPointClient<C> implements Connecti
*/ */
public static public static
String getVersion() { String getVersion() {
return "1.6"; return "1.8";
} }
/** /**

View File

@ -104,7 +104,7 @@ class DnsClient {
*/ */
public static public static
String getVersion() { String getVersion() {
return "1.6"; return "1.8";
} }
/** /**

View File

@ -63,7 +63,7 @@ class Server<C extends Connection> extends EndPointServer<C> {
*/ */
public static public static
String getVersion() { String getVersion() {
return "1.6"; return "1.8";
} }
/** /**