[Arakhnę-Dev] [99] Make more robust the loading of native libraries. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 99
Author: galland
Date: 2009-12-23 10:32:10 +0100 (Wed, 23 Dec 2009)
Log Message:
-----------
Make more robust the loading of native libraries.
Modified Paths:
--------------
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystem.java
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java 2009-12-01 08:07:01 UTC (rev 98)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java 2009-12-23 09:32:10 UTC (rev 99)
@@ -116,7 +116,7 @@
* @return the URL where the specified library was located.
*/
public static URL findLibraryURL(String libName) {
- return findLibraryURL(null,libName);
+ return findLibraryURL(null,libName,null,null);
}
/** Replies the URL for the specified library.
@@ -132,6 +132,10 @@
* @return the URL where the specified library was located.
*/
public static URL findLibraryURL(String path, String libName) {
+ return findLibraryURL(path, libName, null, null);
+ }
+
+ private static URL findLibraryURL(String path, String libName, String platform, String arch) {
ClassLoader cl = ClassLoaderFinder.findClassLoader();
if (cl==null) cl = LibraryLoader.class.getClassLoader();
String resourcePath = path;
@@ -140,8 +144,24 @@
resourcePath += "/"; //$NON-NLS-1$
}
// Find the 64bits version of the DLL
- String realLibName = System.mapLibraryName(libName);
- return cl.getResource(resourcePath+realLibName);
+ String realLibName;
+ if (platform!=null) {
+ StringBuffer buf = new StringBuffer(libName);
+ buf.append("-"); //$NON-NLS-1$
+ buf.append(platform);
+ if (arch!=null) buf.append(arch);
+ realLibName = System.mapLibraryName(buf.toString());
+ int idx = realLibName.indexOf(libName);
+ if (idx>0) realLibName = realLibName.substring(idx);
+ }
+ else {
+ StringBuffer buf = new StringBuffer(libName);
+ if (arch!=null) buf.append(arch);
+ realLibName = System.mapLibraryName(buf.toString());
+ }
+ URL libRes = cl.getResource(resourcePath+realLibName);
+ if (libRes!=null) return libRes;
+ return cl.getResource(realLibName);
}
/**
@@ -265,27 +285,72 @@
* @see java.lang.System#load(java.lang.String)
*/
public static void loadPlatformDependentLibrary(String path, String libname) throws IOException {
+ loadPlatformDependentLibrary(libname, null, path);
+ }
+
+ private static URL getPlatformDependentLibrary(String[] paths, String libname, String platform) {
URL url;
int dataModel = getOperatingSystemArchitectureDataModel();
- // Load the 64 library
- if (dataModel==64) {
- url = findLibraryURL(path, libname+"64"); //$NON-NLS-1$
- if (url!=null) {
- try {
- load(url);
- // library loaded
- return;
- }
- catch(Throwable e) {
- System.err.println("could not load "+url); //$NON-NLS-1$
- e.printStackTrace();
- }
- }
+ for(String path : paths) {
+ // Load the 64 library
+ if (dataModel==64) {
+ url = findLibraryURL(path, libname, platform, "64"); //$NON-NLS-1$
+ if (url!=null) return url;
+ }
+ // Load the 32 library
+ else if (dataModel==32) {
+ url = findLibraryURL(path, libname, platform, "32"); //$NON-NLS-1$
+ if (url!=null) return url;
+ }
+ // Load the multi-platform library
+ url = findLibraryURL(path, libname, platform, null);
+ if (url!=null) return url;
}
- // Load the 32 library
- else if (dataModel==32) {
- url = findLibraryURL(path, libname+"32"); //$NON-NLS-1$
- if (url!=null) {
+ return null;
+ }
+
+ /**
+ * Search and load the dynamic library which is fitting the
+ * current operating system (32 or 64bits operating system...).
+ * A 64 bits library is assumed to be named <code>libname64.dll</code>
+ * on Windows® and <code>liblibname64.so</code> on Unix.
+ * A 32 bits library is assumed to be named <code>libname32.dll</code>
+ * on Windows® and <code>liblibname32.so</code> on Unix.
+ * A library which could be ran either on 32 and 64 platforms is assumed
+ * to be named <code>libname.dll</code> on Windows® and
+ * <code>liblibname.so</code> on Unix.
+ *
+ * @param libname is the name of the library.
+ * @param platform is the name of the current OS platform.
+ * @param paths are the resource's paths where the library was located.
+ * @throws IOException
+ * @throws SecurityException if a security manager exists and its
+ * <code>checkLink</code> method doesn't allow
+ * loading of the specified dynamic library
+ * @throws UnsatisfiedLinkError if the file does not exist.
+ * @throws NullPointerException if <code>filename</code> is
+ * <code>null</code>
+ * @see java.lang.System#load(java.lang.String)
+ */
+ static void loadPlatformDependentLibrary(String libname, String platform, String... paths) throws IOException {
+ URL url;
+ // Package version (according to Maven module)
+ url = getPlatformDependentLibrary(paths, libname, null);
+ if (url!=null) {
+ try {
+ load(url);
+ // library loaded
+ return;
+ }
+ catch(Throwable e) {
+ System.err.println("could not load "+url); //$NON-NLS-1$
+ e.printStackTrace();
+ }
+ }
+ // Eclipse version (according to Maven module)
+ if (platform!=null) {
+ url = getPlatformDependentLibrary(paths, libname, platform);
+ if (url!=null) {
try {
load(url);
// library loaded
@@ -295,21 +360,9 @@
System.err.println("could not load "+url); //$NON-NLS-1$
e.printStackTrace();
}
- }
+ }
}
- // Load the multi-platform library
- url = findLibraryURL(path, libname);
- if (url!=null) {
- try {
- load(url);
- // library loaded
- return;
- }
- catch(Throwable e) {
- System.err.println("could not load "+url); //$NON-NLS-1$
- e.printStackTrace();
- }
- }
+
// System-based loading
loadLibrary(libname);
}
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystem.java 2009-12-01 08:07:01 UTC (rev 98)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystem.java 2009-12-23 09:32:10 UTC (rev 99)
@@ -229,7 +229,10 @@
static {
try {
- LibraryLoader.loadPlatformDependentLibrary("org/arakhne/vmutil","josuuid"); //$NON-NLS-1$ //$NON-NLS-2$
+ LibraryLoader.loadPlatformDependentLibrary(
+ "josuuid", //$NON-NLS-1$
+ System.getProperty("os.name").trim().toLowerCase(), //$NON-NLS-1$
+ "org/arakhne/vmutil"); //$NON-NLS-1$
}
catch (IOException e) {
throw new IOError(e);