Coverage Report - org.mule.transport.file.FileContentsMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
FileContentsMessageAdapter
75%
15/20
75%
3/4
2
 
 1  
 /*
 2  
  * $Id: FileContentsMessageAdapter.java 11559 2008-04-10 18:16:04Z dfeist $
 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.MuleRuntimeException;
 15  
 import org.mule.api.ThreadSafeAccess;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 import org.mule.util.IOUtils;
 18  
 
 19  
 import java.io.FileInputStream;
 20  
 
 21  
 /**
 22  
  * <code>FileContentsMessageAdapter</code> provides a wrapper for file data. 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 PROPERTY_FILENAME and
 25  
  * PROPERTY_DIRECTORY.
 26  
  */
 27  
 public class FileContentsMessageAdapter extends FileMessageAdapter
 28  
 {
 29  
     /**
 30  
      * Serial version
 31  
      */
 32  
     private static final long serialVersionUID = 7368719494535568721L;
 33  
 
 34  27
     private byte[] contents = null;
 35  
 
 36  
     public FileContentsMessageAdapter(Object message) throws MessagingException
 37  
     {
 38  27
         super(message);
 39  27
         this.getPayload();
 40  26
     }
 41  
 
 42  
     public FileContentsMessageAdapter(FileContentsMessageAdapter template)
 43  
     {
 44  0
         super(template);
 45  0
         contents = template.contents;
 46  0
         this.getPayload();
 47  0
     }
 48  
 
 49  
     public Object getPayload()
 50  
     {
 51  149
         if (contents == null)
 52  
         {
 53  27
             synchronized (this)
 54  
             {
 55  
                 try
 56  
                 {
 57  27
                     if (fileInputStream == null)
 58  
                     {
 59  27
                         fileInputStream = new FileInputStream(file);
 60  
                     }
 61  26
                     contents = IOUtils.toByteArray(fileInputStream);
 62  26
                     fileInputStream.close();
 63  
                 }
 64  1
                 catch (Exception noPayloadException)
 65  
                 {
 66  1
                     throw new MuleRuntimeException(CoreMessages.failedToReadPayload(), noPayloadException);
 67  26
                 }
 68  26
             }
 69  
         }
 70  148
         return contents;
 71  
     }
 72  
 
 73  
     public ThreadSafeAccess newThreadCopy()
 74  
     {
 75  0
         return new FileContentsMessageAdapter(this);
 76  
     }
 77  
 }