1
2
3
4
5
6
7
8
9
10
11 package org.mule.util.queue;
12
13 import org.mule.MuleManager;
14 import org.mule.config.MuleConfiguration;
15 import org.mule.util.FileUtils;
16 import org.mule.util.file.DeleteException;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.safehaus.uuid.UUIDGenerator;
31
32 public class FilePersistenceStrategy implements QueuePersistenceStrategy
33 {
34
35 private static final Log logger = LogFactory.getLog(FilePersistenceStrategy.class);
36
37 public static final String EXTENSION = ".msg";
38
39 private File store;
40
41 private UUIDGenerator gen = UUIDGenerator.getInstance();
42
43 public FilePersistenceStrategy()
44 {
45 super();
46 }
47
48 protected String getId(Object obj)
49 {
50 String id = gen.generateRandomBasedUUID().toString();
51 return id;
52 }
53
54
55
56
57
58
59 public Object store(String queue, Object obj) throws IOException
60 {
61 String id = getId(obj);
62 File file = FileUtils.newFile(store, queue + File.separator + id + EXTENSION);
63 file.getParentFile().mkdirs();
64 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
65 oos.writeObject(obj);
66 oos.close();
67 return id;
68 }
69
70
71
72
73
74
75 public void remove(String queue, Object id) throws IOException
76 {
77 File file = FileUtils.newFile(store, queue + File.separator + id + EXTENSION);
78 if (file.exists())
79 {
80 if (!file.delete())
81 {
82 throw new DeleteException(file);
83 }
84 }
85 else
86 {
87 throw new FileNotFoundException(file.toString());
88 }
89 }
90
91
92
93
94
95
96 public Object load(String queue, Object id) throws IOException
97 {
98 File file = FileUtils.newFile(store, queue + File.separator + id + EXTENSION);
99 ObjectInputStream ois = null;
100 try
101 {
102 ois = new ObjectInputStream(new FileInputStream(file));
103 Object obj = ois.readObject();
104 return obj;
105 }
106 catch (ClassNotFoundException e)
107 {
108 throw (IOException) new IOException("Error loading persistent object").initCause(e);
109 }
110 finally
111 {
112 if (ois != null)
113 {
114 ois.close();
115 }
116 }
117 }
118
119
120
121
122
123
124 public List restore() throws IOException
125 {
126 List msgs = new ArrayList();
127 if (store == null)
128 {
129 logger.warn("No store has be set on the File Persistence Strategy. Not restoring at this time");
130 return msgs;
131 }
132 try
133 {
134 restoreFiles(store, msgs);
135 logger.debug("Restore retrieved " + msgs.size() + " objects");
136 return msgs;
137 }
138 catch (ClassNotFoundException e)
139 {
140 throw (IOException) new IOException("Could not restore").initCause(e);
141 }
142 }
143
144 protected void restoreFiles(File dir, List msgs) throws IOException, ClassNotFoundException
145 {
146 File[] files = dir.listFiles();
147 if (files == null)
148 {
149 return;
150 }
151
152 for (int i = 0; i < files.length; i++)
153 {
154 if (files[i].isDirectory())
155 {
156 restoreFiles(files[i], msgs);
157 }
158 else if (files[i].getName().endsWith(EXTENSION))
159 {
160 String id = files[i].getCanonicalPath();
161 id = id.substring(store.getCanonicalPath().length() + 1, id.length() - EXTENSION.length());
162 String queue = id.substring(0, id.indexOf(File.separator));
163 id = id.substring(queue.length() + 1);
164 msgs.add(new HolderImpl(queue, id));
165 }
166 }
167 }
168
169
170
171
172
173
174 public void open() throws IOException
175 {
176 String path = MuleManager.getConfiguration().getWorkingDirectory() + File.separator
177 + MuleConfiguration.DEFAULT_QUEUE_STORE;
178 store = FileUtils.newFile(path).getCanonicalFile();
179 store.mkdirs();
180 }
181
182
183
184
185
186
187 public void close() throws IOException
188 {
189
190 }
191
192 protected static class HolderImpl implements Holder
193 {
194 private String queue;
195 private Object id;
196
197 public HolderImpl(String queue, Object id)
198 {
199 this.queue = queue;
200 this.id = id;
201 }
202
203 public Object getId()
204 {
205 return id;
206 }
207
208 public String getQueue()
209 {
210 return queue;
211 }
212 }
213
214 public boolean isTransient()
215 {
216 return false;
217 }
218 }