Discover user colours
[jalview.git] / src / jalview / bin / Cache.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.bin;\r
20 \r
21 import java.io.*;\r
22 \r
23 import java.util.*;\r
24 \r
25 import java.net.*;\r
26 \r
27 \r
28 /**\r
29  * Stores and retrieves Jalview Application Properties\r
30  * <br><br>Current properties include:\r
31  * <br>logs.Axis.Level - one of the stringified Levels for log4j controlling the logging level for axis (used for web services)\r
32  * <br>logs.Castor.Level - one of the stringified Levels for log4j controlling the logging level for castor (used for serialization)\r
33  * <br>SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_Y=285,SCREEN_X=371,SHOW_FULLSCREEN\r
34  * FONT_NAME,FONT_SIZE,FONT_STYLE,GAP_SYMBOL,LAST_DIRECTORY,USER_DEFINED_COLOUR\r
35  * SHOW_FULL_ID,SHOW_IDENTITY,SHOW_QUALITY,SHOW_ANNOTATIONS,SHOW_CONSERVATION,\r
36  * DEFAULT_COLOUR,DEFAULT_FILE_FORMAT,STARTUP_FILE,SHOW_STARTUP_FILE\r
37 \r
38  * @author $author$\r
39  * @version $Revision$\r
40  */\r
41 public class Cache\r
42 {\r
43 \r
44     /** Jalview Properties */\r
45     public static Properties applicationProperties = new Properties();\r
46 \r
47     /** Default file is  ~/.jalview_properties */\r
48     static String propertiesFile;\r
49 \r
50     /** Called when Jalview is started */\r
51     public static void loadProperties(String propsFile)\r
52     {\r
53         propertiesFile = propsFile;\r
54         if (propsFile == null)\r
55         {\r
56           propertiesFile = System.getProperty("user.home") + "/.jalview_properties";\r
57         }\r
58 \r
59         try\r
60         {\r
61             FileInputStream fis = new FileInputStream(propertiesFile);\r
62             applicationProperties.load(fis);\r
63             applicationProperties.remove("LATEST_VERSION");\r
64             applicationProperties.remove("VERSION");\r
65             fis.close();\r
66         }\r
67         catch (Exception ex)\r
68         {\r
69           System.out.println("Error reading properties file: "+ex);\r
70         }\r
71 \r
72       if(getDefault("USE_PROXY", false))\r
73       {\r
74           System.out.println("Using proxyServer: "+getDefault("PROXY_SERVER", null)+\r
75                            " proxyPort: "+getDefault("PROXY_PORT", null));\r
76           System.setProperty("http.proxyHost", getDefault("PROXY_SERVER", null));\r
77           System.setProperty("http.proxyPort", getDefault("PROXY_PORT", null));\r
78       }\r
79 \r
80         // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar\r
81         // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE\r
82         // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW\r
83          try\r
84          {\r
85              String buildDetails = "jar:"\r
86                  .concat(\r
87                  Cache.class.getProtectionDomain().getCodeSource().getLocation().toString()\r
88                  .concat("!/.build_properties")\r
89                  );\r
90 \r
91              java.net.URL localJarFileURL = new java.net.URL(buildDetails);\r
92 \r
93             InputStream in = localJarFileURL.openStream();\r
94             applicationProperties.load(in);\r
95             in.close();\r
96          }\r
97          catch (Exception ex)\r
98          {\r
99            System.out.println("Error reading build details: "+ex);\r
100            applicationProperties.remove("VERSION");\r
101         }\r
102 \r
103         String jnlpVersion = System.getProperty("jalview.version");\r
104         String codeVersion = getProperty("VERSION");\r
105 \r
106 \r
107         if(codeVersion==null)\r
108         {\r
109           // THIS SHOULD ONLY BE THE CASE WHEN TESTING!!\r
110           codeVersion = "Test";\r
111           jnlpVersion = "Test";\r
112         }\r
113 \r
114 \r
115         System.out.println("Jalview Version: "+codeVersion);\r
116 \r
117         // jnlpVersion will be null if we're using InstallAnywhere\r
118         // Dont do this check if running in headless mode\r
119         if(jnlpVersion==null && (\r
120                 System.getProperty("java.awt.headless")==null\r
121              || System.getProperty("java.awt.headless").equals("false")))\r
122         {\r
123 \r
124 \r
125           class VersionChecker\r
126               extends Thread\r
127           {\r
128             public void run()\r
129             {\r
130               String jnlpVersion = null;\r
131               try\r
132               {\r
133                 java.net.URL url = new java.net.URL(\r
134                     "http://www.jalview.org/webstart/jalview.jnlp");\r
135                 BufferedReader in = new BufferedReader(new InputStreamReader(url.\r
136                     openStream()));\r
137                 String line = null;\r
138                 while ( (line = in.readLine()) != null)\r
139                 {\r
140                   if (line.indexOf("jalview.version") == -1)\r
141                     continue;\r
142 \r
143                   line = line.substring(line.indexOf("value=") + 7);\r
144                   line = line.substring(0, line.lastIndexOf("\""));\r
145                   jnlpVersion = line;\r
146                   break;\r
147                 }\r
148               }\r
149               catch (Exception ex)\r
150               {\r
151                 System.out.println(ex);\r
152                 jnlpVersion = getProperty("VERSION");\r
153               }\r
154 \r
155               setProperty("LATEST_VERSION", jnlpVersion);\r
156             }\r
157           }\r
158 \r
159           VersionChecker vc = new VersionChecker();\r
160           vc.start();\r
161         }\r
162         else\r
163         {\r
164           setProperty("LATEST_VERSION", jnlpVersion);\r
165         }\r
166 \r
167         setProperty("VERSION", codeVersion);\r
168 \r
169         //LOAD USERDEFINED COLOURS\r
170         jalview.gui.UserDefinedColours.initUserColourSchemes( getProperty("USER_DEFINED_COLOURS"));\r
171     }\r
172 \r
173 \r
174 \r
175     /**\r
176      * Gets Jalview application property of given key. Returns null\r
177      * if key not found\r
178      *\r
179      * @param key Name of property\r
180      *\r
181      * @return Property value\r
182      */\r
183     public static String getProperty(String key)\r
184     {\r
185         return applicationProperties.getProperty(key);\r
186     }\r
187 \r
188 \r
189     /** These methods are used when checking if the saved preference\r
190      * is different to the default setting\r
191      */\r
192 \r
193     public static boolean getDefault(String property, boolean def)\r
194     {\r
195       String string = getProperty(property);\r
196       if (string != null)\r
197       {\r
198         def = Boolean.valueOf(string).booleanValue();\r
199       }\r
200 \r
201       return def;\r
202     }\r
203 \r
204     /** These methods are used when checking if the saved preference\r
205     * is different to the default setting\r
206     */\r
207     public static String getDefault(String property, String def)\r
208     {\r
209       String string = getProperty(property);\r
210       if (string != null)\r
211       {\r
212         return string;\r
213       }\r
214 \r
215       return def;\r
216     }\r
217 \r
218 \r
219     /**\r
220      * Stores property in the file "HOME_DIR/.jalview_properties"\r
221      *\r
222      * @param key Name of object\r
223      * @param obj String value of property\r
224      *\r
225      * @return String value of property\r
226      */\r
227     public static String setProperty(String key, String obj)\r
228     {\r
229         try\r
230         {\r
231             FileOutputStream out = new FileOutputStream(propertiesFile);\r
232             applicationProperties.setProperty(key, obj);\r
233             applicationProperties.store(out, "---JalviewX Properties File---");\r
234             out.close();\r
235         }\r
236         catch (Exception ex)\r
237         {    }\r
238         return obj;\r
239     }\r
240 \r
241     public static void saveProperties()\r
242     {\r
243         try\r
244         {\r
245             FileOutputStream out = new FileOutputStream(propertiesFile);\r
246             applicationProperties.store(out, "---JalviewX Properties File---");\r
247             out.close();\r
248         }\r
249         catch (Exception ex)\r
250         {    }\r
251     }\r
252 }\r