Enforce artifact name in path inside artifact jar and allow for customization of library file name.

This commit is contained in:
2014-11-26 14:29:23 +00:00
parent ad96daeaba
commit e8a7cee715
5 changed files with 25 additions and 13 deletions

View File

@@ -53,13 +53,18 @@ public class Native {
} }
public static void load(String group, String name) { public static void load(String group, String name) {
load(group, name, name);
}
public static void load(String group, String name, String file) {
init(null); init(null);
try { try {
loader.load(group, Platform.current().getLibraryName(name)); loader.load(group, name, Platform.current().getLibraryName(file));
} catch (NativeException e) { } catch (NativeException e) {
throw e; throw e;
} catch (Throwable t) { } catch (Throwable t) {
throw new NativeException("Failed to load native library."); throw new NativeException("Failed to load native library.");
} }
}
}
} }

View File

@@ -20,7 +20,9 @@ package com.github.boukefalos.jlibloader;
* Thrown when a given integration is not available for the current machine. * Thrown when a given integration is not available for the current machine.
*/ */
public class NativeLibraryUnavailableException extends NativeException { public class NativeLibraryUnavailableException extends NativeException {
public NativeLibraryUnavailableException(String message) { private static final long serialVersionUID = 1L;
public NativeLibraryUnavailableException(String message) {
super(message); super(message);
} }
} }

View File

@@ -3,11 +3,13 @@ package com.github.boukefalos.jlibloader.internal;
public class LibraryDef { public class LibraryDef {
final String group; final String group;
final String name; final String name;
final String file;
final String platform; 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.group = group;
this.name = name; this.name = name;
this.file = file;
this.platform = platform; this.platform = platform;
} }

View File

@@ -33,12 +33,12 @@ public class NativeLibraryLoader {
this.nativeLibraryLocator = nativeLibraryLocator; this.nativeLibraryLocator = nativeLibraryLocator;
} }
public void load(String libraryGroupName, String libraryFileName) { public void load(String libraryGroupName, String libraryName, String libraryFileName) {
if (loaded.contains(libraryFileName)) { if (loaded.contains(libraryFileName)) {
return; return;
} }
try { 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) { if (libFile == null) {
throw new NativeLibraryUnavailableException(String.format("Native library '%s' is not available for %s.", libraryFileName, platform)); 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) { } catch (NativeException e) {
throw e; throw e;
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace();
throw new NativeException(String.format("Failed to load native library '%s' for %s.", libraryFileName, platform), t); throw new NativeException(String.format("Failed to load native library '%s' for %s.", libraryFileName, platform), t);
} }
loaded.add(libraryFileName); loaded.add(libraryFileName);

View File

@@ -34,9 +34,10 @@ public class NativeLibraryLocator {
} }
public File find(LibraryDef libraryDef) throws IOException { 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) { 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"); File lockFile = new File(libFile.getParentFile(), libFile.getName() + ".lock");
lockFile.getParentFile().mkdirs(); lockFile.getParentFile().mkdirs();
lockFile.createNewFile(); lockFile.createNewFile();
@@ -63,27 +64,28 @@ public class NativeLibraryLocator {
URL resource = getClass().getClassLoader().getResource(resourceName); URL resource = getClass().getClassLoader().getResource(resourceName);
if (resource != null) { if (resource != null) {
File libFile; File libFile;
File libDir = File.createTempFile(libraryDef.name, "dir"); File libDir = File.createTempFile(libraryDef.file, "dir");
libDir.delete(); libDir.delete();
libDir.mkdirs(); libDir.mkdirs();
libFile = new File(libDir, libraryDef.name); libFile = new File(libDir, libraryDef.file);
libFile.deleteOnExit(); libFile.deleteOnExit();
libDir.deleteOnExit();
copy(resource, libFile); copy(resource, libFile);
return libFile; return libFile;
} }
} }
String componentName = libraryDef.name.replaceFirst("^lib", "").replaceFirst("\\.\\w+$", ""); String componentName = libraryDef.file.replaceFirst("^lib", "").replaceFirst("\\.\\w+$", "");
int pos = componentName.indexOf("-"); int pos = componentName.indexOf("-");
while (pos >= 0) { while (pos >= 0) {
componentName = componentName.substring(0, pos) + Character.toUpperCase(componentName.charAt(pos + 1)) + componentName.substring(pos + 2); componentName = componentName.substring(0, pos) + Character.toUpperCase(componentName.charAt(pos + 1)) + componentName.substring(pos + 2);
pos = componentName.indexOf("-", pos); 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()) { if (libFile.isFile()) {
return libFile; 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()) { if (libFile.isFile()) {
return libFile; return libFile;
} }