1
2
3
4
5
6
7
8
9
10
11 package org.mule.providers.ftp;
12
13 import org.mule.umo.endpoint.UMOEndpointURI;
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 UMOEndpointURI uri;
25
26 public FtpConnectionFactory(UMOEndpointURI 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.getUsername(), 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