JAL-3820 JAL-3830 improvements to script launching. Icon on macOS. Fixed CLASSPATH.
[jalview.git] / getdown / src / getdown / core / src / main / java / jalview / util / LaunchUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.util;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.MalformedURLException;
29 import java.net.URL;
30 import java.util.Properties;
31
32 public class LaunchUtils
33 {
34
35   public static void loadChannelProps(File dir)
36   {
37     ChannelProperties.loadProps(dir);
38   }
39
40   private static Properties userPreferences = null;
41
42   public static String getUserPreference(String key)
43   {
44     if (userPreferences == null)
45     {
46       String channelPrefsFilename = ChannelProperties
47               .getProperty("preferences.filename");
48       if (channelPrefsFilename == null)
49       {
50         return null;
51       }
52       File propertiesFile = new File(System.getProperty("user.home"),
53               channelPrefsFilename);
54       if (!propertiesFile.exists())
55       {
56         return null;
57       }
58       try
59       {
60         userPreferences = new Properties();
61         userPreferences.load(new FileInputStream(propertiesFile));
62       } catch (FileNotFoundException e)
63       {
64         // didn't find user preferences file
65         return null;
66       } catch (IOException e)
67       {
68         System.err.println(e.getMessage());
69         return null;
70       }
71     }
72     return userPreferences.getProperty(key);
73   }
74
75   public static boolean getBooleanUserPreference(String key)
76   {
77     return Boolean.parseBoolean(getUserPreference(key));
78   }
79
80   public static int JAVA_COMPILE_VERSION = 0;
81
82   public static int getJavaCompileVersion()
83   {
84     if (JAVA_COMPILE_VERSION > 0)
85     {
86       return JAVA_COMPILE_VERSION;
87     }
88     String buildDetails = "jar:".concat(LaunchUtils.class
89             .getProtectionDomain().getCodeSource().getLocation().toString()
90             .concat("!" + "/.build_properties"));
91     try
92     {
93       URL localFileURL = new URL(buildDetails);
94       InputStream in = localFileURL.openStream();
95       Properties buildProperties = new Properties();
96       buildProperties.load(in);
97       in.close();
98       String JCV = buildProperties.getProperty("JAVA_COMPILE_VERSION",
99               null);
100       if (JCV == null)
101       {
102         System.out.println(
103                 "Could not obtain JAVA_COMPILE_VERSION for comparison");
104         return -2;
105       }
106       JAVA_COMPILE_VERSION = Integer.parseInt(JCV);
107     } catch (MalformedURLException e)
108     {
109       System.err.println("Could not find " + buildDetails);
110       return -3;
111     } catch (IOException e)
112     {
113       System.err.println("Could not load " + buildDetails);
114       return -4;
115     } catch (NumberFormatException e)
116     {
117       System.err.println("Could not parse JAVA_COMPILE_VERSION");
118       return -5;
119     }
120
121     return JAVA_COMPILE_VERSION;
122   }
123
124   public static int JAVA_VERSION = 0;
125
126   public static int getJavaVersion()
127   {
128     if (JAVA_VERSION > 0)
129     {
130       return JAVA_VERSION;
131     }
132     try
133     {
134       String JV = System.getProperty("java.version");
135       if (JV == null)
136       {
137         System.out.println("Could not obtain java.version for comparison");
138         return -2;
139       }
140       if (JV.startsWith("1."))
141       {
142         JV = JV.substring(2);
143       }
144       JAVA_VERSION = JV.indexOf(".") == -1 ? Integer.parseInt(JV)
145               : Integer.parseInt(JV.substring(0, JV.indexOf(".")));
146     } catch (NumberFormatException e)
147     {
148       System.err.println("Could not parse java.version");
149       return -3;
150     }
151     return JAVA_VERSION;
152   }
153
154   public static boolean checkJavaVersion()
155   {
156     String buildDetails = "jar:".concat(LaunchUtils.class
157             .getProtectionDomain().getCodeSource().getLocation().toString()
158             .concat("!" + "/.build_properties"));
159
160     int java_compile_version = getJavaCompileVersion();
161     int java_version = getJavaVersion();
162
163     if (java_compile_version <= 0 || java_version <= 0)
164     {
165       System.out.println("Could not make Java version check");
166       return true;
167     }
168     // Warn if these java.version and JAVA_COMPILE_VERSION conditions exist
169     // Usually this means a Java 11 compiled JAR being run by a Java 11 JVM
170     if (java_version >= 11 && java_compile_version < 11)
171     {
172       return false;
173     }
174
175     return true;
176   }
177 }