1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.utils;
18
19 import java.io.IOException;
20 import java.io.File;
21 import java.io.InputStream;
22 import java.io.FileOutputStream;
23 import java.util.Properties;
24 import java.io.OutputStreamWriter;
25 import java.io.BufferedWriter;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30
31
32
33
34
35
36
37 public class SecureFileProperties extends FileProperties {
38
39
40
41
42 private static final long serialVersionUID = 1324523452343L;
43
44
45
46
47 private static final Log LOG = LogFactory.getLog(SecureFileProperties.class);
48
49
50
51
52
53
54
55 public SecureFileProperties(File f, Properties defaults) {
56 super(f, defaults);
57 }
58
59
60
61
62
63
64 public SecureFileProperties(File f) {
65 super(f);
66 }
67
68
69
70
71 @Override public synchronized void commit() {
72 FileOutputStream fos = null;
73 try {
74 fos = new FileOutputStream(f);
75 this.store(fos, header);
76 BufferedWriter wb = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
77 wb.write("completed=ok");
78 wb.newLine();
79 wb.close();
80 } catch (IOException e) {
81 LOG.warn("Could not commit SecureFileProperties changes", e);
82 } finally {
83 FileUtils.close(fos);
84 this.lastModified = f.lastModified();
85 }
86 }
87
88
89
90
91 @Override
92 public synchronized void load(InputStream inStream) throws IOException {
93 this.clear();
94 super.load(inStream);
95 String prop = (String) this.get("completed");
96 if (!"ok".equals(prop)) {
97 this.clear();
98 throw new IOException("The file is corrupt:" + f.getName());
99 } else {
100 this.remove("completed");
101 }
102 }
103 }