Release and build date methods changed
[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         }\r
94 \r
95         String jnlpVersion = System.getProperty("jalview.version");\r
96         String codeVersion = getProperty("VERSION");\r
97 \r
98         // jnlpVersion will be null if we're using InstallAnywhere\r
99         if(jnlpVersion==null)\r
100         {\r
101           try{\r
102             java.net.URL url = new java.net.URL("http://www.jalview.org/webstart/jalview.jnlp");\r
103             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));\r
104             String line = null;\r
105             while( (line = in.readLine()) !=null)\r
106             {\r
107               if(line.indexOf("jalview.version")==-1)\r
108                 continue;\r
109 \r
110               line = line.substring(line.indexOf("value=")+7);\r
111               line = line.substring(0, line.lastIndexOf("\""));\r
112               setProperty("jalview.version", line);\r
113             }\r
114           }catch(Exception ex)\r
115           {setProperty("jalview.version", codeVersion);}\r
116         }\r
117     }\r
118 \r
119     /**\r
120      * Gets Jalview application property of given key. Returns null\r
121      * if key not found\r
122      *\r
123      * @param key Name of property\r
124      *\r
125      * @return Property value\r
126      */\r
127     public static String getProperty(String key)\r
128     {\r
129         return applicationProperties.getProperty(key);\r
130     }\r
131 \r
132     /**\r
133      * Stores property in the file "HOME_DIR/.jalview_properties"\r
134      *\r
135      * @param key Name of object\r
136      * @param obj String value of property\r
137      *\r
138      * @return String value of property\r
139      */\r
140     public static String setProperty(String key, String obj)\r
141     {\r
142         try\r
143         {\r
144             FileOutputStream out = new FileOutputStream(propertiesFile);\r
145 \r
146             applicationProperties.setProperty(key, obj);\r
147 \r
148             applicationProperties.store(out, "---JalviewX Properties File---");\r
149             out.close();\r
150         }\r
151         catch (Exception ex)\r
152         {\r
153         }\r
154 \r
155         return obj;\r
156     }\r
157 }\r