01: /*
02: * $Id: FtpConnectionFactory.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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: private EndpointURI uri;
24:
25: public FtpConnectionFactory(EndpointURI uri) {
26: this .uri = uri;
27: }
28:
29: public Object makeObject() throws Exception {
30: FTPClient client = new FTPClient();
31: try {
32: if (uri.getPort() > 0) {
33: client.connect(uri.getHost(), uri.getPort());
34: } else {
35: client.connect(uri.getHost());
36: }
37: if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
38: throw new IOException("Ftp error: "
39: + client.getReplyCode());
40: }
41: if (!client.login(uri.getUser(), uri.getPassword())) {
42: throw new IOException("Ftp error: "
43: + client.getReplyCode());
44: }
45: if (!client.setFileType(FTP.BINARY_FILE_TYPE)) {
46: throw new IOException(
47: "Ftp error. Couldn't set BINARY transfer type.");
48: }
49: } catch (Exception e) {
50: if (client.isConnected()) {
51: client.disconnect();
52: }
53: throw e;
54: }
55: return client;
56: }
57:
58: public void destroyObject(Object obj) throws Exception {
59: FTPClient client = (FTPClient) obj;
60: client.logout();
61: client.disconnect();
62: }
63:
64: public boolean validateObject(Object obj) {
65: FTPClient client = (FTPClient) obj;
66: try {
67: client.sendNoOp();
68: return true;
69: } catch (IOException e) {
70: return false;
71: }
72: }
73:
74: public void activateObject(Object obj) throws Exception {
75: FTPClient client = (FTPClient) obj;
76: client.setReaderThread(true);
77: }
78:
79: public void passivateObject(Object obj) throws Exception {
80: FTPClient client = (FTPClient) obj;
81: client.setReaderThread(false);
82: }
83: }
|