URL getresource adds %20 for spaces. Use user.dir instead
[jalview.git] / src / jalview / bin / Cache.java
1 /********************\r
2  * 2004 Jalview Reengineered\r
3  * Barton Group\r
4  * Dundee University\r
5  *\r
6  * AM Waterhouse\r
7  *******************/\r
8 \r
9 \r
10 package jalview.bin;\r
11 import java.util.*;\r
12 import java.io.*;\r
13 \r
14 import java.util.jar.*;\r
15 public class Cache\r
16 {\r
17   public static String  VERSION = "Release 2.0";\r
18   public static String  BUILD_DATE = "";\r
19 \r
20   public static Properties applicationProperties;\r
21   // Current properties include:\r
22   //\r
23   // LAST_DIRECTORY   , use this to cache record of where the user looked to find a file\r
24   // UNIPROT_CACHE\r
25   // USER_DEFINED_COLOUR - file describing last user set colours\r
26   // FONT_NAME\r
27   // FONT_STYLE\r
28   // FONT_SIZE\r
29   // logs.Axis.Level - one of the stringified Levels for log4j controlling the logging level for axis (used for web services)\r
30 \r
31   public static void loadProperties()\r
32   {\r
33     // get last build time.\r
34     long date = 0;\r
35     try{\r
36           String localFile = Cache.class.getProtectionDomain().getCodeSource().getLocation().toString();\r
37           localFile = localFile.concat("!/");\r
38           String tmpString = "jar:";\r
39           String localJarFileString = tmpString.concat(localFile);\r
40           java.net.URL localJarFileURL = new java.net.URL(localJarFileString);\r
41           java.net.JarURLConnection localJarFile = (java.net.JarURLConnection)localJarFileURL.openConnection();\r
42           date = localJarFile.getLastModified();\r
43     }catch(Exception ex){ex.printStackTrace();}\r
44     if(date==0)\r
45     {\r
46       // this is called for unpackaged class files, ie not in a Jar file\r
47       // InstallAnywhere version will find build date this way\r
48       // not entirely accurate as it only tells you when Cache.class was last compiled\r
49 \r
50 \r
51      java.io.File f = new java.io.File(System.getProperty("user.dir") + "/jalview/bin/Cache.class" );\r
52      date = f.lastModified();\r
53     }\r
54 \r
55     if(date!=0)\r
56       BUILD_DATE = new Date( date ).toString();\r
57 \r
58     applicationProperties = new Properties();\r
59 \r
60     try\r
61     {\r
62       FileInputStream in = new FileInputStream(System.getProperty("user.home") +\r
63                                                "/.jalview_properties");\r
64       applicationProperties = new Properties();\r
65       applicationProperties.load(in);\r
66       in.close();\r
67     }\r
68     catch (Exception ex)\r
69     { }\r
70   }\r
71 \r
72   public static String getProperty(String key)\r
73   {\r
74     return  applicationProperties.getProperty(key);\r
75   }\r
76 \r
77   public static String setProperty(String key, String obj)\r
78   {\r
79     try\r
80     {\r
81       FileOutputStream out = new FileOutputStream(System.getProperty(\r
82           "user.home") + "/.jalview_properties");\r
83 \r
84       applicationProperties.setProperty(key, obj);\r
85 \r
86       applicationProperties.store(out, "---JalviewX Properties File---");\r
87       out.close();\r
88     }\r
89     catch (Exception ex)\r
90     {}\r
91 \r
92     return obj;\r
93 \r
94   }\r
95 \r
96 \r
97 \r
98 \r
99 \r
100 }\r