Coverage Report - org.mule.transport.sftp.SftpConnectionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
SftpConnectionFactory
0%
0/37
0%
0/12
2.286
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transport.sftp;
 8  
 
 9  
 import org.apache.commons.pool.PoolableObjectFactory;
 10  
 import org.apache.log4j.Logger;
 11  
 import org.mule.api.endpoint.EndpointURI;
 12  
 import org.mule.api.endpoint.ImmutableEndpoint;
 13  
 
 14  
 import java.io.IOException;
 15  
 
 16  
 public class SftpConnectionFactory implements PoolableObjectFactory
 17  
 {
 18  0
     private final static Logger logger = Logger.getLogger(SftpConnectionFactory.class);
 19  
 
 20  
     private final ImmutableEndpoint endpoint;
 21  
 
 22  
     public SftpConnectionFactory(ImmutableEndpoint endpoint)
 23  0
     {
 24  0
         this.endpoint = endpoint;
 25  0
     }
 26  
 
 27  
     public void activateObject(Object o) throws Exception
 28  
     {
 29  
         // Do nothing!
 30  0
     }
 31  
 
 32  
     public void destroyObject(Object o) throws Exception
 33  
     {
 34  0
         SftpClient client = (SftpClient) o;
 35  0
         client.disconnect();
 36  0
     }
 37  
 
 38  
     public Object makeObject() throws Exception
 39  
     {
 40  0
         return createClient(endpoint);
 41  
     }
 42  
 
 43  
     public static SftpClient createClient(ImmutableEndpoint endpoint) throws Exception
 44  
     {
 45  0
         EndpointURI endpointURI = endpoint.getEndpointURI();
 46  
 
 47  0
         String host = endpointURI.getHost();
 48  0
         if (logger.isDebugEnabled())
 49  
         {
 50  0
             logger.debug("Using host: " + host);
 51  
         }
 52  
 
 53  0
         SftpClient client = new SftpClient(host);
 54  
 
 55  
         try
 56  
         {
 57  0
             int uriPort = endpointURI.getPort();
 58  0
             if (uriPort != -1)
 59  
             {
 60  0
                 if (logger.isDebugEnabled())
 61  
                 {
 62  0
                     logger.debug("Using port: " + uriPort);
 63  
                 }
 64  0
                 client.setPort(uriPort);
 65  
             }
 66  
 
 67  0
             SftpUtil sftpUtil = new SftpUtil(endpoint);
 68  0
             String identityFile = sftpUtil.getIdentityFile();
 69  
 
 70  
             /*
 71  
              * TODO: There is a problem if the SSHd uses a low value of
 72  
              * "MaxStartups", which means that if there is many new concurrent
 73  
              * connections the server will respond with "Connection reset", and thus
 74  
              * we will get exceptions of type
 75  
              * "Session.connect: java.net.SocketException: Connection reset"...
 76  
              * Solutions: increase the MaxStartups on the server or fix a retry or
 77  
              * use a exception-strategy(?)
 78  
              */
 79  
 
 80  
             // boolean succeeded = false;
 81  
             // int numberOfAttempts = 2;
 82  
             // while(!succeeded && numberOfAttempts > 0)
 83  
             // {
 84  
             // try
 85  
             // {
 86  0
             if (identityFile != null)
 87  
             {
 88  0
                 String passphrase = sftpUtil.getPassphrase();
 89  
 
 90  0
                 client.login(endpointURI.getUser(), identityFile, passphrase);
 91  0
             }
 92  
             else
 93  
             {
 94  0
                 client.login(endpointURI.getUser(), endpointURI.getPassword());
 95  
             }
 96  
             // } catch (SocketException e)
 97  
             // {
 98  
             // numberOfAttempts--;
 99  
             // continue;
 100  
             // }
 101  
             // succeeded = true;
 102  
             // }
 103  
 
 104  0
             if (logger.isDebugEnabled())
 105  
             {
 106  0
                 logger.debug("Successfully connected to: " + endpointURI);
 107  
             }
 108  
 
 109  0
             return client;
 110  
         }
 111  0
         catch (IOException e)
 112  
         {
 113  0
             client.disconnect();
 114  0
             throw e;
 115  
         }
 116  
     }
 117  
 
 118  
     public void passivateObject(Object o) throws Exception
 119  
     {
 120  
         // Do nothing!
 121  0
     }
 122  
 
 123  
     public boolean validateObject(Object o)
 124  
     {
 125  0
         SftpClient client = (SftpClient) o;
 126  0
         if (logger.isDebugEnabled())
 127  
         {
 128  0
             logger.debug("Inside validateObject - will return " + client.isConnected());
 129  
         }
 130  0
         return client.isConnected();
 131  
     }
 132  
 }