/** Util.java * * Utility routines related to directories for G4blGui and G4blData. **/ import java.io.*; import java.net.URL; import java.util.*; public class Util { static String osName = null; static String g4bl_dir = null; static String geant4_data = null; /// findG4blDir() returns the absolute path of the G4beamline install directory. /// Returns null if it cannot be determined. public static String findG4blDir() { if(g4bl_dir != null) return g4bl_dir; osName = System.getProperty("os.name"); String filesep = System.getProperty("file.separator"); String pathsep = System.getProperty("path.separator"); g4bl_dir = System.getenv("G4BL_DIR"); if(g4bl_dir == null || g4bl_dir == "") { g4bl_dir = System.getProperty("java.class.path"); int i = g4bl_dir.indexOf(pathsep); if(i >= 0) g4bl_dir = g4bl_dir.substring(0,i); File tmp = new File(g4bl_dir); g4bl_dir = tmp.getParentFile().getParentFile().getAbsolutePath(); } if(g4bl_dir.endsWith("/") || g4bl_dir.endsWith("\\")) g4bl_dir = g4bl_dir.substring(0,g4bl_dir.length()-1); // verify g4bl_dir File dir = new File(g4bl_dir); String ext = ""; if(osName.startsWith("Windows")) ext = ".exe"; if(!new File(g4bl_dir+filesep+"bin"+filesep+"g4beamline"+ext).exists()) g4bl_dir = null; System.out.printf("G4BL_DIR=%s\n",g4bl_dir); return g4bl_dir; } /// findGeant4Data() will find the directory and return its absolute path. /// Returns null if not found. public static String findGeant4Data() { if(geant4_data != null) return geant4_data; if(findG4blDir() == null) return null; File d = new File(g4bl_dir); if(!d.isDirectory()) return null; d = new File(d,"Geant4Data"); System.out.printf("Looking for Geant4Data in '%s'",d.toString()); if(d.exists() && d.isDirectory()) { System.out.printf(" found.\n"); geant4_data = d.getAbsolutePath(); return geant4_data; } System.out.printf(" not found.\n"); d = new File(System.getProperty("user.home")); if(!d.isDirectory()) return null; d = new File(d,"Geant4Data"); System.out.printf("Looking for Geant4Data in '%s'",d.toString()); if(d.exists() && d.isDirectory()) { System.out.printf(" found.\n"); geant4_data = d.getAbsolutePath(); return geant4_data; } System.out.printf(" not found.\n"); return null; } /// findOrCreateGeant4Data() will find the directory, creating it if not found. /// Returns its absolute path, or null if it cannot be created. public static String findOrCreateGeant4Data() { String s = findGeant4Data(); if(s != null) return s; File d = new File(g4bl_dir); if(!d.isDirectory()) return null; d = new File(d,"Geant4Data"); try { if(d.mkdir()) { geant4_data = d.getAbsolutePath(); return geant4_data; } } catch(Exception e) { } d = new File(System.getProperty("user.home")); if(!d.isDirectory()) return null; d = new File(d,"Geant4Data"); try { if(d.mkdir()) { geant4_data = d.getAbsolutePath(); return geant4_data; } } catch(Exception e) { } return null; } };