From d5987c51c9d4ccbd689d009a8cf9fca5944992b7 Mon Sep 17 00:00:00 2001
From: jrandom <jrandom>
Date: Fri, 25 Jun 2004 18:32:17 +0000
Subject: [PATCH] yet another deployment option - the user can define a
 jbigi.ref environmental variable to specify a file from which the name of the
 resource to be loaded should be found (default is "jbigi.cfg") if that file
 exists, the NativeBigInteger will act as if jbigi.impl was set to the
 contents of that file. For instance, a jbigi.cfg containing "win-p4" would
 have the NativeBigInteger search the classpath for the "win-p4" file and use
 it as a native library. The jbigi.ref preempts the jbigi.impl property (only
 if the file exists and is not empty), but the external platform specific
 jbigi preempts this (e.g. jbigi.dll or libjbigi.so), as does the jbigi.enable
 flag. This option lets us have the admin console write to a file to choose
 which jbigi to use, rather than have to parse some shell script, etc

---
 .../src/net/i2p/util/NativeBigInteger.java    | 38 ++++++++++++++++++-
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/core/java/src/net/i2p/util/NativeBigInteger.java b/core/java/src/net/i2p/util/NativeBigInteger.java
index 14d2a5db8e..c8b538929f 100644
--- a/core/java/src/net/i2p/util/NativeBigInteger.java
+++ b/core/java/src/net/i2p/util/NativeBigInteger.java
@@ -13,7 +13,10 @@ import java.util.Random;
 import java.security.SecureRandom;
 
 import java.net.URL;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
 import java.io.FileOutputStream;
+import java.io.FileInputStream;
 import java.io.InputStream;
 import java.io.IOException;
 import java.io.File;
@@ -25,9 +28,11 @@ import java.io.File;
  * GMP library - a collection of insanely efficient routines for dealing with 
  * big numbers.</p>
  *
- * There are two environmental properties for configuring this component: <ul>
+ * There are three environmental properties for configuring this component: <ul>
  * <li><b>jbigi.enable</b>: whether to use the native library (defaults to "true")</li>
  * <li><b>jbigi.impl</b>: select which resource to use as the native implementation</li>
+ * <li><b>jbigi.ref</b>: the file specified in this parameter may contain a resource
+ *                       name to override jbigi.impl (defaults to "jbigi.cfg")</li>
  * </ul>
  *
  * <p>If jbigi.enable is set to false, this class won't even attempt to use the 
@@ -91,6 +96,8 @@ public class NativeBigInteger extends BigInteger {
      */
     private static final boolean _doLog = true;
     
+    private static String DEFAULT_REF_FILENAME = "jbigi.cfg";
+    
     static {
         loadNative();
     }
@@ -281,7 +288,7 @@ public class NativeBigInteger extends BigInteger {
      *
      */
     private static final boolean loadFromResource() {
-        String resourceName = System.getProperty("jbigi.impl");
+        String resourceName = getResourceName();
         if (resourceName == null) return false;
         URL resource = NativeBigInteger.class.getClassLoader().getResource(resourceName);
         if (resource == null) {
@@ -323,4 +330,31 @@ public class NativeBigInteger extends BigInteger {
             }
         }
     }
+    
+    private static final String getResourceName() {
+        String jbigiRefFile = System.getProperty("jbigi.ref");
+        if (jbigiRefFile == null)
+            jbigiRefFile = DEFAULT_REF_FILENAME;
+        
+        File refFile = new File(jbigiRefFile);
+        if (refFile.exists()) {
+            BufferedReader in = null;
+            try {
+                in = new BufferedReader(new InputStreamReader(new FileInputStream(refFile)));
+                String resourceName = in.readLine();
+                if ( (resourceName != null) || (resourceName.trim().length() <= 0) )
+                    return resourceName;
+            } catch (IOException ioe) {
+                if (_doLog) {
+                    System.err.println("WARN: Unable to read the jbigi reference file " + jbigiRefFile);
+                    ioe.printStackTrace();
+                }
+            } finally {
+                if (in != null) try { in.close(); } catch (IOException ioe) {}
+            }
+        }
+        
+        // the file doesnt exist, or it is empty, so use the jbigi.impl system env
+        return System.getProperty("jbigi.impl");
+    }
 }
\ No newline at end of file
-- 
GitLab