1 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 ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 28 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 baseDir = null; 39 40 FTPClientWrapper(final GenericFileName root, final FileSystemOptions fileSystemOptions) throws FileSystemException 41 { 42 this.root = root; 43 this.fileSystemOptions = fileSystemOptions; 44 getFtpClient(); } 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 85 { 86 try 87 { 88 getFtpClient().disconnect(); 89 } 90 finally 91 { 92 ftpClient = null; 93 } 94 } 95 96 public FTPFile[] listFiles(String key, String relPath) throws IOException 97 { 98 try 99 { 100 FTPClient client = getFtpClient(); 101 String 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 e) 108 { 109 disconnect(); 110 FTPClient client = getFtpClient(); 111 String 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 relPath) throws IOException 122 { 123 try 124 { 125 return getFtpClient().removeDirectory(relPath); 126 } 127 catch (IOException e) 128 { 129 disconnect(); 130 return getFtpClient().removeDirectory(relPath); 131 } 132 } 133 134 public boolean deleteFile(String relPath) throws IOException 135 { 136 try 137 { 138 return getFtpClient().deleteFile(relPath); 139 } 140 catch (IOException e) 141 { 142 disconnect(); 143 return getFtpClient().deleteFile(relPath); 144 } 145 } 146 147 public boolean rename(String oldName, String newName) throws IOException 148 { 149 try 150 { 151 return getFtpClient().rename(oldName, newName); 152 } 153 catch (IOException e) 154 { 155 disconnect(); 156 return getFtpClient().rename(oldName, newName); 157 } 158 } 159 160 public boolean makeDirectory(String relPath) throws IOException 161 { 162 try 163 { 164 return getFtpClient().makeDirectory(relPath); 165 } 166 catch (IOException e) 167 { 168 disconnect(); 169 return getFtpClient().makeDirectory(relPath); 170 } 171 } 172 173 public boolean completePendingCommand() throws IOException 174 { 175 if (ftpClient != null) 176 { 177 return getFtpClient().completePendingCommand(); 178 } 179 180 return true; 181 } 182 183 public InputStream retrieveFileStream(String relPath) throws IOException 184 { 185 try 186 { 187 return getFtpClient().retrieveFileStream(relPath); 188 } 189 catch (IOException e) 190 { 191 disconnect(); 192 return getFtpClient().retrieveFileStream(relPath); 193 } 194 } 195 196 public InputStream retrieveFileStream(String relPath, long restartOffset) throws IOException 197 { 198 try 199 { 200 FTPClient client = getFtpClient(); 201 client.setRestartOffset(restartOffset); 202 return client.retrieveFileStream(relPath); 203 } 204 catch (IOException e) 205 { 206 disconnect(); 207 208 FTPClient client = getFtpClient(); 209 client.setRestartOffset(restartOffset); 210 return client.retrieveFileStream(relPath); 211 } 212 } 213 214 public OutputStream appendFileStream(String relPath) throws IOException 215 { 216 try 217 { 218 return getFtpClient().appendFileStream(relPath); 219 } 220 catch (IOException e) 221 { 222 disconnect(); 223 return getFtpClient().appendFileStream(relPath); 224 } 225 } 226 227 public OutputStream storeFileStream(String relPath) throws IOException 228 { 229 try 230 { 231 return getFtpClient().storeFileStream(relPath); 232 } 233 catch (IOException e) 234 { 235 disconnect(); 236 return getFtpClient().storeFileStream(relPath); 237 } 238 } 239 240 public boolean abort() throws IOException 241 { 242 try 243 { 244 249 disconnect(); 250 return true; 251 } 252 catch (IOException e) 253 { 254 disconnect(); 255 } 256 return true; 257 } 258 259 public String getReplyString() throws IOException 260 { 261 return getFtpClient().getReplyString(); 262 } 263 } | Popular Tags |