1 17 package org.apache.servicemix.components.net; 18 19 import org.apache.commons.net.SocketClient; 20 import org.apache.commons.net.ftp.FTP; 21 import org.apache.commons.net.ftp.FTPClient; 22 import org.apache.commons.net.ftp.FTPReply; 23 import org.apache.commons.net.ftp.FTPClientConfig; 24 25 import java.io.IOException ; 26 27 33 public class FTPClientPool extends SocketClientPoolSupport { 34 35 private String username; 36 private String password; 37 private boolean binaryMode = true; 38 private boolean passiveMode = false; 39 private FTPClientConfig config; 40 41 public boolean validateObject(Object object) { 42 FTPClient client = (FTPClient) object; 43 try { 44 return client.sendNoOp(); 45 } 46 catch (IOException e) { 47 throw new RuntimeException ("Failed to validate client: " + e, e); 48 } 49 } 50 51 public void activateObject(Object object) throws Exception { 52 FTPClient client = (FTPClient) object; 53 client.setReaderThread(true); 54 } 55 56 public void passivateObject(Object object) throws Exception { 57 FTPClient client = (FTPClient) object; 58 client.setReaderThread(false); 59 } 60 61 public String getUsername() { 64 return username; 65 } 66 67 public void setUsername(String username) { 68 this.username = username; 69 } 70 71 public String getPassword() { 72 return password; 73 } 74 75 public void setPassword(String password) { 76 this.password = password; 77 } 78 79 public boolean isBinaryMode() { 80 return binaryMode; 81 } 82 83 public void setBinaryMode(boolean binaryMode) { 84 this.binaryMode = binaryMode; 85 } 86 87 90 public boolean isPassiveMode() { 91 return passiveMode; 92 } 93 94 97 public void setPassiveMode(boolean passiveMode) { 98 this.passiveMode = passiveMode; 99 } 100 101 public FTPClientConfig getConfig() { 102 return config; 103 } 104 105 public void setConfig(FTPClientConfig config) { 106 this.config = config; 107 } 108 109 protected void connect(SocketClient client) throws Exception { 112 FTPClient ftp = (FTPClient) client; 113 114 if (config != null) { 115 ftp.configure(config); 116 } 117 super.connect(client); 118 119 int code = ftp.getReplyCode(); 120 if (!FTPReply.isPositiveCompletion(code)) { 121 ftp.disconnect(); 122 throw new ConnectionRefusedException(code); 123 } 124 if (!ftp.login(getUsername(), getPassword())) { 125 ftp.disconnect(); 126 throw new ConnectionRefusedException(ftp.getReplyCode()); 127 } 128 if (isBinaryMode()) { 129 ftp.setFileType(FTP.BINARY_FILE_TYPE); 130 } 131 if (isPassiveMode()) { 132 ftp.enterLocalPassiveMode(); 133 } 134 } 135 136 protected void disconnect(SocketClient client) throws Exception { 137 FTPClient ftp = (FTPClient) client; 138 if (ftp.isConnected()) { 139 ftp.logout(); 140 } 141 super.disconnect(client); 142 } 143 144 protected SocketClient createSocketClient() { 145 return new FTPClient(); 146 } 147 } 148 | Popular Tags |