1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.util.store; |
8 | |
|
9 | |
import org.mule.api.lifecycle.InitialisationException; |
10 | |
import org.mule.api.store.ObjectStoreException; |
11 | |
import org.mule.util.FileUtils; |
12 | |
import org.mule.util.IOUtils; |
13 | |
import org.mule.util.StringUtils; |
14 | |
|
15 | |
import java.io.File; |
16 | |
import java.io.FileInputStream; |
17 | |
import java.io.FileOutputStream; |
18 | |
import java.io.IOException; |
19 | |
import java.io.Serializable; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.Map; |
22 | |
import java.util.Properties; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public class TextFileObjectStore extends InMemoryObjectStore<String> |
31 | |
{ |
32 | |
protected File fileStore; |
33 | |
protected String directory; |
34 | |
protected String encoding; |
35 | |
|
36 | |
private FileOutputStream output; |
37 | |
|
38 | |
@Override |
39 | |
public void initialise() throws InitialisationException |
40 | |
{ |
41 | 0 | super.initialise(); |
42 | 0 | if (encoding == null) |
43 | |
{ |
44 | 0 | encoding = context.getConfiguration().getDefaultEncoding(); |
45 | |
} |
46 | |
|
47 | 0 | if (directory == null) |
48 | |
{ |
49 | 0 | directory = context.getConfiguration().getWorkingDirectory() + "/objectstore"; |
50 | |
} |
51 | |
|
52 | |
try |
53 | |
{ |
54 | 0 | File dir = FileUtils.openDirectory(directory); |
55 | 0 | fileStore = new File(dir, name + ".dat"); |
56 | 0 | if (fileStore.exists()) |
57 | |
{ |
58 | 0 | loadFromStore(); |
59 | |
} |
60 | |
} |
61 | 0 | catch (Exception e) |
62 | |
{ |
63 | 0 | throw new InitialisationException(e, this); |
64 | 0 | } |
65 | 0 | } |
66 | |
|
67 | |
protected synchronized void loadFromStore() throws Exception |
68 | |
{ |
69 | 0 | Properties props = new Properties(); |
70 | 0 | props.load(new FileInputStream(fileStore)); |
71 | |
|
72 | 0 | for (Map.Entry<Object, Object> entry : props.entrySet()) |
73 | |
{ |
74 | 0 | super.store(entry.getKey().toString(), entry.getValue().toString()); |
75 | |
} |
76 | 0 | } |
77 | |
|
78 | |
@Override |
79 | |
public void store(Serializable id, String item) throws ObjectStoreException |
80 | |
{ |
81 | 0 | super.store(id, item); |
82 | |
|
83 | |
try |
84 | |
{ |
85 | 0 | if (output == null) |
86 | |
{ |
87 | 0 | output = new FileOutputStream(fileStore, true); |
88 | |
} |
89 | |
|
90 | 0 | StringBuffer buf = new StringBuffer(); |
91 | 0 | buf.append(id).append("=").append(item.toString()).append(IOUtils.LINE_SEPARATOR); |
92 | 0 | output.write(buf.toString().getBytes()); |
93 | |
} |
94 | 0 | catch (IOException iox) |
95 | |
{ |
96 | 0 | throw new ObjectStoreException(iox); |
97 | 0 | } |
98 | 0 | } |
99 | |
|
100 | |
public String getDirectory() |
101 | |
{ |
102 | 0 | return directory; |
103 | |
} |
104 | |
|
105 | |
public void setDirectory(String directory) |
106 | |
{ |
107 | 0 | this.directory = directory; |
108 | 0 | } |
109 | |
|
110 | |
public String getEncoding() |
111 | |
{ |
112 | 0 | return encoding; |
113 | |
} |
114 | |
|
115 | |
public void setEncoding(String encoding) |
116 | |
{ |
117 | 0 | this.encoding = encoding; |
118 | 0 | } |
119 | |
|
120 | |
@Override |
121 | |
public synchronized void dispose() |
122 | |
{ |
123 | 0 | Properties props = new Properties(); |
124 | |
|
125 | 0 | for (Iterator<?> iterator = super.store.values().iterator(); iterator.hasNext();) |
126 | |
{ |
127 | 0 | StoredObject<?> storedObject = (StoredObject<?>) iterator.next(); |
128 | 0 | props.put(storedObject.getId(), storedObject.getItem()); |
129 | 0 | } |
130 | |
|
131 | 0 | if (output == null) |
132 | |
{ |
133 | |
try |
134 | |
{ |
135 | 0 | output = new FileOutputStream(fileStore, false); |
136 | 0 | props.store(output, StringUtils.EMPTY); |
137 | 0 | IOUtils.closeQuietly(output); |
138 | |
} |
139 | 0 | catch (IOException e) |
140 | |
{ |
141 | 0 | logger.error(e.getMessage(), e); |
142 | 0 | } |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | 0 | IOUtils.closeQuietly(output); |
147 | |
} |
148 | |
|
149 | 0 | super.dispose(); |
150 | 0 | } |
151 | |
} |