View Javadoc

1   /*
2    * $Id: FileMessageAdapter.java 7963 2007-08-21 08:53: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.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   * <code>FileMessageAdapter</code> provides a wrapper for a file reference. Users
28   * can obtain the contents of the message through the payload property and can get
29   * the filename and directory in the properties using FileConnector.PROPERTY_FILENAME and
30   * FileConnector.PROPERTY_DIRECTORY.
31   */
32  public class FileMessageAdapter extends AbstractMessageAdapter
33  {
34      /**
35       * Serial version
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                      // TODO unfortunately reading the file here is required,
79                      // since otherwise the FileMessageReceiver might delete the
80                      // file
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       * Converts the message implementation into a String representation
94       * 
95       * @param encoding The encoding to use when transforming the message (if
96       *            necessary). The parameter is used when converting from a byte array
97       * @return String representation of the message payload
98       * @throws Exception Implementation may throw an endpoint specific exception
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             // save any file access exceptions
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 }