diff --git a/common-tools/clas-utils/src/main/java/org/jlab/utils/system/ClasUtilsFile.java b/common-tools/clas-utils/src/main/java/org/jlab/utils/system/ClasUtilsFile.java index a56d4358be..d47d2eb9c3 100644 --- a/common-tools/clas-utils/src/main/java/org/jlab/utils/system/ClasUtilsFile.java +++ b/common-tools/clas-utils/src/main/java/org/jlab/utils/system/ClasUtilsFile.java @@ -8,6 +8,7 @@ import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -20,6 +21,7 @@ public class ClasUtilsFile { private static String moduleString = "[ClasUtilsFile] --> "; public static String getName(){ return moduleString; } + /** * prints a log message with the module name included. * @param log @@ -27,6 +29,40 @@ public class ClasUtilsFile { public static void printLog(String log){ System.out.println(ClasUtilsFile.getName() + " " + log); } + + /** + * @param clazz + * @return absolute path of the jar file containing clazz + */ + public static String getJarPath(Class clazz) { + try { + return (new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI())).getAbsolutePath(); + } catch (Exception e) { + System.getLogger(ClasUtilsFile.class.getName()).log(System.Logger.Level.ERROR, (String) null, e); + return null; + } + } + + /** + * @return absolute path to COATJAVA installation runtime directory + */ + public static String getCoatjavaRuntimeDir() { + String ret = getJarPath(ClasUtilsFile.class); + if (ret != null) { + String[] d = ret.split("/"); + if (System.console() == null) + // When run from an IDE, the jar is in the coatjava source tree, + // so assume the "coatjava" installation directory at the top: + ret = "/" + String.join("/", Arrays.copyOfRange(d,0,d.length-4)) + "/coatjava"; + else + // When running the JVM directly, the jar is already inside a + // coatjava installation, so just get to the top of it: + ret = "/" + String.join("/", Arrays.copyOfRange(d,0,d.length-3)); + + } + return ret; + } + /** * returns package resource directory with given enviromental variable * and relative path. @@ -36,25 +72,29 @@ public static void printLog(String log){ */ public static String getResourceDir(String env, String rpath){ - String envString = System.getenv(env); - if(envString==null){ + String value = System.getenv(env); + + if(value==null){ ClasUtilsFile.printLog("Environment variable ["+env+"] is not defined"); - envString = System.getProperty(env); + value = System.getProperty(env); } - if(envString == null){ + if(value == null){ ClasUtilsFile.printLog("System property ["+env+"] is not defined"); - return null; + if (env.equals("COATJAVA") || env.equals("CLAS12DIR")) { + value = getCoatjavaRuntimeDir(); + } } + + if (value == null) return null; StringBuilder str = new StringBuilder(); - int index = envString.length()-1; - str.append(envString); - //Char fileSeparator = - if(envString.charAt(index)!='/' && rpath.startsWith("/")==false) str.append('/'); + str.append(value); + if (!value.endsWith("/") && !rpath.startsWith("/")) str.append('/'); str.append(rpath); return str.toString(); } + /** * returns list of files in the directory. absolute path is given. * This function will not exclude ".*" and "*~" files. @@ -62,7 +102,7 @@ public static String getResourceDir(String env, String rpath){ * @return */ public static List getFileList(String directory){ - List fileList = new ArrayList(); + List fileList = new ArrayList<>(); File[] files = new File(directory).listFiles(); System.out.println("FILE LIST LENGTH = " + files.length); for (File file : files) { @@ -88,7 +128,7 @@ public static List getFileList(String env, String rpath){ String directory = ClasUtilsFile.getResourceDir(env, rpath); if(directory==null){ ClasUtilsFile.printLog("(error) directory does not exist : " + directory); - return new ArrayList(); + return new ArrayList<>(); } return ClasUtilsFile.getFileList(directory); } @@ -101,10 +141,10 @@ public static List getFileList(String env, String rpath){ */ public static List getFileList(String env, String rpath, String ext){ String directory = ClasUtilsFile.getResourceDir(env, rpath); - if(directory!=null) return new ArrayList(); + if(directory!=null) return new ArrayList<>(); List files = ClasUtilsFile.getFileList(directory); - List selected = new ArrayList(); + List selected = new ArrayList<>(); for(String item : files){ if(item.endsWith(ext)==true) selected.add(item); } @@ -135,7 +175,7 @@ public static void writeFile(String filename, List lines){ * @return */ public static List readFile(String filename){ - List lines = new ArrayList(); + List lines = new ArrayList<>(); String line = null; try { // FileReader reads text files in the default encoding. @@ -177,7 +217,7 @@ public static String readFileString(String filename){ * @return */ public static List getFileNamesRelative(List files){ - List newList = new ArrayList(); + List newList = new ArrayList<>(); for(String file : files){ int index = file.lastIndexOf('/'); if(index>=0&&index