View Javadoc
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      private final static Logger logger = Logger.getLogger(SftpConnectionFactory.class);
19  
20      private final ImmutableEndpoint endpoint;
21  
22      public SftpConnectionFactory(ImmutableEndpoint endpoint)
23      {
24          this.endpoint = endpoint;
25      }
26  
27      public void activateObject(Object o) throws Exception
28      {
29          // Do nothing!
30      }
31  
32      public void destroyObject(Object o) throws Exception
33      {
34          SftpClient client = (SftpClient) o;
35          client.disconnect();
36      }
37  
38      public Object makeObject() throws Exception
39      {
40          return createClient(endpoint);
41      }
42  
43      public static SftpClient createClient(ImmutableEndpoint endpoint) throws Exception
44      {
45          EndpointURI endpointURI = endpoint.getEndpointURI();
46  
47          String host = endpointURI.getHost();
48          if (logger.isDebugEnabled())
49          {
50              logger.debug("Using host: " + host);
51          }
52  
53          SftpClient client = new SftpClient(host);
54  
55          try
56          {
57              int uriPort = endpointURI.getPort();
58              if (uriPort != -1)
59              {
60                  if (logger.isDebugEnabled())
61                  {
62                      logger.debug("Using port: " + uriPort);
63                  }
64                  client.setPort(uriPort);
65              }
66  
67              SftpUtil sftpUtil = new SftpUtil(endpoint);
68              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              if (identityFile != null)
87              {
88                  String passphrase = sftpUtil.getPassphrase();
89  
90                  client.login(endpointURI.getUser(), identityFile, passphrase);
91              }
92              else
93              {
94                  client.login(endpointURI.getUser(), endpointURI.getPassword());
95              }
96              // } catch (SocketException e)
97              // {
98              // numberOfAttempts--;
99              // continue;
100             // }
101             // succeeded = true;
102             // }
103 
104             if (logger.isDebugEnabled())
105             {
106                 logger.debug("Successfully connected to: " + endpointURI);
107             }
108 
109             return client;
110         }
111         catch (IOException e)
112         {
113             client.disconnect();
114             throw e;
115         }
116     }
117 
118     public void passivateObject(Object o) throws Exception
119     {
120         // Do nothing!
121     }
122 
123     public boolean validateObject(Object o)
124     {
125         SftpClient client = (SftpClient) o;
126         if (logger.isDebugEnabled())
127         {
128             logger.debug("Inside validateObject - will return " + client.isConnected());
129         }
130         return client.isConnected();
131     }
132 }