Author: nextgens
Date: 2008-02-26 08:09:13 +0000 (Tue, 26 Feb 2008)
New Revision: 18160
Added:
trunk/freenet/src/freenet/support/LibraryLoader.java
Modified:
trunk/freenet/src/freenet/support/io/NativeThread.java
Log:
Create a LibraryLoader to factor some code out
Added: trunk/freenet/src/freenet/support/LibraryLoader.java
===================================================================
--- trunk/freenet/src/freenet/support/LibraryLoader.java
(rev 0)
+++ trunk/freenet/src/freenet/support/LibraryLoader.java 2008-02-26
08:09:13 UTC (rev 18160)
@@ -0,0 +1,67 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+
+package freenet.support;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * A simple class to load native libraries from the -ext jarfile
+ *
+ * @author Florent Daignière <nextgens at freenetproject.org>
+ */
+public class LibraryLoader {
+
+ public static String getSimplifiedArchitecture() {
+ String arch;
+
if(System.getProperty("os.arch").toLowerCase().matches("(i?[x0-9]86_64|amd64)"))
{
+ arch = "amd64";
+ } else
if(System.getProperty("os.arch").toLowerCase().matches("(ppc)")) {
+ arch = "ppc";
+ } else {
+ arch = "i386";
+ }
+
+ return arch;
+ }
+
+ public static void loadNative(String path, String libraryName) {
+ final boolean isWindows = File.pathSeparatorChar == '\\';
+ final String libraryNameWithPrefix = (isWindows ? "" : "lib") +
libraryName;
+ final String libraryNameWithPrefixAndArch =
libraryNameWithPrefix + '-' + getSimplifiedArchitecture();
+ final String libraryNameWithPrefixAndArchAndSuffix =
libraryNameWithPrefixAndArch + (isWindows ? ".dll" : ".so");
+ String resourceName = path +
libraryNameWithPrefixAndArchAndSuffix;
+
+ try {
+ // Get the resource
+ URL resource =
LibraryLoader.class.getResource(resourceName);
+
+ // Get input stream from jar resource
+ InputStream inputStream = resource.openStream();
+
+ // Copy resource to filesystem in a temp folder with a
unique name
+ File temporaryLib =
File.createTempFile(libraryNameWithPrefixAndArch, ".tmp");
+
+ // Delete on exit the dll
+ temporaryLib.deleteOnExit();
+
+ FileOutputStream outputStream = new
FileOutputStream(temporaryLib);
+ byte[] array = new byte[2048];
+ int read = 0;
+ while((read = inputStream.read(array)) > 0) {
+ outputStream.write(array, 0, read);
+ }
+ outputStream.close();
+
+ // Finally, load the dll
+ System.out.println("Attempting to load the
"+libraryName+" library ["+resource+']');
+ System.load(temporaryLib.getPath());
+ } catch(Throwable e) {
+ e.printStackTrace();
+ }
+ }
+}
Modified: trunk/freenet/src/freenet/support/io/NativeThread.java
===================================================================
--- trunk/freenet/src/freenet/support/io/NativeThread.java 2008-02-26
07:21:27 UTC (rev 18159)
+++ trunk/freenet/src/freenet/support/io/NativeThread.java 2008-02-26
08:09:13 UTC (rev 18160)
@@ -5,13 +5,9 @@
package freenet.support.io;
import freenet.node.NodeStarter;
+import freenet.support.LibraryLoader;
import freenet.support.Logger;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.net.URL;
-
/**
* Do *NOT* forget to call super.run() if you extend it!
*
@@ -44,7 +40,7 @@
_loadNative =
"Linux".equalsIgnoreCase(System.getProperty("os.name")) &&
NodeStarter.extBuildNumber > 18;
if(_loadNative) {
//System.loadLibrary("NativeThread");
- loadNative();
+ LibraryLoader.loadNative("/freenet/support/io/",
"NativeThread");
NATIVE_PRIORITY_BASE = getLinuxPriority();
NATIVE_PRIORITY_RANGE = 19 - NATIVE_PRIORITY_BASE;
System.out.println("Using the NativeThread
implementation (base nice level is "+NATIVE_PRIORITY_BASE+')');
@@ -65,48 +61,6 @@
Logger.minor(NativeThread.class, "Run init(): _loadNative =
"+_loadNative);
}
- private static void loadNative() {
- // System.loadLibrary("NativeThread");
- String arch;
-
if(System.getProperty("os.arch").toLowerCase().matches("(i?[x0-9]86_64|amd64)"))
{
- arch = "amd64";
- } else
if(System.getProperty("os.arch").toLowerCase().matches("(ppc)")) {
- arch = "ppc";
- } else {
- arch = "i386";
- }
-
- String resourceName = "/freenet/support/io/libNativeThread-" +
arch + ".so";
- try {
- System.out.println("ok");
- // Get the resource
- URL resource =
NativeThread.class.getResource(resourceName);
-
- // Get input stream from jar resource
- InputStream inputStream = resource.openStream();
-
- // Copy resource to filesystem in a temp folder with a
unique name
- File temporaryLib =
File.createTempFile("libNativeThread", ".so");
-
- // Delete on exit the dll
- temporaryLib.deleteOnExit();
-
- FileOutputStream outputStream = new
FileOutputStream(temporaryLib);
- byte[] array = new byte[2048];
- int read = 0;
- while((read = inputStream.read(array)) > 0) {
- outputStream.write(array, 0, read);
- }
- outputStream.close();
-
- // Finally, load the dll
- System.out.println("Attempting to load the NativeThread
library ["+resource+']');
- System.load(temporaryLib.getPath());
- } catch(Throwable e) {
- e.printStackTrace();
- }
- }
-
public NativeThread(String name, int priority, boolean dontCheckRenice)
{
super(name);
this.currentPriority = priority;