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