5bd4a08491e0c9bba73e30223898a0455d37eb64
[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 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 (Platform.isJS())
85     {
86       return -1;
87     }
88     else if (JAVA_COMPILE_VERSION > 0)
89     {
90       return JAVA_COMPILE_VERSION;
91     }
92     String buildDetails = "jar:".concat(LaunchUtils.class
93             .getProtectionDomain().getCodeSource().getLocation().toString()
94             .concat("!" + "/.build_properties"));
95     try
96     {
97       URL localFileURL = new URL(buildDetails);
98       InputStream in = localFileURL.openStream();
99       Properties buildProperties = new Properties();
100       buildProperties.load(in);
101       in.close();
102       String JCV = buildProperties.getProperty("JAVA_COMPILE_VERSION",
103               null);
104       if (JCV == null)
105       {
106         System.out.println(
107                 "Could not obtain JAVA_COMPILE_VERSION for comparison");
108         return -2;
109       }
110       JAVA_COMPILE_VERSION = Integer.parseInt(JCV);
111     } catch (MalformedURLException e)
112     {
113       System.err.println("Could not find " + buildDetails);
114       return -3;
115     } catch (IOException e)
116     {
117       System.err.println("Could not load " + buildDetails);
118       return -4;
119     } catch (NumberFormatException e)
120     {
121       System.err.println("Could not parse JAVA_COMPILE_VERSION");
122       return -5;
123     }
124
125     return JAVA_COMPILE_VERSION;
126   }
127
128   public static int JAVA_VERSION = 0;
129
130   public static int getJavaVersion()
131   {
132     if (Platform.isJS())
133     {
134       return -1;
135     }
136     else if (JAVA_VERSION > 0)
137     {
138       return JAVA_VERSION;
139     }
140     try
141     {
142       String JV = System.getProperty("java.version");
143       if (JV == null)
144       {
145         System.out.println("Could not obtain java.version for comparison");
146         return -2;
147       }
148       if (JV.startsWith("1."))
149       {
150         JV = JV.substring(2);
151       }
152       JAVA_VERSION = JV.indexOf(".") == -1 ? Integer.parseInt(JV)
153               : Integer.parseInt(JV.substring(0, JV.indexOf(".")));
154     } catch (NumberFormatException e)
155     {
156       System.err.println("Could not parse java.version");
157       return -3;
158     }
159     return JAVA_VERSION;
160   }
161
162   public static boolean checkJavaVersion()
163   {
164     if (Platform.isJS())
165     {
166       return true;
167     }
168     String buildDetails = "jar:".concat(LaunchUtils.class
169             .getProtectionDomain().getCodeSource().getLocation().toString()
170             .concat("!" + "/.build_properties"));
171
172     int java_compile_version = getJavaCompileVersion();
173     int java_version = getJavaVersion();
174
175     if (java_compile_version <= 0 || java_version <= 0)
176     {
177       System.out.println("Could not make Java version check");
178       return true;
179     }
180     // Warn if these java.version and JAVA_COMPILE_VERSION conditions exist
181     // Usually this means a Java 11 compiled JAR being run by a Java 11 JVM
182     if (java_version >= 11 && java_compile_version < 11)
183     {
184       return false;
185     }
186
187     return true;
188   }
189 }