1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package net.sf.bluecove.se;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.util.Properties;
33
34 import net.sf.bluecove.util.Storage;
35
36
37
38
39 public class FileStorage implements Storage {
40
41 private Properties properties;
42
43 private long propertiesFileLoadedLastModified = 0;
44
45 private File propertyFile;
46
47 public FileStorage() {
48
49 }
50
51
52
53
54
55
56 public synchronized String retriveData(String name) {
57 return getProperties().getProperty(name);
58 }
59
60
61
62
63
64
65
66 public synchronized void storeData(String name, String value) {
67 Properties p = getProperties();
68 if (name != null) {
69 if (value == null) {
70 if (p.remove(name) == null) {
71
72 return;
73 }
74 } else {
75 if (value.equals(p.put(name, value))) {
76
77 return;
78 }
79 }
80 }
81 File f = getPropertyFile();
82 if (f == null) {
83 return;
84 }
85 FileOutputStream out = null;
86 try {
87 out = new FileOutputStream(f);
88
89 p.save(out, "");
90 } catch (FileNotFoundException ignore) {
91 }
92 try {
93 out.close();
94 } catch (Throwable ignore) {
95 }
96 propertiesFileLoadedLastModified = f.lastModified();
97 }
98
99 private static File homePath() {
100 String path = ".bluecove";
101 boolean isWindows = false;
102 String sysName = System.getProperty("os.name");
103 if (sysName != null) {
104 sysName = sysName.toLowerCase();
105 if (sysName.indexOf("windows") != -1) {
106 isWindows = true;
107 path = "Application Data";
108 }
109 }
110 File dir;
111 try {
112 dir = new File(System.getProperty("user.home"), path);
113 if (!dir.exists()) {
114 if (!dir.mkdirs()) {
115 throw new SecurityException();
116 }
117 }
118 } catch (SecurityException e) {
119 dir = new File(new File(System.getProperty("java.io.tmpdir"), System.getProperty("user.name")), path);
120 }
121 if (isWindows) {
122 dir = new File(dir, "BlueCove");
123 }
124 if (!dir.exists()) {
125 if (!dir.mkdirs()) {
126 return null;
127 }
128 } else if (!dir.isDirectory()) {
129 dir.delete();
130 if (!dir.mkdirs()) {
131 return null;
132 }
133 }
134 return dir;
135 }
136
137 private File getPropertyFile() {
138 if (propertyFile != null) {
139 return propertyFile;
140 }
141 File homeDir = homePath();
142 if (homeDir == null) {
143 try {
144 propertyFile = File.createTempFile("bluecove-tester", ".properties");
145 return propertyFile;
146 } catch (IOException e) {
147 return null;
148 }
149 }
150
151
152 String id = "";
153 try {
154 String stack = System.getProperty("bluecove.stack");
155 if (stack != null) {
156 id = stack;
157 }
158 } catch (SecurityException ignore) {
159
160 }
161 try {
162 String deviceID = System.getProperty("bluecove.deviceID");
163 if (deviceID != null) {
164 id += deviceID;
165 }
166 } catch (SecurityException ignore) {
167
168 }
169 propertyFile = new File(homeDir, "bluecove-tester" + id + ".properties");
170 return propertyFile;
171 }
172
173 private Properties getProperties() {
174 File f = getPropertyFile();
175 long lastModified = 0;
176
177 if (f != null && f.exists()) {
178 lastModified = f.lastModified();
179 } else {
180 lastModified = propertiesFileLoadedLastModified;
181 }
182
183 if ((properties != null) && (propertiesFileLoadedLastModified == lastModified)) {
184 return properties;
185 }
186 Properties p = new Properties();
187 if (f != null && f.exists()) {
188 FileInputStream in = null;
189 try {
190 in = new FileInputStream(f);
191 p.load(in);
192 } catch (IOException ignore) {
193 } finally {
194 try {
195 in.close();
196 } catch (Throwable ignore) {
197 }
198 }
199 }
200 propertiesFileLoadedLastModified = lastModified;
201 properties = p;
202 return properties;
203 }
204
205 }