You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by ch...@apache.org on 2013/02/21 17:11:51 UTC

svn commit: r1448702 - /uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/utils/Utils.java

Author: challngr
Date: Thu Feb 21 16:11:51 2013
New Revision: 1448702

URL: http://svn.apache.org/r1448702
Log:
UIMA-2686
Derive DUCC_HOME from search order:
1. environment
2. system property
3. location of jar

Fail if can't otherwise be found.

Modified:
    uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/utils/Utils.java

Modified: uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/utils/Utils.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/utils/Utils.java?rev=1448702&r1=1448701&r2=1448702&view=diff
==============================================================================
--- uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/utils/Utils.java (original)
+++ uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/utils/Utils.java Thu Feb 21 16:11:51 2013
@@ -29,6 +29,7 @@ import java.lang.management.ManagementFa
 import java.lang.reflect.Field;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
+import java.net.URL;
 import java.rmi.server.UID;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -348,6 +349,51 @@ public class Utils {
 		}
 		return retVal;
 	}
+
+    public static String findDuccHome()
+    {
+        String ducc_home = System.getenv("DUCC_HOME");
+        if ( ducc_home != null ) {                  // The environment trumps all
+            return ducc_home;
+        }
+
+        ducc_home = System.getProperty("DUCC_HOME");
+        if ( ducc_home != null ) {                  // System properties next
+            return ducc_home;
+        }
+
+        // If nothing, we infer ducc home from where "this" Utils class lives.
+
+        Class<Utils> cl = Utils.class;
+        String n = "/" + cl.getName().replaceAll("\\.", "/")+".class"; // to for /org/apache/uima ... Utils.class
+
+
+        // URL will be of form jar:file:/home/whatever/ducc_runtime/lib/uima-ducc-common.jar!/org/apache/uima/ducc/common/utils/Utils.class
+        URL res = cl.getResource(n);
+        if ( res == null ) {
+        	throw new IllegalArgumentException("Cannot find or infer DUCC_HOME.");
+        }
+        
+        String p = res.getFile();
+        String[] parts = p.split("/");
+
+        // The parent must be "lib", and "I" must be a jar, if this is to be valid
+        int last = parts.length - 1;
+        if ( !parts[last].endsWith(".jar") ) {
+        	throw new IllegalArgumentException("Cannot find or infer DUCC_HOME, Utils is not in a jar.");
+        }
+
+        if ( !parts[last-1].equals(".lib") ) {
+        	throw new IllegalArgumentException("Cannot find or infer DUCC_HOME, Utils is not found in a 'lib' directory.");
+        }
+        int ndx = p.lastIndexOf("/");
+        ndx = p.lastIndexOf("/", ndx);
+        System.out.println("res " + res);
+        System.out.println("p " + p);
+
+        return p.substring(0, ndx);
+    }
+
 	public static void main(String[] args) {
 		try {
 			if ( Utils.isThisNode("192.168.3.3", "192.168.3.3") ) {