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.config.i18n.CoreMessages; |
15 | |
import org.mule.config.i18n.Message; |
16 | |
import org.mule.impl.ThreadSafeAccess; |
17 | |
import org.mule.providers.AbstractMessageAdapter; |
18 | |
import org.mule.providers.file.i18n.FileMessages; |
19 | |
import org.mule.providers.file.transformers.FileToByteArray; |
20 | |
import org.mule.umo.MessagingException; |
21 | |
import org.mule.umo.provider.MessageTypeNotSupportedException; |
22 | |
import org.mule.util.ObjectUtils; |
23 | |
|
24 | |
import java.io.File; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class FileMessageAdapter extends AbstractMessageAdapter |
33 | |
{ |
34 | |
|
35 | |
|
36 | |
|
37 | |
private static final long serialVersionUID = 4127485947547548996L; |
38 | |
|
39 | 2 | private static final FileToByteArray transformer = new FileToByteArray(); |
40 | |
|
41 | 34 | private File file = null; |
42 | 34 | private byte[] contents = null; |
43 | |
|
44 | |
public FileMessageAdapter(Object message) throws MessagingException |
45 | |
{ |
46 | 26 | super(); |
47 | |
|
48 | 26 | if (message instanceof File) |
49 | |
{ |
50 | 22 | this.setMessage((File)message); |
51 | |
} |
52 | |
else |
53 | |
{ |
54 | 4 | throw new MessageTypeNotSupportedException(message, this.getClass()); |
55 | |
} |
56 | 22 | } |
57 | |
|
58 | |
protected FileMessageAdapter(FileMessageAdapter template) |
59 | |
{ |
60 | 8 | super(template); |
61 | 8 | file = template.file; |
62 | 8 | contents = template.contents; |
63 | 8 | } |
64 | |
|
65 | |
public Object getPayload() |
66 | |
{ |
67 | 4 | return file; |
68 | |
} |
69 | |
|
70 | |
public byte[] getPayloadAsBytes() throws Exception |
71 | |
{ |
72 | 58 | synchronized (this) |
73 | |
{ |
74 | 58 | if (contents == null) |
75 | |
{ |
76 | |
try |
77 | |
{ |
78 | |
|
79 | |
|
80 | |
|
81 | 22 | this.contents = (byte[])transformer.transform(file); |
82 | |
} |
83 | 0 | catch (Exception noPayloadException) |
84 | |
{ |
85 | 0 | throw new MuleException(CoreMessages.failedToReadPayload(), noPayloadException); |
86 | 22 | } |
87 | |
} |
88 | 58 | return contents; |
89 | 0 | } |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public String getPayloadAsString(String encoding) throws Exception |
101 | |
{ |
102 | 4 | synchronized (this) |
103 | |
{ |
104 | 4 | return new String(this.getPayloadAsBytes(), encoding); |
105 | 0 | } |
106 | |
} |
107 | |
|
108 | |
protected void setMessage(File message) throws MessagingException |
109 | |
{ |
110 | |
boolean fileIsValid; |
111 | |
Exception fileInvalidException; |
112 | |
|
113 | |
try |
114 | |
{ |
115 | 24 | fileIsValid = (message != null && message.isFile()); |
116 | 24 | fileInvalidException = null; |
117 | |
} |
118 | 0 | catch (Exception ex) |
119 | |
{ |
120 | |
|
121 | 0 | fileInvalidException = ex; |
122 | 0 | fileIsValid = false; |
123 | 24 | } |
124 | |
|
125 | 24 | if (!fileIsValid) |
126 | |
{ |
127 | |
Object exceptionArg; |
128 | |
|
129 | 0 | if (fileInvalidException != null) |
130 | |
{ |
131 | 0 | exceptionArg = fileInvalidException; |
132 | |
} |
133 | |
else |
134 | |
{ |
135 | 0 | exceptionArg = ObjectUtils.toString(message, "null"); |
136 | |
} |
137 | |
|
138 | 0 | Message msg = FileMessages.fileDoesNotExist(ObjectUtils.toString(message, "null")); |
139 | |
|
140 | 0 | throw new MessagingException(msg, exceptionArg); |
141 | |
} |
142 | |
|
143 | 24 | this.file = message; |
144 | 24 | this.contents = null; |
145 | 24 | this.setProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, this.file.getName()); |
146 | 24 | this.setProperty(FileConnector.PROPERTY_DIRECTORY, this.file.getParent()); |
147 | 24 | } |
148 | |
|
149 | |
public String getUniqueId() |
150 | |
{ |
151 | 12 | return file.getAbsolutePath(); |
152 | |
} |
153 | |
|
154 | |
public ThreadSafeAccess newThreadCopy() |
155 | |
{ |
156 | 0 | return new FileMessageAdapter(this); |
157 | |
} |
158 | |
|
159 | |
} |