e1978feaab88a8956639c0a5560f9fd82312dd7f
[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     // logs.Castor.Level - one of the stringified Levels for log4j controlling the logging level for castor (used for serialization)\r
42     // jalview.browser - used in the jalview.utils.browserLauncher class if it doesn't know what else to do.\r
43     public static void loadProperties() {\r
44         // get last build time.\r
45         long date = 0;\r
46 \r
47         try {\r
48             String localFile = Cache.class.getProtectionDomain().getCodeSource()\r
49                                           .getLocation().toString();\r
50             localFile = localFile.concat("!/");\r
51 \r
52             String tmpString = "jar:";\r
53             String localJarFileString = tmpString.concat(localFile);\r
54             java.net.URL localJarFileURL = new java.net.URL(localJarFileString);\r
55             java.net.JarURLConnection localJarFile = (java.net.JarURLConnection) localJarFileURL.openConnection();\r
56             date = localJarFile.getLastModified();\r
57         } catch (Exception ex) {\r
58             ex.printStackTrace();\r
59         }\r
60 \r
61         if (date == 0) {\r
62             // this is called for unpackaged class files, ie not in a Jar file\r
63             // InstallAnywhere version will find build date this way\r
64             // not entirely accurate as it only tells you when Cache.class was last compiled\r
65             java.io.File f = new java.io.File(System.getProperty("user.dir") +\r
66                     "/jalview/bin/Cache.class");\r
67             date = f.lastModified();\r
68         }\r
69 \r
70         if (date != 0) {\r
71             BUILD_DATE = new Date(date).toString();\r
72         }\r
73 \r
74         applicationProperties = new Properties();\r
75 \r
76         try {\r
77             FileInputStream in = new FileInputStream(System.getProperty(\r
78                         "user.home") + "/.jalview_properties");\r
79             applicationProperties = new Properties();\r
80             applicationProperties.load(in);\r
81             in.close();\r
82         } catch (Exception ex) {\r
83         }\r
84     }\r
85 \r
86     public static String getProperty(String key) {\r
87         return applicationProperties.getProperty(key);\r
88     }\r
89 \r
90     public static String setProperty(String key, String obj) {\r
91         try {\r
92             FileOutputStream out = new FileOutputStream(System.getProperty(\r
93                         "user.home") + "/.jalview_properties");\r
94 \r
95             applicationProperties.setProperty(key, obj);\r
96 \r
97             applicationProperties.store(out, "---JalviewX Properties File---");\r
98             out.close();\r
99         } catch (Exception ex) {\r
100         }\r
101 \r
102         return obj;\r
103     }\r
104 }\r