Formatting 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 \r
26 /**\r
27  * Stores and retrieves Jalview Application Properties\r
28  * <br><br>Current properties include:\r
29  * <br>logs.Axis.Level - one of the stringified Levels for log4j controlling the logging level for axis (used for web services)\r
30  * <br>logs.Castor.Level - one of the stringified Levels for log4j controlling the logging level for castor (used for serialization)\r
31  * <br>jalview.browser - used in the jalview.utils.browserLauncher class if it doesn't know what else to do.\r
32  * <br>SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_Y=285,SCREEN_X=371,SHOW_FULLSCREEN\r
33  * FONT_NAME,FONT_SIZE,FONT_STYLE,GAP_SYMBOL,LAST_DIRECTORY,USER_DEFINED_COLOUR\r
34  * SHOW_FULL_ID,SHOW_IDENTITY,SHOW_QUALITY,SHOW_ANNOTATIONS,SHOW_CONSERVATION,\r
35  * DEFAULT_COLOUR,DEFAULT_FILE_FORMAT,STARTUP_FILE,SHOW_STARTUP_FILE\r
36 \r
37  * @author $author$\r
38  * @version $Revision$\r
39  */\r
40 public class Cache\r
41 {\r
42     /** Current release tag */\r
43     public static String VERSION = "AW Test";\r
44 \r
45     /** Date Jalview was last packaged, else compilation date of Cache.class */\r
46     public static String BUILD_DATE = "";\r
47 \r
48     /** Jalview Properties */\r
49     public static Properties applicationProperties;\r
50 \r
51     /** Default file is  ~/.jalview_properties */\r
52     static String propertiesFile;\r
53 \r
54     /** Called when Jalview is started */\r
55     public static void loadProperties(String propsFile)\r
56     {\r
57       propertiesFile = propsFile;\r
58       if (propsFile == null)\r
59       {\r
60         propertiesFile = System.getProperty("user.home") + "/.jalview_properties";\r
61       }\r
62 \r
63 \r
64         // get last build time.\r
65         long date = 0;\r
66 \r
67         try\r
68         {\r
69             String localFile = Cache.class.getProtectionDomain().getCodeSource()\r
70                                           .getLocation().toString();\r
71             localFile = localFile.concat("!/");\r
72 \r
73             String tmpString = "jar:";\r
74             String localJarFileString = tmpString.concat(localFile);\r
75             java.net.URL localJarFileURL = new java.net.URL(localJarFileString);\r
76             java.net.JarURLConnection localJarFile = (java.net.JarURLConnection) localJarFileURL.openConnection();\r
77             date = localJarFile.getLastModified();\r
78         }\r
79         catch (Exception ex)\r
80         {\r
81             ex.printStackTrace();\r
82         }\r
83 \r
84         if (date == 0)\r
85         {\r
86             // this is called for unpackaged class files, ie not in a Jar file\r
87             // InstallAnywhere version will find build date this way\r
88             // not entirely accurate as it only tells you when Cache.class was last compiled\r
89             java.io.File f = new java.io.File(System.getProperty("user.dir") +\r
90                     "/jalview/bin/Cache.class");\r
91             date = f.lastModified();\r
92         }\r
93 \r
94         if (date != 0)\r
95         {\r
96             BUILD_DATE = new Date(date).toString();\r
97         }\r
98 \r
99         applicationProperties = new Properties();\r
100 \r
101         try\r
102         {\r
103             FileInputStream in = new FileInputStream(propertiesFile);\r
104             applicationProperties = new Properties();\r
105             applicationProperties.load(in);\r
106             in.close();\r
107         }\r
108         catch (Exception ex)\r
109         {\r
110         }\r
111     }\r
112 \r
113     /**\r
114      * Gets Jalview application property of given key. Returns null\r
115      * if key not found\r
116      *\r
117      * @param key Name of property\r
118      *\r
119      * @return Property value\r
120      */\r
121     public static String getProperty(String key)\r
122     {\r
123         return applicationProperties.getProperty(key);\r
124     }\r
125 \r
126     /**\r
127      * Stores property in the file "HOME_DIR/.jalview_properties"\r
128      *\r
129      * @param key Name of object\r
130      * @param obj String value of property\r
131      *\r
132      * @return String value of property\r
133      */\r
134     public static String setProperty(String key, String obj)\r
135     {\r
136         try\r
137         {\r
138             FileOutputStream out = new FileOutputStream(propertiesFile);\r
139 \r
140             applicationProperties.setProperty(key, obj);\r
141 \r
142             applicationProperties.store(out, "---JalviewX Properties File---");\r
143             out.close();\r
144         }\r
145         catch (Exception ex)\r
146         {\r
147         }\r
148 \r
149         return obj;\r
150     }\r
151 }\r