Version changes
[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         // jnlpVersion will be null if we're using InstallAnywhere\r
109         if(jnlpVersion==null)\r
110         {\r
111           try{\r
112             java.net.URL url = new java.net.URL("http://www.jalview.org/webstart/jalview.jnlp");\r
113             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));\r
114             String line = null;\r
115             while( (line = in.readLine()) !=null)\r
116             {\r
117               if(line.indexOf("jalview.version")==-1)\r
118                 continue;\r
119 \r
120               line = line.substring(line.indexOf("value=")+7);\r
121               line = line.substring(0, line.lastIndexOf("\""));\r
122               jnlpVersion = line;\r
123               break;\r
124             }\r
125           }catch(Exception ex)\r
126           {  jnlpVersion =  codeVersion; }\r
127         }\r
128 \r
129         System.out.println("Version: "+codeVersion);\r
130         System.out.println("Latest : "+jnlpVersion);\r
131 \r
132         setProperty("LATEST_VERSION", jnlpVersion);\r
133         setProperty("VERSION", codeVersion);\r
134     }\r
135 \r
136     /**\r
137      * Gets Jalview application property of given key. Returns null\r
138      * if key not found\r
139      *\r
140      * @param key Name of property\r
141      *\r
142      * @return Property value\r
143      */\r
144     public static String getProperty(String key)\r
145     {\r
146         return applicationProperties.getProperty(key);\r
147     }\r
148 \r
149     /**\r
150      * Stores property in the file "HOME_DIR/.jalview_properties"\r
151      *\r
152      * @param key Name of object\r
153      * @param obj String value of property\r
154      *\r
155      * @return String value of property\r
156      */\r
157     public static String setProperty(String key, String obj)\r
158     {\r
159         try\r
160         {\r
161             FileOutputStream out = new FileOutputStream(propertiesFile);\r
162 \r
163             applicationProperties.setProperty(key, obj);\r
164 \r
165             applicationProperties.store(out, "---JalviewX Properties File---");\r
166             out.close();\r
167         }\r
168         catch (Exception ex)\r
169         {\r
170         }\r
171 \r
172         return obj;\r
173     }\r
174 }\r