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