1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.utils;
18 import java.io.IOException;
19 import java.io.File;
20 import java.util.Arrays;
21 import java.io.FileOutputStream;
22 import java.util.Enumeration;
23 import java.util.Properties;
24 import java.io.OutputStreamWriter;
25 import java.io.BufferedWriter;
26 import java.io.OutputStream;
27 import java.io.PrintWriter;
28 import java.util.Date;
29
30
31
32
33
34
35
36
37
38 public class SortedFileProperties extends SecureFileProperties {
39
40
41
42
43 private static final long serialVersionUID = 14376457265L;
44
45
46
47
48
49
50
51 public SortedFileProperties(File f, Properties defaults) {
52 super(f, defaults);
53 }
54
55
56
57
58
59
60 public SortedFileProperties(File f) {
61 super(f);
62 }
63
64
65
66
67 @Override public synchronized void commit() {
68 FileOutputStream fos = null;
69 try {
70 fos = new FileOutputStream(f);
71 this.store(fos, header);
72 BufferedWriter wb =
73 new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
74 wb.write("completed=ok");
75 wb.newLine();
76 wb.close();
77 } catch (IOException e) {
78
79 } finally {
80 try {
81 fos.close();
82 } catch (Exception ignore) {
83 }
84 this.lastModified = f.lastModified();
85 }
86 }
87
88
89
90
91 @Override
92 public synchronized void store(OutputStream out, String header) throws IOException {
93 PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "8859_1"));
94 if (header != null) {
95 writer.println("#" + header);
96 }
97 writer.println("#" + new Date().toString());
98 String[] sorted = new String[this.size()];
99 int i = 0;
100 for (Enumeration<Object> e = keys(); e.hasMoreElements();) {
101 sorted[i] = (String)e.nextElement();
102 i++;
103 }
104 Arrays.sort(sorted);
105 for (i = 0; i < sorted.length; i++) {
106 String key = sorted[i];
107 String val = (String)get(key);
108 writer.println(key + "=" + val);
109 }
110 writer.flush();
111 }
112 }