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