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