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