01: package tide.editor;
02:
03: import java.util.Arrays;
04: import java.util.regex.Matcher;
05: import snow.utils.ProcessUtils;
06: import snow.utils.SysUtils;
07: import java.util.regex.Pattern;
08: import java.io.File;
09:
10: public final class TideUtils {
11: private TideUtils() {
12: }
13:
14: /** @return null if webstart mode, i.e. if no tide.jar is found on the classpath.
15: * TODO: also allow tide_VVV.jar
16: */
17: public static File getTideJarCurrentlyRunning() {
18: String cps = System.getProperty("java.class.path", "");
19: //System.out.println(""+cps);
20: String[] cp = cps.split(Pattern.quote(System
21: .getProperty("path.separator")));
22: for (String cpi : cp) {
23: cpi = cpi.trim();
24:
25: if (!cpi.toLowerCase().endsWith(".jar"))
26: continue;
27:
28: if (cpi.toLowerCase().contains("tide.jar")
29: || cpi.toLowerCase().contains("tide_")) {
30: if (cpi.equalsIgnoreCase("tide.jar")
31: || cpi.toLowerCase().startsWith("tide_")) {
32: // special case !
33: File fi = new File(System.getProperty("user.dir",
34: ""), cpi);
35: if (fi.exists())
36: return fi;
37: }
38: File fi = new File(cpi);
39: if (fi.exists())
40: return fi;
41: }
42: }
43: // => assume is WebStart mode
44: return null;
45: }
46:
47: /** Simplified try... just looks if a tide.jar is found on classpath. If no, assume we're in WebStartMode.
48: */
49: public static boolean isWebStartMode() {
50: try {
51: //a more robust lookup:
52: // BasicService bs = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService");
53: // if(bs!=null) return true;
54:
55: File tf = TideUtils.getTideJarCurrentlyRunning();
56: if (tf == null)
57: return true;
58: } catch (Exception e) {
59: }
60:
61: return false;
62: }
63:
64: /** You have to compare with "1.5"
65: */
66: public static String readJavaVersion(final File javaExe) {
67: if (javaExe == null || !javaExe.exists())
68: return "0 (error: no java exe found)";
69: try {
70: String vs = ProcessUtils.readWholeProcessStack(Arrays
71: .asList(javaExe.getAbsolutePath(), "-version"));
72: Pattern pat = Pattern.compile("\\\"(.+)\\\"");
73: Matcher m = pat.matcher(vs);
74: if (m.find()) {
75: return m.group(1);
76: } else {
77: System.out.println("no java version found in\n" + vs);
78: return "1.6 (error: no version read)";
79: }
80: } catch (Exception e) {
81: e.printStackTrace();
82: return "0 (error: " + e.getMessage() + ")";
83: }
84:
85: }
86:
87: public static File getJavaToolUsedToLaunchTide() {
88: return new File(System.getProperty("java.home"), SysUtils
89: .is_Windows_OS() ? "bin/java.exe" : "bin/java");
90: }
91: }
|