1 16 package org.apache.commons.vfs.provider.ftp; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.apache.commons.vfs.FileName; 21 import org.apache.commons.vfs.FileObject; 22 import org.apache.commons.vfs.FileSystemException; 23 import org.apache.commons.vfs.FileSystemOptions; 24 import org.apache.commons.vfs.VfsLog; 25 import org.apache.commons.vfs.provider.AbstractFileSystem; 26 import org.apache.commons.vfs.provider.GenericFileName; 27 28 import java.io.IOException ; 29 import java.util.Collection ; 30 31 36 public class FtpFileSystem 37 extends AbstractFileSystem 38 { 39 private final static Log log = LogFactory.getLog(FtpFileSystem.class); 40 41 46 private FtpClient idleClient; 48 private final Object idleClientSync = new Object (); 49 50 protected FtpFileSystem(final GenericFileName rootName, final FtpClient ftpClient, final FileSystemOptions fileSystemOptions) 51 { 52 super(rootName, null, fileSystemOptions); 53 56 idleClient = ftpClient; 57 } 58 59 protected void doCloseCommunicationLink() 60 { 61 if (idleClient != null) 63 { 64 closeConnection(idleClient); 65 idleClient = null; 66 } 67 } 68 69 72 protected void addCapabilities(final Collection caps) 73 { 74 caps.addAll(FtpFileProvider.capabilities); 75 } 76 77 80 private void closeConnection(final FtpClient client) 81 { 82 try 83 { 84 if (client.isConnected()) 86 { 87 client.disconnect(); 88 } 89 } 90 catch (final IOException e) 91 { 92 VfsLog.warn(getLogger(), log, "vfs.provider.ftp/close-connection.error", e); 94 } 95 } 96 97 100 public FtpClient getClient() throws FileSystemException 101 { 102 synchronized (idleClientSync) 103 { 104 if (idleClient == null || !idleClient.isConnected()) 105 { 106 FtpClient ftpClient = new FTPClientWrapper((GenericFileName) getRoot().getName(), getFileSystemOptions()); 107 return ftpClient; 108 118 } 119 else 120 { 121 final FtpClient client = idleClient; 122 idleClient = null; 123 return client; 124 } 125 } 126 } 127 128 131 public void putClient(final FtpClient client) 132 { 133 synchronized (idleClientSync) 134 { 135 if (idleClient == null) 136 { 137 idleClient = client; 139 } 140 else 141 { 142 closeConnection(client); 144 } 145 } 146 } 147 148 151 protected FileObject createFile(final FileName name) 152 throws FileSystemException 153 { 154 return new FtpFileObject(name, this, getRootName()); 155 } 156 } 157 | Popular Tags |