Coverage Report - org.mule.transport.ftp.FtpConnectionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
FtpConnectionFactory
85%
29/34
50%
5/10
3
 
 1  
 /*
 2  
  * $Id: FtpConnectionFactory.java 10489 2008-01-23 17:53:38Z 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.ftp;
 12  
 
 13  
 import org.mule.api.endpoint.EndpointURI;
 14  
 
 15  
 import java.io.IOException;
 16  
 
 17  
 import org.apache.commons.net.ftp.FTP;
 18  
 import org.apache.commons.net.ftp.FTPClient;
 19  
 import org.apache.commons.net.ftp.FTPReply;
 20  
 import org.apache.commons.pool.PoolableObjectFactory;
 21  
 
 22  
 public class FtpConnectionFactory implements PoolableObjectFactory
 23  
 {
 24  
     private EndpointURI uri;
 25  
 
 26  
     public FtpConnectionFactory(EndpointURI uri)
 27  10
     {
 28  10
         this.uri = uri;
 29  10
     }
 30  
 
 31  
     public Object makeObject() throws Exception
 32  
     {
 33  11
         FTPClient client = new FTPClient();
 34  
         try
 35  
         {
 36  11
             if (uri.getPort() > 0)
 37  
             {
 38  11
                 client.connect(uri.getHost(), uri.getPort());
 39  
             }
 40  
             else
 41  
             {
 42  0
                 client.connect(uri.getHost());
 43  
             }
 44  8
             if (!FTPReply.isPositiveCompletion(client.getReplyCode()))
 45  
             {
 46  0
                 throw new IOException("Ftp error: " + client.getReplyCode());
 47  
             }
 48  8
             if (!client.login(uri.getUser(), uri.getPassword()))
 49  
             {
 50  0
                 throw new IOException("Ftp error: " + client.getReplyCode());
 51  
             }
 52  8
             if (!client.setFileType(FTP.BINARY_FILE_TYPE))
 53  
             {
 54  0
                 throw new IOException("Ftp error. Couldn't set BINARY transfer type.");
 55  
             }
 56  
         }
 57  3
         catch (Exception e)
 58  
         {
 59  3
             if (client.isConnected())
 60  
             {
 61  0
                 client.disconnect();
 62  
             }
 63  3
             throw e;
 64  8
         }
 65  8
         return client;
 66  
     }
 67  
 
 68  
     public void destroyObject(Object obj) throws Exception
 69  
     {
 70  7
         FTPClient client = (FTPClient) obj;
 71  7
         client.logout();
 72  4
         client.disconnect();
 73  4
     }
 74  
 
 75  
     public boolean validateObject(Object obj)
 76  
     {
 77  20
         FTPClient client = (FTPClient) obj;
 78  
         try
 79  
         {
 80  20
             client.sendNoOp();
 81  19
             return true;
 82  
         }
 83  1
         catch (IOException e)
 84  
         {
 85  1
             return false;
 86  
         }
 87  
     }
 88  
 
 89  
     public void activateObject(Object obj) throws Exception
 90  
     {
 91  24
         FTPClient client = (FTPClient) obj;
 92  24
         client.setReaderThread(true);
 93  24
     }
 94  
 
 95  
     public void passivateObject(Object obj) throws Exception
 96  
     {
 97  22
         FTPClient client = (FTPClient) obj;
 98  22
         client.setReaderThread(false);
 99  22
     }
 100  
 }
 101