Skip to content

Commit da36be0

Browse files
authored
feat: Auto-detect $CLAS12DIR coatjava location (#795)
1 parent 931465b commit da36be0

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

common-tools/clas-utils/src/main/java/org/jlab/utils/system/ClasUtilsFile.java

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.FileWriter;
99
import java.io.IOException;
1010
import java.util.ArrayList;
11+
import java.util.Arrays;
1112
import java.util.List;
1213
import java.util.logging.Level;
1314
import java.util.logging.Logger;
@@ -20,13 +21,48 @@ public class ClasUtilsFile {
2021
private static String moduleString = "[ClasUtilsFile] --> ";
2122

2223
public static String getName(){ return moduleString; }
24+
2325
/**
2426
* prints a log message with the module name included.
2527
* @param log
2628
*/
2729
public static void printLog(String log){
2830
System.out.println(ClasUtilsFile.getName() + " " + log);
2931
}
32+
33+
/**
34+
* @param clazz
35+
* @return absolute path of the jar file containing clazz
36+
*/
37+
public static String getJarPath(Class clazz) {
38+
try {
39+
return (new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI())).getAbsolutePath();
40+
} catch (Exception e) {
41+
System.getLogger(ClasUtilsFile.class.getName()).log(System.Logger.Level.ERROR, (String) null, e);
42+
return null;
43+
}
44+
}
45+
46+
/**
47+
* @return absolute path to COATJAVA installation runtime directory
48+
*/
49+
public static String getCoatjavaRuntimeDir() {
50+
String ret = getJarPath(ClasUtilsFile.class);
51+
if (ret != null) {
52+
String[] d = ret.split("/");
53+
if (System.console() == null)
54+
// When run from an IDE, the jar is in the coatjava source tree,
55+
// so assume the "coatjava" installation directory at the top:
56+
ret = "/" + String.join("/", Arrays.copyOfRange(d,0,d.length-4)) + "/coatjava";
57+
else
58+
// When running the JVM directly, the jar is already inside a
59+
// coatjava installation, so just get to the top of it:
60+
ret = "/" + String.join("/", Arrays.copyOfRange(d,0,d.length-3));
61+
62+
}
63+
return ret;
64+
}
65+
3066
/**
3167
* returns package resource directory with given enviromental variable
3268
* and relative path.
@@ -36,33 +72,37 @@ public static void printLog(String log){
3672
*/
3773
public static String getResourceDir(String env, String rpath){
3874

39-
String envString = System.getenv(env);
40-
if(envString==null){
75+
String value = System.getenv(env);
76+
77+
if(value==null){
4178
ClasUtilsFile.printLog("Environment variable ["+env+"] is not defined");
42-
envString = System.getProperty(env);
79+
value = System.getProperty(env);
4380
}
4481

45-
if(envString == null){
82+
if(value == null){
4683
ClasUtilsFile.printLog("System property ["+env+"] is not defined");
47-
return null;
84+
if (env.equals("COATJAVA") || env.equals("CLAS12DIR")) {
85+
value = getCoatjavaRuntimeDir();
86+
}
4887
}
88+
89+
if (value == null) return null;
4990

5091
StringBuilder str = new StringBuilder();
51-
int index = envString.length()-1;
52-
str.append(envString);
53-
//Char fileSeparator =
54-
if(envString.charAt(index)!='/' && rpath.startsWith("/")==false) str.append('/');
92+
str.append(value);
93+
if (!value.endsWith("/") && !rpath.startsWith("/")) str.append('/');
5594
str.append(rpath);
5695
return str.toString();
5796
}
97+
5898
/**
5999
* returns list of files in the directory. absolute path is given.
60100
* This function will not exclude ".*" and "*~" files.
61101
* @param directory
62102
* @return
63103
*/
64104
public static List<String> getFileList(String directory){
65-
List<String> fileList = new ArrayList<String>();
105+
List<String> fileList = new ArrayList<>();
66106
File[] files = new File(directory).listFiles();
67107
System.out.println("FILE LIST LENGTH = " + files.length);
68108
for (File file : files) {
@@ -88,7 +128,7 @@ public static List<String> getFileList(String env, String rpath){
88128
String directory = ClasUtilsFile.getResourceDir(env, rpath);
89129
if(directory==null){
90130
ClasUtilsFile.printLog("(error) directory does not exist : " + directory);
91-
return new ArrayList<String>();
131+
return new ArrayList<>();
92132
}
93133
return ClasUtilsFile.getFileList(directory);
94134
}
@@ -101,10 +141,10 @@ public static List<String> getFileList(String env, String rpath){
101141
*/
102142
public static List<String> getFileList(String env, String rpath, String ext){
103143
String directory = ClasUtilsFile.getResourceDir(env, rpath);
104-
if(directory!=null) return new ArrayList<String>();
144+
if(directory!=null) return new ArrayList<>();
105145

106146
List<String> files = ClasUtilsFile.getFileList(directory);
107-
List<String> selected = new ArrayList<String>();
147+
List<String> selected = new ArrayList<>();
108148
for(String item : files){
109149
if(item.endsWith(ext)==true) selected.add(item);
110150
}
@@ -135,7 +175,7 @@ public static void writeFile(String filename, List<String> lines){
135175
* @return
136176
*/
137177
public static List<String> readFile(String filename){
138-
List<String> lines = new ArrayList<String>();
178+
List<String> lines = new ArrayList<>();
139179
String line = null;
140180
try {
141181
// FileReader reads text files in the default encoding.
@@ -177,7 +217,7 @@ public static String readFileString(String filename){
177217
* @return
178218
*/
179219
public static List<String> getFileNamesRelative(List<String> files){
180-
List<String> newList = new ArrayList<String>();
220+
List<String> newList = new ArrayList<>();
181221
for(String file : files){
182222
int index = file.lastIndexOf('/');
183223
if(index>=0&&index<file.length()){
@@ -209,15 +249,13 @@ public static String createFileName(String filename, String addition, boolean pr
209249

210250
StringBuilder str = new StringBuilder();
211251
int index = inputFile.lastIndexOf(".");
212-
//int index = filename.lastIndexOf(".");
213252
str.append(inputFile.substring(0, index));
214253
str.append(addition);
215254
str.append(inputFile.substring(index, inputFile.length()));
216255
return str.toString();
217256
}
218257

219258
public static void main(String[] args){
220-
String output_file = ClasUtilsFile.createFileName("/Users/gavalian/Work/Software/Release-9.0/COATJAVA/coatjava/../datasets/gemc/eklambda/gemc_eklambda_A0001_gen.evio", "_header", true);
221-
System.out.println(output_file);
259+
System.out.println(getCoatjavaRuntimeDir());
222260
}
223261
}

0 commit comments

Comments
 (0)