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