1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.provider.ftp;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.commons.vfs.FileName;
22 import org.apache.commons.vfs.FileObject;
23 import org.apache.commons.vfs.FileSystemException;
24 import org.apache.commons.vfs.FileSystemOptions;
25 import org.apache.commons.vfs.VfsLog;
26 import org.apache.commons.vfs.provider.AbstractFileSystem;
27 import org.apache.commons.vfs.provider.GenericFileName;
28
29 import java.io.IOException;
30 import java.util.Collection;
31
32 /***
33 * An FTP file system.
34 *
35 * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
36 * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
37 */
38 public class FtpFileSystem
39 extends AbstractFileSystem
40 {
41 private final static Log log = LogFactory.getLog(FtpFileSystem.class);
42
43
44
45
46
47
48
49 private FtpClient idleClient;
50 private final Object idleClientSync = new Object();
51
52 protected FtpFileSystem(final GenericFileName rootName, final FtpClient ftpClient, final FileSystemOptions fileSystemOptions)
53 {
54 super(rootName, null, fileSystemOptions);
55
56
57
58 idleClient = ftpClient;
59 }
60
61 protected void doCloseCommunicationLink()
62 {
63
64 if (idleClient != null)
65 {
66 closeConnection(idleClient);
67 idleClient = null;
68 }
69 }
70
71 /***
72 * Adds the capabilities of this file system.
73 */
74 protected void addCapabilities(final Collection caps)
75 {
76 caps.addAll(FtpFileProvider.capabilities);
77 }
78
79 /***
80 * Cleans up the connection to the server.
81 */
82 private void closeConnection(final FtpClient client)
83 {
84 try
85 {
86
87 if (client.isConnected())
88 {
89 client.disconnect();
90 }
91 }
92 catch (final IOException e)
93 {
94
95 VfsLog.warn(getLogger(), log, "vfs.provider.ftp/close-connection.error", e);
96 }
97 }
98
99 /***
100 * Creates an FTP client to use.
101 */
102 public FtpClient getClient() throws FileSystemException
103 {
104 synchronized (idleClientSync)
105 {
106 if (idleClient == null || !idleClient.isConnected())
107 {
108 FtpClient ftpClient = new FTPClientWrapper((GenericFileName) getRoot().getName(), getFileSystemOptions());
109 return ftpClient;
110
111
112
113
114
115
116
117
118
119
120 }
121 else
122 {
123 final FtpClient client = idleClient;
124 idleClient = null;
125 return client;
126 }
127 }
128 }
129
130 /***
131 * Returns an FTP client after use.
132 */
133 public void putClient(final FtpClient client)
134 {
135 synchronized (idleClientSync)
136 {
137 if (idleClient == null)
138 {
139
140 idleClient = client;
141 }
142 else
143 {
144
145 closeConnection(client);
146 }
147 }
148 }
149
150 /***
151 * Creates a file object.
152 */
153 protected FileObject createFile(final FileName name)
154 throws FileSystemException
155 {
156 return new FtpFileObject(name, this, getRootName());
157 }
158 }