System.out if error setting properties
[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>logs.Jalview.Level - Jalview.log stringified level.\r
34  * <br>DISCOVERY_START - Boolean - controls if discovery services are queried on startup\r
35  * <br>DISCOVERY_URLS - comma separated list of Discovery Service endpoints.\r
36  * <br>SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_Y=285,SCREEN_X=371,SHOW_FULLSCREEN\r
37  * FONT_NAME,FONT_SIZE,FONT_STYLE,GAP_SYMBOL,LAST_DIRECTORY,USER_DEFINED_COLOUR\r
38  * SHOW_FULL_ID,SHOW_IDENTITY,SHOW_QUALITY,SHOW_ANNOTATIONS,SHOW_CONSERVATION,\r
39  * DEFAULT_COLOUR,DEFAULT_FILE_FORMAT,STARTUP_FILE,SHOW_STARTUP_FILE\r
40 \r
41  * @author $author$\r
42  * @version $Revision$\r
43  */\r
44 public class Cache\r
45 {\r
46 \r
47     /** Jalview Properties */\r
48     public static Properties applicationProperties = new Properties();\r
49 \r
50     /** Default file is  ~/.jalview_properties */\r
51     static String propertiesFile;\r
52 \r
53     /** Called when Jalview is started */\r
54     public static void loadProperties(String propsFile)\r
55     {\r
56         propertiesFile = propsFile;\r
57         if (propsFile == null)\r
58         {\r
59           propertiesFile = System.getProperty("user.home") + "/.jalview_properties";\r
60         }\r
61 \r
62         try\r
63         {\r
64             FileInputStream fis = new FileInputStream(propertiesFile);\r
65             applicationProperties.load(fis);\r
66             applicationProperties.remove("LATEST_VERSION");\r
67             applicationProperties.remove("VERSION");\r
68             fis.close();\r
69         }\r
70         catch (Exception ex)\r
71         {\r
72           System.out.println("Error reading properties file: "+ex);\r
73         }\r
74 \r
75       if(getDefault("USE_PROXY", false))\r
76       {\r
77           System.out.println("Using proxyServer: "+getDefault("PROXY_SERVER", null)+\r
78                            " proxyPort: "+getDefault("PROXY_PORT", null));\r
79           System.setProperty("http.proxyHost", getDefault("PROXY_SERVER", null));\r
80           System.setProperty("http.proxyPort", getDefault("PROXY_PORT", null));\r
81       }\r
82 \r
83         // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar\r
84         // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE\r
85         // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW\r
86          try\r
87          {\r
88              String buildDetails = "jar:"\r
89                  .concat(\r
90                  Cache.class.getProtectionDomain().getCodeSource().getLocation().toString()\r
91                  .concat("!/.build_properties")\r
92                  );\r
93 \r
94              java.net.URL localJarFileURL = new java.net.URL(buildDetails);\r
95 \r
96             InputStream in = localJarFileURL.openStream();\r
97             applicationProperties.load(in);\r
98             in.close();\r
99          }\r
100          catch (Exception ex)\r
101          {\r
102            System.out.println("Error reading build details: "+ex);\r
103            applicationProperties.remove("VERSION");\r
104         }\r
105 \r
106         String jnlpVersion = System.getProperty("jalview.version");\r
107         String codeVersion = getProperty("VERSION");\r
108 \r
109 \r
110         if(codeVersion==null)\r
111         {\r
112           // THIS SHOULD ONLY BE THE CASE WHEN TESTING!!\r
113           codeVersion = "Test";\r
114           jnlpVersion = "Test";\r
115         }\r
116 \r
117 \r
118         System.out.println("Jalview Version: "+codeVersion);\r
119 \r
120         // jnlpVersion will be null if we're using InstallAnywhere\r
121         // Dont do this check if running in headless mode\r
122         if(jnlpVersion==null && (\r
123                 System.getProperty("java.awt.headless")==null\r
124              || System.getProperty("java.awt.headless").equals("false")))\r
125         {\r
126 \r
127 \r
128           class VersionChecker\r
129               extends Thread\r
130           {\r
131             public void run()\r
132             {\r
133               String jnlpVersion = null;\r
134               try\r
135               {\r
136                 java.net.URL url = new java.net.URL(\r
137                     "http://www.jalview.org/webstart/jalview.jnlp");\r
138                 BufferedReader in = new BufferedReader(new InputStreamReader(url.\r
139                     openStream()));\r
140                 String line = null;\r
141                 while ( (line = in.readLine()) != null)\r
142                 {\r
143                   if (line.indexOf("jalview.version") == -1)\r
144                     continue;\r
145 \r
146                   line = line.substring(line.indexOf("value=") + 7);\r
147                   line = line.substring(0, line.lastIndexOf("\""));\r
148                   jnlpVersion = line;\r
149                   break;\r
150                 }\r
151               }\r
152               catch (Exception ex)\r
153               {\r
154                 System.out.println(ex);\r
155                 jnlpVersion = getProperty("VERSION");\r
156               }\r
157 \r
158               setProperty("LATEST_VERSION", jnlpVersion);\r
159             }\r
160           }\r
161 \r
162           VersionChecker vc = new VersionChecker();\r
163           vc.start();\r
164         }\r
165         else\r
166         {\r
167           setProperty("LATEST_VERSION", jnlpVersion);\r
168         }\r
169 \r
170         setProperty("VERSION", codeVersion);\r
171 \r
172         //LOAD USERDEFINED COLOURS\r
173         jalview.gui.UserDefinedColours.initUserColourSchemes( getProperty("USER_DEFINED_COLOURS"));\r
174     }\r
175 \r
176 \r
177 \r
178     /**\r
179      * Gets Jalview application property of given key. Returns null\r
180      * if key not found\r
181      *\r
182      * @param key Name of property\r
183      *\r
184      * @return Property value\r
185      */\r
186     public static String getProperty(String key)\r
187     {\r
188         return applicationProperties.getProperty(key);\r
189     }\r
190 \r
191 \r
192     /** These methods are used when checking if the saved preference\r
193      * is different to the default setting\r
194      */\r
195 \r
196     public static boolean getDefault(String property, boolean def)\r
197     {\r
198       String string = getProperty(property);\r
199       if (string != null)\r
200       {\r
201         def = Boolean.valueOf(string).booleanValue();\r
202       }\r
203 \r
204       return def;\r
205     }\r
206 \r
207     /** These methods are used when checking if the saved preference\r
208     * is different to the default setting\r
209     */\r
210     public static String getDefault(String property, String def)\r
211     {\r
212       String string = getProperty(property);\r
213       if (string != null)\r
214       {\r
215         return string;\r
216       }\r
217 \r
218       return def;\r
219     }\r
220 \r
221 \r
222     /**\r
223      * Stores property in the file "HOME_DIR/.jalview_properties"\r
224      *\r
225      * @param key Name of object\r
226      * @param obj String value of property\r
227      *\r
228      * @return String value of property\r
229      */\r
230     public static String setProperty(String key, String obj)\r
231     {\r
232         try\r
233         {\r
234             FileOutputStream out = new FileOutputStream(propertiesFile);\r
235             applicationProperties.setProperty(key, obj);\r
236             applicationProperties.store(out, "---JalviewX Properties File---");\r
237             out.close();\r
238         }\r
239         catch (Exception ex)\r
240         {   System.out.println("Error setting property: "+key+" "+obj+"\n"+ex);   }\r
241         return obj;\r
242     }\r
243 \r
244     public static void saveProperties()\r
245     {\r
246         try\r
247         {\r
248             FileOutputStream out = new FileOutputStream(propertiesFile);\r
249             applicationProperties.store(out, "---JalviewX Properties File---");\r
250             out.close();\r
251         }\r
252         catch (Exception ex)\r
253         {  System.out.println("Error saving properties: "+ex);  }\r
254     }\r
255 }\r