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