1
2
3
4
5
6
7
8
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 if (uri.getPort() > 0)
35 {
36 client.connect(uri.getHost(), uri.getPort());
37 }
38 else
39 {
40 client.connect(uri.getHost());
41 }
42 if (!FTPReply.isPositiveCompletion(client.getReplyCode()))
43 {
44 throw new IOException("Ftp connect failed: " + client.getReplyCode());
45 }
46 if (!client.login(uri.getUser(), uri.getPassword()))
47 {
48 throw new IOException("Ftp login failed: " + client.getReplyCode());
49 }
50 if (!client.setFileType(FTP.BINARY_FILE_TYPE))
51 {
52 throw new IOException("Ftp error. Couldn't set BINARY transfer type: " + client.getReplyCode());
53 }
54 return client;
55 }
56
57 public void destroyObject(Object obj) throws Exception
58 {
59 FTPClient client = (FTPClient) obj;
60 client.logout();
61 client.disconnect();
62 }
63
64 public boolean validateObject(Object obj)
65 {
66 FTPClient client = (FTPClient) obj;
67 try
68 {
69 return client.sendNoOp();
70 }
71 catch (IOException e)
72 {
73 return false;
74 }
75 }
76
77 public void activateObject(Object obj) throws Exception
78 {
79
80 }
81
82 public void passivateObject(Object obj) throws Exception
83 {
84
85 }
86 }
87