Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: SftpConnectionFactory.java 21125 2011-01-26 21:21:10Z dzapata $
   * --------------------------------------------------------------------------------------
   * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
   *
   * The software in this package is published under the terms of the CPAL v1.0
   * license, a copy of which has been included with this distribution in the
   * LICENSE.txt file.
   */
 
 package org.mule.transport.sftp;
 
 
 
 public class SftpConnectionFactory implements PoolableObjectFactory
 {
     private final static Logger logger = Logger.getLogger(SftpConnectionFactory.class);
 
     private final ImmutableEndpoint endpoint;
 
     public SftpConnectionFactory(ImmutableEndpoint endpoint)
     {
         this. = endpoint;
     }
 
     public void activateObject(Object othrows Exception
     {
         // Do nothing!
     }
 
     public void destroyObject(Object othrows Exception
     {
         SftpClient client = (SftpCliento;
         client.disconnect();
     }
 
     public Object makeObject() throws Exception
     {
         return createClient();
     }
 
     public static SftpClient createClient(ImmutableEndpoint endpointthrows Exception
     {
         EndpointURI endpointURI = endpoint.getEndpointURI();
 
         String host = endpointURI.getHost();
         if (.isDebugEnabled())
         {
             .debug("Using host: " + host);
         }
 
         SftpClient client = new SftpClient(host);
 
         try
         {
             int uriPort = endpointURI.getPort();
             if (uriPort != -1)
             {
                 if (.isDebugEnabled())
                 {
                     .debug("Using port: " + uriPort);
                 }
                 client.setPort(uriPort);
             }
 
             SftpUtil sftpUtil = new SftpUtil(endpoint);
             String identityFile = sftpUtil.getIdentityFile();
 
             /*
              * TODO: There is a problem if the SSHd uses a low value of
              * "MaxStartups", which means that if there is many new concurrent
              * connections the server will respond with "Connection reset", and thus
              * we will get exceptions of type
              * "Session.connect: java.net.SocketException: Connection reset"...
              * Solutions: increase the MaxStartups on the server or fix a retry or
              * use a exception-strategy(?)
              */
 
             // boolean succeeded = false;
             // int numberOfAttempts = 2;
             // while(!succeeded && numberOfAttempts > 0)
             // {
             // try
             // {
             if (identityFile != null)
             {
                 String passphrase = sftpUtil.getPassphrase();
 
                 client.login(endpointURI.getUser(), identityFilepassphrase);
             }
             else
             {
                 client.login(endpointURI.getUser(), endpointURI.getPassword());
             }
            // } catch (SocketException e)
            // {
            // numberOfAttempts--;
            // continue;
            // }
            // succeeded = true;
            // }
            if (.isDebugEnabled())
            {
                .debug("Successfully connected to: " + endpointURI);
            }
            return client;
        }
        catch (IOException e)
        {
            client.disconnect();
            throw e;
        }
    }
    public void passivateObject(Object othrows Exception
    {
        // Do nothing!
    }
    public boolean validateObject(Object o)
    {
        SftpClient client = (SftpCliento;
        if (.isDebugEnabled())
        {
            .debug("Inside validateObject - will return " + client.isConnected());
        }
        return client.isConnected();
    }
New to GrepCode? Check out our FAQ X