d8a6d3d37b56b76a592236a65c5b524828ab2ca7
[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>jalview.browser - used in the jalview.utils.browserLauncher class if it doesn't know what else to do.\r
34  * <br>SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_Y=285,SCREEN_X=371,SHOW_FULLSCREEN\r
35  * FONT_NAME,FONT_SIZE,FONT_STYLE,GAP_SYMBOL,LAST_DIRECTORY,USER_DEFINED_COLOUR\r
36  * SHOW_FULL_ID,SHOW_IDENTITY,SHOW_QUALITY,SHOW_ANNOTATIONS,SHOW_CONSERVATION,\r
37  * DEFAULT_COLOUR,DEFAULT_FILE_FORMAT,STARTUP_FILE,SHOW_STARTUP_FILE\r
38 \r
39  * @author $author$\r
40  * @version $Revision$\r
41  */\r
42 public class Cache\r
43 {\r
44 \r
45     /** Jalview Properties */\r
46     public static Properties applicationProperties;\r
47 \r
48     /** Default file is  ~/.jalview_properties */\r
49     static String propertiesFile;\r
50 \r
51     /** Called when Jalview is started */\r
52     public static void loadProperties(String propsFile)\r
53     {\r
54         applicationProperties = new Properties();\r
55         propertiesFile = propsFile;\r
56         if (propsFile == null)\r
57         {\r
58           propertiesFile = System.getProperty("user.home") + "/.jalview_properties";\r
59         }\r
60 \r
61         try\r
62         {\r
63             FileInputStream fis = new FileInputStream(propertiesFile);\r
64             applicationProperties.load(fis);\r
65             applicationProperties.remove("LATEST_VERSION");\r
66             applicationProperties.remove("VERSION");\r
67             fis.close();\r
68         }\r
69         catch (Exception ex)\r
70         {\r
71           System.out.println("Error reading properties file: "+ex);\r
72         }\r
73 \r
74         // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar\r
75         // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE\r
76         // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW\r
77          try\r
78          {\r
79              String buildDetails = "jar:"\r
80                  .concat(\r
81                  Cache.class.getProtectionDomain().getCodeSource().getLocation().toString()\r
82                  .concat("!/.build_properties")\r
83                  );\r
84 \r
85              java.net.URL localJarFileURL = new java.net.URL(buildDetails);\r
86 \r
87             InputStream in = localJarFileURL.openStream();\r
88             applicationProperties.load(in);\r
89             in.close();\r
90          }\r
91          catch (Exception ex)\r
92          {\r
93            System.out.println("Error reading build details: "+ex);\r
94            applicationProperties.remove("VERSION");\r
95         }\r
96 \r
97         String jnlpVersion = System.getProperty("jalview.version");\r
98         String codeVersion = getProperty("VERSION");\r
99 \r
100 \r
101         if(codeVersion==null)\r
102         {\r
103           // THIS SHOULD ONLY BE THE CASE WHEN TESTING!!\r
104           codeVersion = "Test";\r
105           jnlpVersion = "Test";\r
106         }\r
107 \r
108 \r
109         System.out.println("Version: "+codeVersion);\r
110 \r
111         // jnlpVersion will be null if we're using InstallAnywhere\r
112         // Dont do this check if running in headless mode\r
113         if(jnlpVersion==null && (\r
114                 System.getProperty("java.awt.headless")==null\r
115              || System.getProperty("java.awt.headless").equals("false")))\r
116         {\r
117           try{\r
118             java.net.URL url = new java.net.URL("http://www.jalview.org/webstart/jalview.jnlp");\r
119             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));\r
120             String line = null;\r
121             while( (line = in.readLine()) !=null)\r
122             {\r
123               if(line.indexOf("jalview.version")==-1)\r
124                 continue;\r
125 \r
126               line = line.substring(line.indexOf("value=")+7);\r
127               line = line.substring(0, line.lastIndexOf("\""));\r
128               jnlpVersion = line;\r
129               break;\r
130             }\r
131           }catch(Exception ex)\r
132           {  jnlpVersion =  codeVersion; }\r
133 \r
134           System.out.println("Latest : "+jnlpVersion);\r
135         }\r
136 \r
137         setProperty("LATEST_VERSION", jnlpVersion);\r
138         setProperty("VERSION", codeVersion);\r
139     }\r
140 \r
141     /**\r
142      * Gets Jalview application property of given key. Returns null\r
143      * if key not found\r
144      *\r
145      * @param key Name of property\r
146      *\r
147      * @return Property value\r
148      */\r
149     public static String getProperty(String key)\r
150     {\r
151         return applicationProperties.getProperty(key);\r
152     }\r
153 \r
154     /**\r
155      * Stores property in the file "HOME_DIR/.jalview_properties"\r
156      *\r
157      * @param key Name of object\r
158      * @param obj String value of property\r
159      *\r
160      * @return String value of property\r
161      */\r
162     public static String setProperty(String key, String obj)\r
163     {\r
164         try\r
165         {\r
166             FileOutputStream out = new FileOutputStream(propertiesFile);\r
167 \r
168             applicationProperties.setProperty(key, obj);\r
169 \r
170             applicationProperties.store(out, "---JalviewX Properties File---");\r
171             out.close();\r
172         }\r
173         catch (Exception ex)\r
174         {\r
175         }\r
176 \r
177         return obj;\r
178     }\r
179 }\r