Enforce artifact name in path inside artifact jar and allow for customization of library file name.
This commit is contained in:
@@ -53,13 +53,18 @@ public class Native {
|
||||
}
|
||||
|
||||
public static void load(String group, String name) {
|
||||
load(group, name, name);
|
||||
}
|
||||
|
||||
public static void load(String group, String name, String file) {
|
||||
init(null);
|
||||
try {
|
||||
loader.load(group, Platform.current().getLibraryName(name));
|
||||
loader.load(group, name, Platform.current().getLibraryName(file));
|
||||
} catch (NativeException e) {
|
||||
throw e;
|
||||
} catch (Throwable t) {
|
||||
throw new NativeException("Failed to load native library.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,9 @@ package com.github.boukefalos.jlibloader;
|
||||
* Thrown when a given integration is not available for the current machine.
|
||||
*/
|
||||
public class NativeLibraryUnavailableException extends NativeException {
|
||||
public NativeLibraryUnavailableException(String message) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public NativeLibraryUnavailableException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,13 @@ package com.github.boukefalos.jlibloader.internal;
|
||||
public class LibraryDef {
|
||||
final String group;
|
||||
final String name;
|
||||
final String file;
|
||||
final String platform;
|
||||
|
||||
public LibraryDef(String group, String name, String platform) {
|
||||
public LibraryDef(String group, String name, String file, String platform) {
|
||||
this.group = group;
|
||||
this.name = name;
|
||||
this.file = file;
|
||||
this.platform = platform;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,12 +33,12 @@ public class NativeLibraryLoader {
|
||||
this.nativeLibraryLocator = nativeLibraryLocator;
|
||||
}
|
||||
|
||||
public void load(String libraryGroupName, String libraryFileName) {
|
||||
public void load(String libraryGroupName, String libraryName, String libraryFileName) {
|
||||
if (loaded.contains(libraryFileName)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
File libFile = nativeLibraryLocator.find(new LibraryDef(libraryGroupName, libraryFileName, platform.getId()));
|
||||
File libFile = nativeLibraryLocator.find(new LibraryDef(libraryGroupName, libraryName, libraryFileName, platform.getId()));
|
||||
if (libFile == null) {
|
||||
throw new NativeLibraryUnavailableException(String.format("Native library '%s' is not available for %s.", libraryFileName, platform));
|
||||
}
|
||||
@@ -46,6 +46,7 @@ public class NativeLibraryLoader {
|
||||
} catch (NativeException e) {
|
||||
throw e;
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
throw new NativeException(String.format("Failed to load native library '%s' for %s.", libraryFileName, platform), t);
|
||||
}
|
||||
loaded.add(libraryFileName);
|
||||
|
||||
@@ -34,9 +34,10 @@ public class NativeLibraryLocator {
|
||||
}
|
||||
|
||||
public File find(LibraryDef libraryDef) throws IOException {
|
||||
String resourceName = String.format("%s/%s/%s", libraryDef.getGroupPath(), libraryDef.platform, libraryDef.name);
|
||||
String resourceName = String.format("%s/%s/%s/%s", libraryDef.getGroupPath(), libraryDef.name, libraryDef.platform, libraryDef.file);
|
||||
System.out.println(resourceName);
|
||||
if (extractDir != null) {
|
||||
File libFile = new File(extractDir, String.format("%s/%s", libraryDef.platform, libraryDef.name));
|
||||
File libFile = new File(extractDir, String.format("%s/%s", libraryDef.platform, libraryDef.file));
|
||||
File lockFile = new File(libFile.getParentFile(), libFile.getName() + ".lock");
|
||||
lockFile.getParentFile().mkdirs();
|
||||
lockFile.createNewFile();
|
||||
@@ -63,27 +64,28 @@ public class NativeLibraryLocator {
|
||||
URL resource = getClass().getClassLoader().getResource(resourceName);
|
||||
if (resource != null) {
|
||||
File libFile;
|
||||
File libDir = File.createTempFile(libraryDef.name, "dir");
|
||||
File libDir = File.createTempFile(libraryDef.file, "dir");
|
||||
libDir.delete();
|
||||
libDir.mkdirs();
|
||||
libFile = new File(libDir, libraryDef.name);
|
||||
libFile = new File(libDir, libraryDef.file);
|
||||
libFile.deleteOnExit();
|
||||
libDir.deleteOnExit();
|
||||
copy(resource, libFile);
|
||||
return libFile;
|
||||
}
|
||||
}
|
||||
|
||||
String componentName = libraryDef.name.replaceFirst("^lib", "").replaceFirst("\\.\\w+$", "");
|
||||
String componentName = libraryDef.file.replaceFirst("^lib", "").replaceFirst("\\.\\w+$", "");
|
||||
int pos = componentName.indexOf("-");
|
||||
while (pos >= 0) {
|
||||
componentName = componentName.substring(0, pos) + Character.toUpperCase(componentName.charAt(pos + 1)) + componentName.substring(pos + 2);
|
||||
pos = componentName.indexOf("-", pos);
|
||||
}
|
||||
File libFile = new File(String.format("build/binaries/%sSharedLibrary/%s/%s", componentName, libraryDef.platform.replace("-", "_"), libraryDef.name));
|
||||
File libFile = new File(String.format("build/binaries/%sSharedLibrary/%s/%s", componentName, libraryDef.platform.replace("-", "_"), libraryDef.file));
|
||||
if (libFile.isFile()) {
|
||||
return libFile;
|
||||
}
|
||||
libFile = new File(String.format("build/binaries/mainSharedLibrary/%s/%s", libraryDef.platform.replace("-", "_"), libraryDef.name));
|
||||
libFile = new File(String.format("build/binaries/mainSharedLibrary/%s/%s", libraryDef.platform.replace("-", "_"), libraryDef.file));
|
||||
if (libFile.isFile()) {
|
||||
return libFile;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user