[Arakhnę-Dev] [23] Support the loaded of 64 and 32 bits libraries according to the data model used by the current operating system .

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 23
Author:   galland
Date:     2008-12-10 14:31:17 +0100 (Wed, 10 Dec 2008)

Log Message:
-----------
Support the loaded of 64 and 32 bits libraries according to the data model used by the current operating system.

Modified Paths:
--------------
    trunk/arakhneVmutils/bin.xml
    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/bin.xml
===================================================================
--- trunk/arakhneVmutils/bin.xml	2008-12-10 13:30:47 UTC (rev 22)
+++ trunk/arakhneVmutils/bin.xml	2008-12-10 13:31:17 UTC (rev 23)
@@ -19,12 +19,12 @@
     <file>
       <source>native/josuuid/linux/target/josuuid-linux.so</source>
       <outputDirectory>org/arakhne/vmutil</outputDirectory>
-      <destName>libjosuuid.so</destName>
+      <destName>libjosuuid32.so</destName>
     </file>
     <file>
       <source>native/josuuid/mingw/target/josuuid-mingw.dll</source>
       <outputDirectory>org/arakhne/vmutil</outputDirectory>
-      <destName>josuuid.dll</destName>
+      <destName>josuuid32.dll</destName>
     </file>
   </files>
 </assembly>

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java	2008-12-10 13:30:47 UTC (rev 22)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/LibraryLoader.java	2008-12-10 13:31:17 UTC (rev 23)
@@ -132,13 +132,14 @@
      * @return the URL where the specified library was located. 
      */
     public static URL findLibraryURL(String path, String libName) {
-        String realLibName = System.mapLibraryName(libName);
         ClassLoader cl = ClassLoaderFinder.findClassLoader();
         if (cl==null) cl = LibraryLoader.class.getClassLoader();
         if (path==null) path = ""; //$NON-NLS-1$
         else if ((path.length()>0)&&(!path.endsWith("/"))) { //$NON-NLS-1$
         	path += "/"; //$NON-NLS-1$
         }
+        // Find the 64bits version of the DLL
+        String realLibName = System.mapLibraryName(libName);
         return cl.getResource(path+realLibName);
     }
 
@@ -197,4 +198,119 @@
         }
     }
     
+    /**
+     * 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&reg; and <code>liblibname64.so</code> on Unix.
+     * A 32 bits library is assumed to be named <code>libname32.dll</code>
+     * on Windows&reg; 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&reg; and
+     * <code>liblibname.so</code> on Unix.
+     *
+     * @param libname is the name of the library.
+     * @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)
+     */
+    public static void loadPlatformDependentLibrary(String libname) throws IOException {
+    	loadPlatformDependentLibrary(null,libname);
+    }
+
+	/** Replies the data model of the current operating system: 32 or 64 bits.
+	 * 
+	 * @return the integer which is corresponding to the data model, or <code>0</code> if
+	 * it could not be determined.
+	 */
+	static int getOperatingSystemArchitectureDataModel() {
+		String arch = System.getProperty("sun.arch.data.model"); //$NON-NLS-1$
+		if (arch!=null) {
+			try {
+				return Integer.parseInt(arch); 
+			}
+			catch(Throwable _) {
+				//
+			}
+		}
+		return 0;
+	}
+
+    /**
+     * 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&reg; and <code>liblibname64.so</code> on Unix.
+     * A 32 bits library is assumed to be named <code>libname32.dll</code>
+     * on Windows&reg; 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&reg; and
+     * <code>liblibname.so</code> on Unix.
+     *
+     * @param path is the resource's path where the library was located.
+     * @param libname is the name of the library.
+     * @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)
+     */
+    public static void loadPlatformDependentLibrary(String path, String libname) throws IOException {
+    	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();
+    			}
+    		}
+    	}
+    	// Load the 32 library
+    	else if (dataModel==32) {
+    		url = findLibraryURL(path, libname+"32"); //$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();
+    			}
+    		}
+    	}
+    	// 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	2008-12-10 13:30:47 UTC (rev 22)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystem.java	2008-12-10 13:31:17 UTC (rev 23)
@@ -21,7 +21,8 @@
 
 package org.arakhne.vmutil;
 
-import java.net.URL;
+import java.io.IOError;
+import java.io.IOException;
 
 /**
  * This is a list of supported operating system.  
@@ -176,7 +177,35 @@
 	          return OTHER;
 	      }
 	}
+	
+	/** Replies the data model of the current operating system: 32 or 64 bits.
+	 * 
+	 * @return the integer which is corresponding to the data model, or <code>0</code> if
+	 * it could not be determined.
+	 */
+	public static int getOperatingSystemArchitectureDataModel() {
+		return LibraryLoader.getOperatingSystemArchitectureDataModel();
+	}
 
+	/** Replies if the current operating system is 64bit.
+	 * 
+	 * @return <code>true</code> if the operating system is 64bits, othewise
+	 * <code>false</code>
+	 */
+	public static boolean is64BitOperatingSystem() {
+		return getOperatingSystemArchitectureDataModel()==64;
+	}
+
+	/** Replies if the current operating system is 32bit.
+	 * 
+	 * @return <code>true</code> if the operating system is 32bits, othewise
+	 * <code>false</code>
+	 */
+	public static boolean is32BitOperatingSystem() {
+		int dataModel = getOperatingSystemArchitectureDataModel();
+		return dataModel==32 || dataModel==0;
+	}
+
 	/** Get the OS serial number.
 	 * 
 	 * @return the serial number associated to the current operating system.
@@ -190,20 +219,12 @@
 	public static native String getOSUUID();
 	
 	static {
-    	String libName = "josuuid"; //$NON-NLS-1$
-        String realLibName = System.mapLibraryName(libName);
-    	try {
-    		ClassLoader clsLoader = ClassLoaderFinder.findClassLoader();
-    		URL libURL = clsLoader.getResource("org/arakhne/vmutil/"+realLibName); //$NON-NLS-1$
-    		//For debuging purpose only:
-    		//URL libURL = new File("/home/stephane/workspace/jlauncherlib/Release/"+realLibName).toURL(); //$NON-NLS-1$
-    		if (libURL!=null) {
-    			LibraryLoader.load(libURL);
-    		}
-    	} 
-    	catch (Throwable e) {
-    		//
-    	}
+		try {
+			LibraryLoader.loadPlatformDependentLibrary("org/arakhne/vmutil","josuuid"); //$NON-NLS-1$ //$NON-NLS-2$
+		}
+		catch (IOException e) {
+			throw new IOError(e);
+		}
 	}
 
 }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/