Add test case if code version not found
[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 \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             fis.close();\r
67         }\r
68         catch (Exception ex)\r
69         {\r
70           System.out.println("Error reading properties file: "+ex);\r
71         }\r
72 \r
73         // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar\r
74         // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE\r
75         // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW\r
76          try\r
77          {\r
78              String buildDetails = "jar:"\r
79                  .concat(\r
80                  Cache.class.getProtectionDomain().getCodeSource().getLocation().toString()\r
81                  .concat("!/.build_properties")\r
82                  );\r
83 \r
84              java.net.URL localJarFileURL = new java.net.URL(buildDetails);\r
85 \r
86             InputStream in = localJarFileURL.openStream();\r
87             applicationProperties.load(in);\r
88             in.close();\r
89          }\r
90          catch (Exception ex)\r
91          {\r
92            System.out.println("Error reading build details: "+ex);\r
93            applicationProperties.remove("VERSION");\r
94         }\r
95 \r
96         String jnlpVersion = System.getProperty("jalview.version");\r
97         String codeVersion = getProperty("VERSION");\r
98 \r
99         if(codeVersion==null)\r
100         {\r
101           // THIS SHOULD ONLY BE THE CASE WHEN TESTING!!\r
102           codeVersion = "Test";\r
103           jnlpVersion = "Test";\r
104           setProperty("jalview.version", jnlpVersion);\r
105           setProperty("VERSION", codeVersion);\r
106         }\r
107 \r
108 \r
109         // jnlpVersion will be null if we're using InstallAnywhere\r
110         if(jnlpVersion==null)\r
111         {\r
112           try{\r
113             java.net.URL url = new java.net.URL("http://www.jalview.org/webstart/jalview.jnlp");\r
114             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));\r
115             String line = null;\r
116             while( (line = in.readLine()) !=null)\r
117             {\r
118               if(line.indexOf("jalview.version")==-1)\r
119                 continue;\r
120 \r
121               line = line.substring(line.indexOf("value=")+7);\r
122               line = line.substring(0, line.lastIndexOf("\""));\r
123               setProperty("jalview.version", line);\r
124             }\r
125           }catch(Exception ex)\r
126           {  setProperty("jalview.version", codeVersion); }\r
127         }\r
128     }\r
129 \r
130     /**\r
131      * Gets Jalview application property of given key. Returns null\r
132      * if key not found\r
133      *\r
134      * @param key Name of property\r
135      *\r
136      * @return Property value\r
137      */\r
138     public static String getProperty(String key)\r
139     {\r
140         return applicationProperties.getProperty(key);\r
141     }\r
142 \r
143     /**\r
144      * Stores property in the file "HOME_DIR/.jalview_properties"\r
145      *\r
146      * @param key Name of object\r
147      * @param obj String value of property\r
148      *\r
149      * @return String value of property\r
150      */\r
151     public static String setProperty(String key, String obj)\r
152     {\r
153         try\r
154         {\r
155             FileOutputStream out = new FileOutputStream(propertiesFile);\r
156 \r
157             applicationProperties.setProperty(key, obj);\r
158 \r
159             applicationProperties.store(out, "---JalviewX Properties File---");\r
160             out.close();\r
161         }\r
162         catch (Exception ex)\r
163         {\r
164         }\r
165 \r
166         return obj;\r
167     }\r
168 }\r