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 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 super.initialise();
44 if (encoding == null)
45 {
46 encoding = context.getConfiguration().getDefaultEncoding();
47 }
48
49 if (directory == null)
50 {
51 directory = context.getConfiguration().getWorkingDirectory() + "/objectstore";
52 }
53
54 try
55 {
56 File dir = FileUtils.openDirectory(directory);
57 fileStore = new File(dir, name + ".dat");
58 if (fileStore.exists())
59 {
60 loadFromStore();
61 }
62 }
63 catch (Exception e)
64 {
65 throw new InitialisationException(e, this);
66 }
67
68 }
69
70 protected synchronized void loadFromStore() throws Exception
71 {
72 Properties props = new Properties();
73 props.load(new FileInputStream(fileStore));
74 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
75 {
76 Map.Entry entry = (Map.Entry) iterator.next();
77 super.storeObject(entry.getKey().toString(), entry.getValue());
78 }
79 }
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 if (!(item instanceof String))
96 {
97 throw new IllegalArgumentException("TextFile store can only be used for storing text entries");
98 }
99 boolean result = super.storeObject(id, item);
100 if (output == null)
101 {
102 output = new FileOutputStream(fileStore, true);
103 }
104 StringBuffer buf = new StringBuffer();
105 buf.append(id).append("=").append(item.toString()).append(IOUtils.LINE_SEPARATOR);
106 output.write(buf.toString().getBytes());
107 return result;
108 }
109
110 public String getDirectory()
111 {
112 return directory;
113 }
114
115 public void setDirectory(String directory)
116 {
117 this.directory = directory;
118 }
119
120 public String getEncoding()
121 {
122 return encoding;
123 }
124
125 public void setEncoding(String encoding)
126 {
127 this.encoding = encoding;
128 }
129
130 public synchronized void dispose()
131 {
132 Properties props = new Properties();
133
134 for (Iterator iterator = super.store.values().iterator(); iterator.hasNext();)
135 {
136 StoredObject storedObject = (StoredObject) iterator.next();
137 props.put(storedObject.getId(), storedObject.getItem());
138 }
139
140 if (output == null)
141 {
142 try
143 {
144 output = new FileOutputStream(fileStore, false);
145 props.save(output, StringUtils.EMPTY);
146 IOUtils.closeQuietly(output);
147 }
148 catch (FileNotFoundException e)
149 {
150 logger.error(e.getMessage(), e);
151 }
152 }
153 else
154 {
155 IOUtils.closeQuietly(output);
156 }
157
158
159 super.dispose();
160 }
161 }