1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.file; |
12 | |
|
13 | |
import org.mule.api.MessagingException; |
14 | |
import org.mule.api.ThreadSafeAccess; |
15 | |
import org.mule.api.transport.MessageTypeNotSupportedException; |
16 | |
import org.mule.transport.AbstractMessageAdapter; |
17 | |
|
18 | |
import java.io.File; |
19 | |
import java.io.InputStream; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class FileMessageAdapter extends AbstractMessageAdapter |
29 | |
{ |
30 | |
|
31 | |
private static final long serialVersionUID = 4127485947547548996L; |
32 | |
|
33 | 69 | protected File file = null; |
34 | |
protected InputStream fileInputStream; |
35 | |
|
36 | |
public FileMessageAdapter(Object message) throws MessagingException |
37 | |
{ |
38 | 65 | super(); |
39 | |
|
40 | 65 | if (message instanceof File) |
41 | |
{ |
42 | 33 | this.setFileMessage((File) message); |
43 | |
} |
44 | 32 | else if (message instanceof ReceiverFileInputStream) |
45 | |
{ |
46 | 32 | this.setStreamMessage((ReceiverFileInputStream) message); |
47 | |
} |
48 | |
else |
49 | |
{ |
50 | 0 | throw new MessageTypeNotSupportedException(message, this.getClass()); |
51 | |
} |
52 | 65 | } |
53 | |
|
54 | |
protected FileMessageAdapter(FileMessageAdapter template) |
55 | |
{ |
56 | 4 | super(template); |
57 | 4 | file = template.file; |
58 | 4 | fileInputStream = template.fileInputStream; |
59 | 4 | } |
60 | |
|
61 | |
public Object getPayload() |
62 | |
{ |
63 | 158 | if (fileInputStream != null) |
64 | |
{ |
65 | 148 | return fileInputStream; |
66 | |
} |
67 | 10 | return file; |
68 | |
} |
69 | |
|
70 | |
protected void setFileMessage(File message) throws MessagingException |
71 | |
{ |
72 | 33 | this.file = message; |
73 | 33 | setProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, this.file.getName()); |
74 | 33 | setProperty(FileConnector.PROPERTY_DIRECTORY, this.file.getParent()); |
75 | 33 | } |
76 | |
|
77 | |
protected void setStreamMessage(ReceiverFileInputStream message) throws MessagingException |
78 | |
{ |
79 | 32 | this.file = message.getCurrentFile(); |
80 | 32 | this.fileInputStream = message; |
81 | 32 | setProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, this.file.getName()); |
82 | 32 | setProperty(FileConnector.PROPERTY_DIRECTORY, this.file.getParent()); |
83 | 32 | } |
84 | |
|
85 | |
public String getUniqueId() |
86 | |
{ |
87 | 42 | return file.getAbsolutePath(); |
88 | |
} |
89 | |
|
90 | |
public ThreadSafeAccess newThreadCopy() |
91 | |
{ |
92 | 4 | return new FileMessageAdapter(this); |
93 | |
} |
94 | |
|
95 | |
} |