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