View Javadoc

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