1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.file; |
12 | |
|
13 | |
import org.mule.MuleException; |
14 | |
import org.mule.MuleManager; |
15 | |
import org.mule.impl.MuleMessage; |
16 | |
import org.mule.providers.AbstractMessageDispatcher; |
17 | |
import org.mule.providers.file.filters.FilenameWildcardFilter; |
18 | |
import org.mule.providers.file.i18n.FileMessages; |
19 | |
import org.mule.umo.UMOEvent; |
20 | |
import org.mule.umo.UMOException; |
21 | |
import org.mule.umo.UMOMessage; |
22 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
23 | |
import org.mule.util.FileUtils; |
24 | |
|
25 | |
import java.io.File; |
26 | |
import java.io.FileOutputStream; |
27 | |
import java.io.FilenameFilter; |
28 | |
import java.net.URLDecoder; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class FileMessageDispatcher extends AbstractMessageDispatcher |
34 | |
{ |
35 | |
private final FileConnector connector; |
36 | |
|
37 | |
public FileMessageDispatcher(UMOImmutableEndpoint endpoint) |
38 | |
{ |
39 | 4 | super(endpoint); |
40 | 4 | this.connector = (FileConnector) endpoint.getConnector(); |
41 | 4 | } |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
protected void doDispatch(UMOEvent event) throws Exception |
49 | |
{ |
50 | 0 | Object data = event.getTransformedMessage(); |
51 | |
|
52 | 0 | UMOMessage message = new MuleMessage(data, event.getMessage()); |
53 | |
|
54 | |
byte[] buf; |
55 | 0 | if (data instanceof byte[]) |
56 | |
{ |
57 | 0 | buf = (byte[]) data; |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | 0 | buf = data.toString().getBytes(event.getEncoding()); |
62 | |
} |
63 | |
|
64 | 0 | FileOutputStream fos = (FileOutputStream) connector.getOutputStream(event.getEndpoint(), message); |
65 | 0 | if (event.getMessage().getStringProperty(FileConnector.PROPERTY_FILENAME, null) == null) |
66 | |
{ |
67 | 0 | event.getMessage().setStringProperty(FileConnector.PROPERTY_FILENAME, |
68 | |
message.getStringProperty(FileConnector.PROPERTY_FILENAME, "")); |
69 | |
} |
70 | |
try |
71 | |
{ |
72 | 0 | fos.write(buf); |
73 | |
} |
74 | |
finally |
75 | |
{ |
76 | 0 | fos.close(); |
77 | 0 | } |
78 | 0 | } |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
protected UMOMessage doReceive(long timeout) throws Exception |
92 | |
{ |
93 | 4 | File file = FileUtils.newFile(endpoint.getEndpointURI().getAddress()); |
94 | 4 | File result = null; |
95 | 4 | FilenameFilter filenameFilter = null; |
96 | 4 | String filter = (String) endpoint.getProperty("filter"); |
97 | 4 | if (filter != null) |
98 | |
{ |
99 | 0 | filter = URLDecoder.decode(filter, MuleManager.getConfiguration().getEncoding()); |
100 | 0 | filenameFilter = new FilenameWildcardFilter(filter); |
101 | |
} |
102 | 4 | if (file.exists()) |
103 | |
{ |
104 | 4 | if (file.isFile()) |
105 | |
{ |
106 | 0 | result = file; |
107 | |
} |
108 | 4 | else if (file.isDirectory()) |
109 | |
{ |
110 | 4 | result = getNextFile(endpoint.getEndpointURI().getAddress(), filenameFilter); |
111 | |
} |
112 | 4 | if (result != null) |
113 | |
{ |
114 | 4 | boolean checkFileAge = connector.getCheckFileAge(); |
115 | 4 | if (checkFileAge) |
116 | |
{ |
117 | 0 | long fileAge = connector.getFileAge(); |
118 | 0 | long lastMod = result.lastModified(); |
119 | 0 | long now = System.currentTimeMillis(); |
120 | 0 | long thisFileAge = now - lastMod; |
121 | 0 | if (thisFileAge < fileAge) |
122 | |
{ |
123 | 0 | if (logger.isDebugEnabled()) { |
124 | 0 | logger.debug("The file has not aged enough yet, will return nothing for: " + |
125 | |
result.getCanonicalPath()); |
126 | |
} |
127 | 0 | return null; |
128 | |
} |
129 | |
} |
130 | |
|
131 | 4 | MuleMessage message = new MuleMessage(connector.getMessageAdapter(result)); |
132 | 4 | File destinationFile = null; |
133 | 4 | if (connector.getMoveToDirectory() != null) |
134 | |
{ |
135 | 0 | destinationFile = FileUtils.newFile(connector.getMoveToDirectory(), result |
136 | |
.getName()); |
137 | 0 | if (!result.renameTo(destinationFile)) |
138 | |
{ |
139 | 0 | logger.error("Failed to move file: " + result.getAbsolutePath() |
140 | |
+ " to " + destinationFile.getAbsolutePath()); |
141 | |
} |
142 | |
} |
143 | |
|
144 | 4 | if (connector.isAutoDelete()) |
145 | |
{ |
146 | |
|
147 | 2 | if (destinationFile == null) |
148 | |
{ |
149 | |
|
150 | 2 | if (!result.delete()) |
151 | |
{ |
152 | 0 | throw new MuleException( |
153 | |
FileMessages.failedToDeleteFile(result.getAbsolutePath())); |
154 | |
} |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
} |
160 | |
|
161 | 4 | return message; |
162 | |
} |
163 | |
} |
164 | 0 | return null; |
165 | |
} |
166 | |
|
167 | |
private File getNextFile(String dir, FilenameFilter filter) throws UMOException |
168 | |
{ |
169 | |
File[] files; |
170 | 4 | File file = FileUtils.newFile(dir); |
171 | 4 | File result = null; |
172 | |
try |
173 | |
{ |
174 | 4 | if (file.exists()) |
175 | |
{ |
176 | 4 | if (file.isFile()) |
177 | |
{ |
178 | 0 | result = file; |
179 | |
} |
180 | 4 | else if (file.isDirectory()) |
181 | |
{ |
182 | 4 | if (filter != null) |
183 | |
{ |
184 | 0 | files = file.listFiles(filter); |
185 | |
} |
186 | |
else |
187 | |
{ |
188 | 4 | files = file.listFiles(); |
189 | |
} |
190 | 4 | if (files.length > 0) |
191 | |
{ |
192 | 4 | result = files[0]; |
193 | |
} |
194 | |
} |
195 | |
} |
196 | 4 | return result; |
197 | |
} |
198 | 0 | catch (Exception e) |
199 | |
{ |
200 | 0 | throw new MuleException(FileMessages.errorWhileListingFiles(), e); |
201 | |
} |
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
protected UMOMessage doSend(UMOEvent event) throws Exception |
210 | |
{ |
211 | 0 | doDispatch(event); |
212 | 0 | return event.getMessage(); |
213 | |
} |
214 | |
|
215 | |
protected void doDispose() |
216 | |
{ |
217 | |
|
218 | 4 | } |
219 | |
|
220 | |
protected void doConnect() throws Exception |
221 | |
{ |
222 | |
|
223 | 4 | } |
224 | |
|
225 | |
protected void doDisconnect() throws Exception |
226 | |
{ |
227 | |
|
228 | 4 | } |
229 | |
|
230 | |
} |