Coverage Report - org.mule.providers.file.FileMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
FileMessageAdapter
70%
30/43
58%
7/12
2.125
 
 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  2
     private static final FileToByteArray transformer = new FileToByteArray();
 40  
 
 41  34
     private File file = null;
 42  34
     private byte[] contents = null;
 43  
 
 44  
     public FileMessageAdapter(Object message) throws MessagingException
 45  
     {
 46  26
         super();
 47  
 
 48  26
         if (message instanceof File)
 49  
         {
 50  22
             this.setMessage((File)message);
 51  
         }
 52  
         else
 53  
         {
 54  4
             throw new MessageTypeNotSupportedException(message, this.getClass());
 55  
         }
 56  22
     }
 57  
 
 58  
     protected FileMessageAdapter(FileMessageAdapter template)
 59  
     {
 60  8
         super(template);
 61  8
         file = template.file;
 62  8
         contents = template.contents;
 63  8
     }
 64  
 
 65  
     public Object getPayload()
 66  
     {
 67  4
         return file;
 68  
     }
 69  
 
 70  
     public byte[] getPayloadAsBytes() throws Exception
 71  
     {
 72  58
         synchronized (this)
 73  
         {
 74  58
             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  22
                     this.contents = (byte[])transformer.transform(file);
 82  
                 }
 83  0
                 catch (Exception noPayloadException)
 84  
                 {
 85  0
                     throw new MuleException(CoreMessages.failedToReadPayload(), noPayloadException);
 86  22
                 }
 87  
             }
 88  58
             return contents;
 89  0
         }
 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  4
         synchronized (this)
 103  
         {
 104  4
             return new String(this.getPayloadAsBytes(), encoding);
 105  0
         }
 106  
     }
 107  
 
 108  
     protected void setMessage(File message) throws MessagingException
 109  
     {
 110  
         boolean fileIsValid;
 111  
         Exception fileInvalidException;
 112  
 
 113  
         try
 114  
         {
 115  24
             fileIsValid = (message != null && message.isFile());
 116  24
             fileInvalidException = null;
 117  
         }
 118  0
         catch (Exception ex)
 119  
         {
 120  
             // save any file access exceptions
 121  0
             fileInvalidException = ex;
 122  0
             fileIsValid = false;
 123  24
         }
 124  
 
 125  24
         if (!fileIsValid)
 126  
         {
 127  
             Object exceptionArg;
 128  
 
 129  0
             if (fileInvalidException != null)
 130  
             {
 131  0
                 exceptionArg = fileInvalidException;
 132  
             }
 133  
             else
 134  
             {
 135  0
                 exceptionArg = ObjectUtils.toString(message, "null");
 136  
             }
 137  
 
 138  0
             Message msg = FileMessages.fileDoesNotExist(ObjectUtils.toString(message, "null"));
 139  
 
 140  0
             throw new MessagingException(msg, exceptionArg);
 141  
         }
 142  
 
 143  24
         this.file = message;
 144  24
         this.contents = null;
 145  24
         this.setProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, this.file.getName());
 146  24
         this.setProperty(FileConnector.PROPERTY_DIRECTORY, this.file.getParent());
 147  24
     }
 148  
 
 149  
     public String getUniqueId()
 150  
     {
 151  12
         return file.getAbsolutePath();
 152  
     }
 153  
 
 154  
     public ThreadSafeAccess newThreadCopy()
 155  
     {
 156  0
         return new FileMessageAdapter(this);
 157  
     }
 158  
 
 159  
 }