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 protected File file = null;
34 protected InputStream fileInputStream;
35
36 public FileMessageAdapter(Object message) throws MessagingException
37 {
38 super();
39
40 if (message instanceof File)
41 {
42 this.setFileMessage((File) message);
43 }
44 else if (message instanceof ReceiverFileInputStream)
45 {
46 this.setStreamMessage((ReceiverFileInputStream) message);
47 }
48 else
49 {
50 throw new MessageTypeNotSupportedException(message, this.getClass());
51 }
52 }
53
54 protected FileMessageAdapter(FileMessageAdapter template)
55 {
56 super(template);
57 file = template.file;
58 fileInputStream = template.fileInputStream;
59 }
60
61 public Object getPayload()
62 {
63 if (fileInputStream != null)
64 {
65 return fileInputStream;
66 }
67 return file;
68 }
69
70 protected void setFileMessage(File message) throws MessagingException
71 {
72 this.file = message;
73 setProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, this.file.getName());
74 setProperty(FileConnector.PROPERTY_DIRECTORY, this.file.getParent());
75 }
76
77 protected void setStreamMessage(ReceiverFileInputStream message) throws MessagingException
78 {
79 this.file = message.getCurrentFile();
80 this.fileInputStream = message;
81 setProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, this.file.getName());
82 setProperty(FileConnector.PROPERTY_DIRECTORY, this.file.getParent());
83 }
84
85 public String getUniqueId()
86 {
87 return file.getAbsolutePath();
88 }
89
90 public ThreadSafeAccess newThreadCopy()
91 {
92 return new FileMessageAdapter(this);
93 }
94
95 }