Coverage Report - org.mule.transport.file.FileMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
FileMessageDispatcher
70%
30/43
50%
10/20
2.75
 
 1  
 /*
 2  
  * $Id: FileMessageDispatcher.java 12325 2008-07-13 20:49:42Z rossmason $
 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.DefaultMuleMessage;
 14  
 import org.mule.api.DefaultMuleException;
 15  
 import org.mule.api.MuleEvent;
 16  
 import org.mule.api.MuleException;
 17  
 import org.mule.api.MuleMessage;
 18  
 import org.mule.api.endpoint.OutboundEndpoint;
 19  
 import org.mule.api.transport.OutputHandler;
 20  
 import org.mule.transport.AbstractMessageDispatcher;
 21  
 import org.mule.transport.file.i18n.FileMessages;
 22  
 import org.mule.util.FileUtils;
 23  
 import org.mule.util.IOUtils;
 24  
 
 25  
 import java.io.File;
 26  
 import java.io.FileOutputStream;
 27  
 import java.io.FilenameFilter;
 28  
 import java.io.InputStream;
 29  
 
 30  
 /**
 31  
  * <code>FileMessageDispatcher</code> is used to read/write files to the filesystem
 32  
  */
 33  
 public class FileMessageDispatcher extends AbstractMessageDispatcher
 34  
 {
 35  
     private final FileConnector connector;
 36  
 
 37  
     public FileMessageDispatcher(OutboundEndpoint endpoint)
 38  
     {
 39  4
         super(endpoint);
 40  4
         this.connector = (FileConnector) endpoint.getConnector();
 41  
 
 42  4
         if (endpoint.getProperty("outputAppend") != null)
 43  
         {
 44  2
             throw new IllegalArgumentException("configuring outputAppend on the file endpoint is no longer support. You can configure a the File connector instead.");
 45  
         }
 46  2
     }
 47  
 
 48  
     /*
 49  
      * (non-Javadoc)
 50  
      * 
 51  
      * @see org.mule.api.transport.UMOConnectorSession#dispatch(org.mule.api.MuleEvent)
 52  
      */
 53  
     protected void doDispatch(MuleEvent event) throws Exception
 54  
     {
 55  2
         Object data = event.transformMessage();
 56  
         // Wrap the transformed message before passing it to the filename parser
 57  2
         MuleMessage message = new DefaultMuleMessage(data, event.getMessage());
 58  
 
 59  2
         FileOutputStream fos = (FileOutputStream) connector.getOutputStream(event.getEndpoint(), message);
 60  
         try
 61  
         {
 62  2
             if (event.getMessage().getStringProperty(FileConnector.PROPERTY_FILENAME, null) == null)
 63  
             {
 64  2
                 event.getMessage().setStringProperty(FileConnector.PROPERTY_FILENAME,
 65  
                         message.getStringProperty(FileConnector.PROPERTY_FILENAME, ""));
 66  
             }
 67  
 
 68  2
             if (data instanceof byte[])
 69  
             {
 70  0
                 fos.write((byte[]) data);
 71  
             }
 72  2
             else if (data instanceof String)
 73  
             {
 74  2
                 fos.write(data.toString().getBytes(event.getEncoding()));
 75  
             }
 76  0
             else if (data instanceof OutputHandler)
 77  
             {
 78  0
                 ((OutputHandler) data).write(event, fos);
 79  
             }
 80  
             else
 81  
             {
 82  0
                 InputStream is = (InputStream) event.transformMessage(InputStream.class);
 83  0
                 IOUtils.copyLarge(is, fos);
 84  0
                 is.close();
 85  
             }
 86  
         }
 87  
         finally
 88  
         {
 89  2
             logger.debug("Closing file");
 90  2
             fos.close();
 91  2
         }
 92  2
     }
 93  
 
 94  
     /**
 95  
      * There is no associated session for a file connector
 96  
      *
 97  
      * @throws MuleException
 98  
      */
 99  
     public Object getDelegateSession() throws MuleException
 100  
     {
 101  0
         return null;
 102  
     }
 103  
 
 104  
     protected static File getNextFile(String dir, FilenameFilter filter) throws MuleException
 105  
     {
 106  
         File[] files;
 107  4
         File file = FileUtils.newFile(dir);
 108  4
         File result = null;
 109  
         try
 110  
         {
 111  4
             if (file.exists())
 112  
             {
 113  4
                 if (file.isFile())
 114  
                 {
 115  0
                     result = file;
 116  
                 }
 117  4
                 else if (file.isDirectory())
 118  
                 {
 119  4
                     if (filter != null)
 120  
                     {
 121  0
                         files = file.listFiles(filter);
 122  
                     }
 123  
                     else
 124  
                     {
 125  4
                         files = file.listFiles();
 126  
                     }
 127  4
                     if (files.length > 0)
 128  
                     {
 129  4
                         result = files[0];
 130  
                     }
 131  
                 }
 132  
             }
 133  4
             return result;
 134  
         }
 135  0
         catch (Exception e)
 136  
         {
 137  0
             throw new DefaultMuleException(FileMessages.errorWhileListingFiles(), e);
 138  
         }
 139  
     }
 140  
 
 141  
     /*
 142  
      * (non-Javadoc)
 143  
      * 
 144  
      * @see org.mule.api.transport.UMOConnectorSession#send(org.mule.api.MuleEvent)
 145  
      */
 146  
     protected MuleMessage doSend(MuleEvent event) throws Exception
 147  
     {
 148  0
         doDispatch(event);
 149  0
         return event.getMessage();
 150  
     }
 151  
 
 152  
     protected void doDispose()
 153  
     {
 154  
         // no op
 155  2
     }
 156  
 
 157  
     protected void doConnect() throws Exception
 158  
     {
 159  
         // no op
 160  2
     }
 161  
 
 162  
     protected void doDisconnect() throws Exception
 163  
     {
 164  
         // no op
 165  2
     }
 166  
 
 167  
 }