View Javadoc

1   /*
2    * $Id: FileMessageAdapter.java 11565 2008-04-11 09:26:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * <code>FileMessageAdapter</code> provides a wrapper for a file reference. Users
23   * can obtain the contents of the message through the payload property and can get
24   * the filename and directory in the properties using FileConnector.PROPERTY_FILENAME
25   * and FileConnector.PROPERTY_DIRECTORY.<br>
26   * This message adaptor supports both InputStream and File payload types.
27   */
28  public class FileMessageAdapter extends AbstractMessageAdapter
29  {
30      /** Serial version */
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  }