View Javadoc

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.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  //    private final String hostname;
44  //    private final int port;
45  //    private final String username;
46  //    private final String password;
47  
48      // An idle client
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          // hostname = rootName.getHostName();
56          // port = rootName.getPort();
57  
58          idleClient = ftpClient;
59      }
60  
61      protected void doCloseCommunicationLink()
62      {
63          // Clean up the connection
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              // Clean up
87              if (client.isConnected())
88              {
89                  client.disconnect();
90              }
91          }
92          catch (final IOException e)
93          {
94              // getLogger().warn("vfs.provider.ftp/close-connection.error", e);
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                     final GenericFileName rootName = (GenericFileName) getRoot().getName();
112 
113                     return FtpClientFactory.createConnection(rootName.getHostName(),
114                         rootName.getPort(),
115                         rootName.getUserName(),
116                         rootName.getPassword(),
117                         rootName.getPath(),
118                         getFileSystemOptions());
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                     // Hang on to client for later
140                     idleClient = client;
141                 }
142                 else
143                 {
144                     // Close the client
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 }