View Javadoc

1   /*
2    * $Id: FileContentsMessageAdapter.java 7991 2007-08-22 13:14:57Z 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.MuleRuntimeException;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.impl.ThreadSafeAccess;
16  import org.mule.umo.MessagingException;
17  
18  import java.io.File;
19  
20  /**
21   * <code>FileContentsMessageAdapter</code> provides a wrapper for file data. Users
22   * can obtain the contents of the message through the payload property and can get
23   * the filename and directory in the properties using PROPERTY_FILENAME and
24   * PROPERTY_DIRECTORY.
25   */
26  public class FileContentsMessageAdapter extends FileMessageAdapter
27  {
28      /**
29       * Serial version
30       */
31      private static final long serialVersionUID = 7368719494535568721L;
32  
33      public FileContentsMessageAdapter(Object message) throws MessagingException
34      {
35          super(message);
36      }
37  
38      public FileContentsMessageAdapter(FileContentsMessageAdapter template)
39      {
40          super(template);
41      }
42      
43      protected void setMessage(File message) throws MessagingException
44      {
45          super.setMessage(message);
46          // force reading of file (lazy loading would be really, really complicated)
47          this.getPayload();
48      }
49  
50      public Object getPayload()
51      {
52          synchronized (this)
53          {
54              try
55              {
56                  return this.getPayloadAsBytes();
57              }
58              catch (Exception noPayloadException)
59              {
60                  throw new MuleRuntimeException(CoreMessages.failedToReadPayload(), noPayloadException);
61              }
62          }
63      }
64      
65      public ThreadSafeAccess newThreadCopy()
66      {
67          return new FileContentsMessageAdapter(this);
68      }
69  }