View Javadoc

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      {
28          this.uri = uri;
29      }
30  
31      public Object makeObject() throws Exception
32      {
33          FTPClient client = new FTPClient();
34          try
35          {
36              if (uri.getPort() > 0)
37              {
38                  client.connect(uri.getHost(), uri.getPort());
39              }
40              else
41              {
42                  client.connect(uri.getHost());
43              }
44              if (!FTPReply.isPositiveCompletion(client.getReplyCode()))
45              {
46                  throw new IOException("Ftp error: " + client.getReplyCode());
47              }
48              if (!client.login(uri.getUser(), uri.getPassword()))
49              {
50                  throw new IOException("Ftp error: " + client.getReplyCode());
51              }
52              if (!client.setFileType(FTP.BINARY_FILE_TYPE))
53              {
54                  throw new IOException("Ftp error. Couldn't set BINARY transfer type.");
55              }
56          }
57          catch (Exception e)
58          {
59              if (client.isConnected())
60              {
61                  client.disconnect();
62              }
63              throw e;
64          }
65          return client;
66      }
67  
68      public void destroyObject(Object obj) throws Exception
69      {
70          FTPClient client = (FTPClient) obj;
71          client.logout();
72          client.disconnect();
73      }
74  
75      public boolean validateObject(Object obj)
76      {
77          FTPClient client = (FTPClient) obj;
78          try
79          {
80              client.sendNoOp();
81              return true;
82          }
83          catch (IOException e)
84          {
85              return false;
86          }
87      }
88  
89      public void activateObject(Object obj) throws Exception
90      {
91          FTPClient client = (FTPClient) obj;
92          client.setReaderThread(true);
93      }
94  
95      public void passivateObject(Object obj) throws Exception
96      {
97          FTPClient client = (FTPClient) obj;
98          client.setReaderThread(false);
99      }
100 }
101