001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.ftp;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.apache.commons.vfs.FileName;
022: import org.apache.commons.vfs.FileObject;
023: import org.apache.commons.vfs.FileSystemException;
024: import org.apache.commons.vfs.FileSystemOptions;
025: import org.apache.commons.vfs.VfsLog;
026: import org.apache.commons.vfs.provider.AbstractFileSystem;
027: import org.apache.commons.vfs.provider.GenericFileName;
028:
029: import java.io.IOException;
030: import java.util.Collection;
031:
032: /**
033: * An FTP file system.
034: *
035: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
036: * @version $Revision: 548639 $ $Date: 2007-06-19 00:58:51 -0700 (Tue, 19 Jun 2007) $
037: */
038: public class FtpFileSystem extends AbstractFileSystem {
039: private final static Log log = LogFactory
040: .getLog(FtpFileSystem.class);
041:
042: // private final String hostname;
043: // private final int port;
044: // private final String username;
045: // private final String password;
046:
047: // An idle client
048: private FtpClient idleClient;
049:
050: protected FtpFileSystem(final GenericFileName rootName,
051: final FtpClient ftpClient,
052: final FileSystemOptions fileSystemOptions) {
053: super (rootName, null, fileSystemOptions);
054: // hostname = rootName.getHostName();
055: // port = rootName.getPort();
056:
057: idleClient = ftpClient;
058: }
059:
060: protected void doCloseCommunicationLink() {
061: // Clean up the connection
062: if (idleClient != null) {
063: closeConnection(idleClient);
064: idleClient = null;
065: }
066: }
067:
068: /**
069: * Adds the capabilities of this file system.
070: */
071: protected void addCapabilities(final Collection caps) {
072: caps.addAll(FtpFileProvider.capabilities);
073: }
074:
075: /**
076: * Cleans up the connection to the server.
077: */
078: private void closeConnection(final FtpClient client) {
079: try {
080: // Clean up
081: if (client.isConnected()) {
082: client.disconnect();
083: }
084: } catch (final IOException e) {
085: // getLogger().warn("vfs.provider.ftp/close-connection.error", e);
086: VfsLog.warn(getLogger(), log,
087: "vfs.provider.ftp/close-connection.error", e);
088: }
089: }
090:
091: /**
092: * Creates an FTP client to use.
093: */
094: public FtpClient getClient() throws FileSystemException {
095: synchronized (this ) {
096: if (idleClient == null || !idleClient.isConnected()) {
097: idleClient = null;
098:
099: FtpClient ftpClient = new FTPClientWrapper(
100: (GenericFileName) getRoot().getName(),
101: getFileSystemOptions());
102: return ftpClient;
103: /*
104: final GenericFileName rootName = (GenericFileName) getRoot().getName();
105:
106: return FtpClientFactory.createConnection(rootName.getHostName(),
107: rootName.getPort(),
108: rootName.getUserName(),
109: rootName.getPassword(),
110: rootName.getPath(),
111: getFileSystemOptions());
112: */
113: } else {
114: final FtpClient client = idleClient;
115: idleClient = null;
116: return client;
117: }
118: }
119: }
120:
121: /**
122: * Returns an FTP client after use.
123: */
124: public void putClient(final FtpClient client) {
125: synchronized (this ) {
126: if (idleClient == null) {
127: // Hang on to client for later
128: idleClient = client;
129: } else {
130: // Close the client
131: closeConnection(client);
132: }
133: }
134: }
135:
136: /**
137: * Creates a file object.
138: */
139: protected FileObject createFile(final FileName name)
140: throws FileSystemException {
141: return new FtpFileObject(name, this, getRootName());
142: }
143: }
|