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