KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > ftp > FTPClientWrapper


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

16 package org.apache.commons.vfs.provider.ftp;
17
18 import org.apache.commons.net.ftp.FTPClient;
19 import org.apache.commons.net.ftp.FTPFile;
20 import org.apache.commons.vfs.FileSystemException;
21 import org.apache.commons.vfs.FileSystemOptions;
22 import org.apache.commons.vfs.provider.GenericFileName;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27
28 /**
29  * A wrapper to the FTPClient to allow automatic reconnect on connection loss.<br />
30  * I decided to not to use eg. noop() to determine the state of the connection to avoid unnecesary server round-trips.
31  */

32 class FTPClientWrapper implements FtpClient
33 {
34     private final GenericFileName root;
35     private final FileSystemOptions fileSystemOptions;
36
37     private FTPClient ftpClient = null;
38     private String JavaDoc baseDir = null;
39
40     FTPClientWrapper(final GenericFileName root, final FileSystemOptions fileSystemOptions) throws FileSystemException
41     {
42         this.root = root;
43         this.fileSystemOptions = fileSystemOptions;
44         getFtpClient(); // fail-fast
45
}
46
47     public GenericFileName getRoot()
48     {
49         return root;
50     }
51
52     public FileSystemOptions getFileSystemOptions()
53     {
54         return fileSystemOptions;
55     }
56
57     private FTPClient createClient() throws FileSystemException
58     {
59         final GenericFileName rootName = getRoot();
60
61         return FtpClientFactory.createConnection(rootName.getHostName(),
62             rootName.getPort(),
63             rootName.getUserName(),
64             rootName.getPassword(),
65             rootName.getPath(),
66             getFileSystemOptions());
67     }
68
69     private FTPClient getFtpClient() throws FileSystemException
70     {
71         if (ftpClient == null)
72         {
73             ftpClient = createClient();
74         }
75
76         return ftpClient;
77     }
78
79     public boolean isConnected() throws FileSystemException
80     {
81         return getFtpClient().isConnected();
82     }
83
84     public void disconnect() throws IOException JavaDoc
85     {
86         try
87         {
88             getFtpClient().disconnect();
89         }
90         finally
91         {
92             ftpClient = null;
93         }
94     }
95
96     public FTPFile[] listFiles(String JavaDoc key, String JavaDoc relPath) throws IOException JavaDoc
97     {
98         try
99         {
100             FTPClient client = getFtpClient();
101             String JavaDoc dir = client.printWorkingDirectory();
102             client.changeWorkingDirectory(relPath);
103             FTPFile[] list = client.listFiles(key,null);
104             client.changeWorkingDirectory(dir);
105             return list;
106         }
107         catch (IOException JavaDoc e)
108         {
109             disconnect();
110             FTPClient client = getFtpClient();
111             String JavaDoc dir = client.printWorkingDirectory();
112             client.changeWorkingDirectory(relPath);
113             FTPFile[] list = client.listFiles(key,null);
114             client.changeWorkingDirectory(dir);
115             return list;
116         }
117     }
118     
119     
120
121     public boolean removeDirectory(String JavaDoc relPath) throws IOException JavaDoc
122     {
123         try
124         {
125             return getFtpClient().removeDirectory(relPath);
126         }
127         catch (IOException JavaDoc e)
128         {
129             disconnect();
130             return getFtpClient().removeDirectory(relPath);
131         }
132     }
133
134     public boolean deleteFile(String JavaDoc relPath) throws IOException JavaDoc
135     {
136         try
137         {
138             return getFtpClient().deleteFile(relPath);
139         }
140         catch (IOException JavaDoc e)
141         {
142             disconnect();
143             return getFtpClient().deleteFile(relPath);
144         }
145     }
146
147     public boolean rename(String JavaDoc oldName, String JavaDoc newName) throws IOException JavaDoc
148     {
149         try
150         {
151             return getFtpClient().rename(oldName, newName);
152         }
153         catch (IOException JavaDoc e)
154         {
155             disconnect();
156             return getFtpClient().rename(oldName, newName);
157         }
158     }
159
160     public boolean makeDirectory(String JavaDoc relPath) throws IOException JavaDoc
161     {
162         try
163         {
164             return getFtpClient().makeDirectory(relPath);
165         }
166         catch (IOException JavaDoc e)
167         {
168             disconnect();
169             return getFtpClient().makeDirectory(relPath);
170         }
171     }
172
173     public boolean completePendingCommand() throws IOException JavaDoc
174     {
175         if (ftpClient != null)
176         {
177             return getFtpClient().completePendingCommand();
178         }
179
180         return true;
181     }
182
183     public InputStream JavaDoc retrieveFileStream(String JavaDoc relPath) throws IOException JavaDoc
184     {
185         try
186         {
187             return getFtpClient().retrieveFileStream(relPath);
188         }
189         catch (IOException JavaDoc e)
190         {
191             disconnect();
192             return getFtpClient().retrieveFileStream(relPath);
193         }
194     }
195
196     public InputStream JavaDoc retrieveFileStream(String JavaDoc relPath, long restartOffset) throws IOException JavaDoc
197     {
198         try
199         {
200             FTPClient client = getFtpClient();
201             client.setRestartOffset(restartOffset);
202             return client.retrieveFileStream(relPath);
203         }
204         catch (IOException JavaDoc e)
205         {
206             disconnect();
207
208             FTPClient client = getFtpClient();
209             client.setRestartOffset(restartOffset);
210             return client.retrieveFileStream(relPath);
211         }
212     }
213
214     public OutputStream JavaDoc appendFileStream(String JavaDoc relPath) throws IOException JavaDoc
215     {
216         try
217         {
218             return getFtpClient().appendFileStream(relPath);
219         }
220         catch (IOException JavaDoc e)
221         {
222             disconnect();
223             return getFtpClient().appendFileStream(relPath);
224         }
225     }
226
227     public OutputStream JavaDoc storeFileStream(String JavaDoc relPath) throws IOException JavaDoc
228     {
229         try
230         {
231             return getFtpClient().storeFileStream(relPath);
232         }
233         catch (IOException JavaDoc e)
234         {
235             disconnect();
236             return getFtpClient().storeFileStream(relPath);
237         }
238     }
239
240     public boolean abort() throws IOException JavaDoc
241     {
242         try
243         {
244             // imario@apache.org: 2005-02-14
245
// it should be better to really "abort" the transfer, but
246
// currently I didnt manage to make it work - so lets "abort" the hard way.
247
// return getFtpClient().abort();
248

249             disconnect();
250             return true;
251         }
252         catch (IOException JavaDoc e)
253         {
254             disconnect();
255         }
256         return true;
257     }
258
259     public String JavaDoc getReplyString() throws IOException JavaDoc
260     {
261         return getFtpClient().getReplyString();
262     }
263 }
Popular Tags