KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
29 import java.util.Collection JavaDoc;
30
31 /**
32  * An FTP file system.
33  *
34  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
35  */

36 public class FtpFileSystem
37     extends AbstractFileSystem
38 {
39     private final static Log log = LogFactory.getLog(FtpFileSystem.class);
40
41 // private final String hostname;
42
// private final int port;
43
// private final String username;
44
// private final String password;
45

46     // An idle client
47
private FtpClient idleClient;
48     private final Object JavaDoc idleClientSync = new Object JavaDoc();
49
50     protected FtpFileSystem(final GenericFileName rootName, final FtpClient ftpClient, final FileSystemOptions fileSystemOptions)
51     {
52         super(rootName, null, fileSystemOptions);
53         // hostname = rootName.getHostName();
54
// port = rootName.getPort();
55

56         idleClient = ftpClient;
57     }
58
59     protected void doCloseCommunicationLink()
60     {
61         // Clean up the connection
62
if (idleClient != null)
63         {
64             closeConnection(idleClient);
65             idleClient = null;
66         }
67     }
68
69     /**
70      * Adds the capabilities of this file system.
71      */

72     protected void addCapabilities(final Collection JavaDoc caps)
73     {
74         caps.addAll(FtpFileProvider.capabilities);
75     }
76
77     /**
78      * Cleans up the connection to the server.
79      */

80     private void closeConnection(final FtpClient client)
81     {
82         try
83         {
84             // Clean up
85
if (client.isConnected())
86             {
87                 client.disconnect();
88             }
89         }
90         catch (final IOException JavaDoc e)
91         {
92             // getLogger().warn("vfs.provider.ftp/close-connection.error", e);
93
VfsLog.warn(getLogger(), log, "vfs.provider.ftp/close-connection.error", e);
94         }
95     }
96
97     /**
98      * Creates an FTP client to use.
99      */

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                     /*
109                     final GenericFileName rootName = (GenericFileName) getRoot().getName();
110
111                     return FtpClientFactory.createConnection(rootName.getHostName(),
112                         rootName.getPort(),
113                         rootName.getUserName(),
114                         rootName.getPassword(),
115                         rootName.getPath(),
116                         getFileSystemOptions());
117                     */

118                 }
119                 else
120                 {
121                     final FtpClient client = idleClient;
122                     idleClient = null;
123                     return client;
124                 }
125             }
126     }
127
128     /**
129      * Returns an FTP client after use.
130      */

131     public void putClient(final FtpClient client)
132     {
133         synchronized (idleClientSync)
134             {
135                 if (idleClient == null)
136                 {
137                     // Hang on to client for later
138
idleClient = client;
139                 }
140                 else
141                 {
142                     // Close the client
143
closeConnection(client);
144                 }
145             }
146     }
147
148     /**
149      * Creates a file object.
150      */

151     protected FileObject createFile(final FileName name)
152         throws FileSystemException
153     {
154         return new FtpFileObject(name, this, getRootName());
155     }
156 }
157
Popular Tags