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