9857f445ff4f51c8eb12fe097bd0b515dd3ee151
[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 import java.util.jar.*;\r
25 \r
26 \r
27 public class Cache {\r
28     public static String VERSION = "Release 2.0";\r
29     public static String BUILD_DATE = "";\r
30     public static Properties applicationProperties;\r
31 \r
32     // Current properties include:\r
33     //\r
34     // LAST_DIRECTORY   , use this to cache record of where the user looked to find a file\r
35     // UNIPROT_CACHE\r
36     // USER_DEFINED_COLOUR - file describing last user set colours\r
37     // FONT_NAME\r
38     // FONT_STYLE\r
39     // FONT_SIZE\r
40     // logs.Axis.Level - one of the stringified Levels for log4j controlling the logging level for axis (used for web services)\r
41     // jalview.browser - used in the jalview.utils.browserLauncher class if it doesn't know what else to do.\r
42     public static void loadProperties() {\r
43         // get last build time.\r
44         long date = 0;\r
45 \r
46         try {\r
47             String localFile = Cache.class.getProtectionDomain().getCodeSource()\r
48                                           .getLocation().toString();\r
49             localFile = localFile.concat("!/");\r
50 \r
51             String tmpString = "jar:";\r
52             String localJarFileString = tmpString.concat(localFile);\r
53             java.net.URL localJarFileURL = new java.net.URL(localJarFileString);\r
54             java.net.JarURLConnection localJarFile = (java.net.JarURLConnection) localJarFileURL.openConnection();\r
55             date = localJarFile.getLastModified();\r
56         } catch (Exception ex) {\r
57             ex.printStackTrace();\r
58         }\r
59 \r
60         if (date == 0) {\r
61             // this is called for unpackaged class files, ie not in a Jar file\r
62             // InstallAnywhere version will find build date this way\r
63             // not entirely accurate as it only tells you when Cache.class was last compiled\r
64             java.io.File f = new java.io.File(System.getProperty("user.dir") +\r
65                     "/jalview/bin/Cache.class");\r
66             date = f.lastModified();\r
67         }\r
68 \r
69         if (date != 0) {\r
70             BUILD_DATE = new Date(date).toString();\r
71         }\r
72 \r
73         applicationProperties = new Properties();\r
74 \r
75         try {\r
76             FileInputStream in = new FileInputStream(System.getProperty(\r
77                         "user.home") + "/.jalview_properties");\r
78             applicationProperties = new Properties();\r
79             applicationProperties.load(in);\r
80             in.close();\r
81         } catch (Exception ex) {\r
82         }\r
83     }\r
84 \r
85     public static String getProperty(String key) {\r
86         return applicationProperties.getProperty(key);\r
87     }\r
88 \r
89     public static String setProperty(String key, String obj) {\r
90         try {\r
91             FileOutputStream out = new FileOutputStream(System.getProperty(\r
92                         "user.home") + "/.jalview_properties");\r
93 \r
94             applicationProperties.setProperty(key, obj);\r
95 \r
96             applicationProperties.store(out, "---JalviewX Properties File---");\r
97             out.close();\r
98         } catch (Exception ex) {\r
99         }\r
100 \r
101         return obj;\r
102     }\r
103 }\r