eb43363ad1f7771af5b0044d83b92c9a8299edfe
[jalview.git] / src / 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 import jalview.bin.Console;
33
34 public class LaunchUtils
35 {
36
37   public static void loadChannelProps(File dir)
38   {
39     ChannelProperties.loadProps(dir);
40   }
41
42   private static Properties userPreferences = null;
43
44   public static String getUserPreference(String key)
45   {
46     if (userPreferences == null)
47     {
48       String channelPrefsFilename = ChannelProperties
49               .getProperty("preferences.filename");
50       if (channelPrefsFilename == null)
51       {
52         return null;
53       }
54       File propertiesFile = new File(System.getProperty("user.home"),
55               channelPrefsFilename);
56       if (!propertiesFile.exists())
57       {
58         return null;
59       }
60       try
61       {
62         userPreferences = new Properties();
63         userPreferences.load(new FileInputStream(propertiesFile));
64       } catch (FileNotFoundException e)
65       {
66         // didn't find user preferences file
67         return null;
68       } catch (IOException e)
69       {
70         System.err.println(e.getMessage());
71         return null;
72       }
73     }
74     return userPreferences.getProperty(key);
75   }
76
77   public static boolean getBooleanUserPreference(String key)
78   {
79     return Boolean.parseBoolean(getUserPreference(key));
80   }
81
82   public static int JAVA_COMPILE_VERSION = 0;
83
84   public static int getJavaCompileVersion()
85   {
86     if (Platform.isJS())
87     {
88       return -1;
89     }
90     else if (JAVA_COMPILE_VERSION > 0)
91     {
92       return JAVA_COMPILE_VERSION;
93     }
94     String buildDetails = "jar:".concat(LaunchUtils.class
95             .getProtectionDomain().getCodeSource().getLocation().toString()
96             .concat("!" + "/.build_properties"));
97     try
98     {
99       URL localFileURL = new URL(buildDetails);
100       InputStream in = localFileURL.openStream();
101       Properties buildProperties = new Properties();
102       buildProperties.load(in);
103       in.close();
104       String JCV = buildProperties.getProperty("JAVA_COMPILE_VERSION",
105               null);
106       if (JCV == null)
107       {
108         Console.outputMessage(
109                 "Could not obtain JAVA_COMPILE_VERSION for comparison");
110         return -2;
111       }
112       JAVA_COMPILE_VERSION = Integer.parseInt(JCV);
113     } catch (MalformedURLException e)
114     {
115       System.err.println("Could not find " + buildDetails);
116       return -3;
117     } catch (IOException e)
118     {
119       System.err.println("Could not load " + buildDetails);
120       return -4;
121     } catch (NumberFormatException e)
122     {
123       System.err.println("Could not parse JAVA_COMPILE_VERSION");
124       return -5;
125     }
126
127     return JAVA_COMPILE_VERSION;
128   }
129
130   public static int JAVA_VERSION = 0;
131
132   public static int getJavaVersion()
133   {
134     if (Platform.isJS())
135     {
136       return -1;
137     }
138     else if (JAVA_VERSION > 0)
139     {
140       return JAVA_VERSION;
141     }
142     try
143     {
144       String JV = System.getProperty("java.version");
145       if (JV == null)
146       {
147         Console.outputMessage(
148                 "Could not obtain java.version for comparison");
149         return -2;
150       }
151       if (JV.startsWith("1."))
152       {
153         JV = JV.substring(2);
154       }
155       JAVA_VERSION = JV.indexOf(".") == -1 ? Integer.parseInt(JV)
156               : Integer.parseInt(JV.substring(0, JV.indexOf(".")));
157     } catch (NumberFormatException e)
158     {
159       System.err.println("Could not parse java.version");
160       return -3;
161     }
162     return JAVA_VERSION;
163   }
164
165   public static boolean checkJavaVersion()
166   {
167     if (Platform.isJS())
168     {
169       return true;
170     }
171     String buildDetails = "jar:".concat(LaunchUtils.class
172             .getProtectionDomain().getCodeSource().getLocation().toString()
173             .concat("!" + "/.build_properties"));
174
175     int java_compile_version = getJavaCompileVersion();
176     int java_version = getJavaVersion();
177
178     if (java_compile_version <= 0 || java_version <= 0)
179     {
180       Console.outputMessage("Could not make Java version check");
181       return true;
182     }
183     // Warn if these java.version and JAVA_COMPILE_VERSION conditions exist
184     // Usually this means a Java 11 compiled JAR being run by a Java 11 JVM
185     if (java_version >= 11 && java_compile_version < 11)
186     {
187       return false;
188     }
189
190     return true;
191   }
192 }