KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > net > FTPClientPool


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
26
27 /**
28  * A pool of FTP clients for
29  * the <a HREF="http://jakarta.apache.org/commons/net.html">Jakarta Commons Net</a> library
30  *
31  * @version $Revision: 426415 $
32  */

33 public class FTPClientPool extends SocketClientPoolSupport {
34
35     private String JavaDoc username;
36     private String JavaDoc password;
37     private boolean binaryMode = true;
38     private boolean passiveMode = false;
39     private FTPClientConfig config;
40
41     public boolean validateObject(Object JavaDoc object) {
42         FTPClient client = (FTPClient) object;
43         try {
44             return client.sendNoOp();
45         }
46         catch (IOException JavaDoc e) {
47             throw new RuntimeException JavaDoc("Failed to validate client: " + e, e);
48         }
49     }
50
51     public void activateObject(Object JavaDoc object) throws Exception JavaDoc {
52         FTPClient client = (FTPClient) object;
53         client.setReaderThread(true);
54     }
55
56     public void passivateObject(Object JavaDoc object) throws Exception JavaDoc {
57         FTPClient client = (FTPClient) object;
58         client.setReaderThread(false);
59     }
60
61     // Properties
62
//-------------------------------------------------------------------------
63
public String JavaDoc getUsername() {
64         return username;
65     }
66
67     public void setUsername(String JavaDoc username) {
68         this.username = username;
69     }
70
71     public String JavaDoc getPassword() {
72         return password;
73     }
74
75     public void setPassword(String JavaDoc 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     /**
88      * @return the passiveMode
89      */

90     public boolean isPassiveMode() {
91         return passiveMode;
92     }
93
94     /**
95      * @param passiveMode the passiveMode to set
96      */

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     // Implementation methods
110
//-------------------------------------------------------------------------
111
protected void connect(SocketClient client) throws Exception JavaDoc {
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 JavaDoc {
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