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 */ 017package org.apache.commons.vfs2.provider.ftp; 018 019import java.io.IOException; 020import java.util.Collection; 021import java.util.concurrent.atomic.AtomicReference; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.apache.commons.vfs2.Capability; 026import org.apache.commons.vfs2.FileObject; 027import org.apache.commons.vfs2.FileSystemException; 028import org.apache.commons.vfs2.FileSystemOptions; 029import org.apache.commons.vfs2.VfsLog; 030import org.apache.commons.vfs2.provider.AbstractFileName; 031import org.apache.commons.vfs2.provider.AbstractFileSystem; 032import org.apache.commons.vfs2.provider.GenericFileName; 033 034/** 035 * An FTP file system. 036 */ 037public class FtpFileSystem extends AbstractFileSystem { 038 private static final Log LOG = LogFactory.getLog(FtpFileSystem.class); 039 040 // private final String hostname; 041 // private final int port; 042 // private final String username; 043 // private final String password; 044 045 // An idle client 046 private final AtomicReference<FtpClient> idleClient = new AtomicReference<>(); 047 048 /** 049 * @param rootName The root of the file system. 050 * @param ftpClient The FtpClient. 051 * @param fileSystemOptions The FileSystemOptions. 052 * @since 2.0 (was protected) 053 */ 054 public FtpFileSystem(final GenericFileName rootName, final FtpClient ftpClient, 055 final FileSystemOptions fileSystemOptions) { 056 super(rootName, null, fileSystemOptions); 057 // hostname = rootName.getHostName(); 058 // port = rootName.getPort(); 059 060 idleClient.set(ftpClient); 061 } 062 063 @Override 064 protected void doCloseCommunicationLink() { 065 final FtpClient idle = idleClient.getAndSet(null); 066 // Clean up the connection 067 if (idle != null) { 068 closeConnection(idle); 069 } 070 } 071 072 /** 073 * Adds the capabilities of this file system. 074 */ 075 @Override 076 protected void addCapabilities(final Collection<Capability> caps) { 077 caps.addAll(FtpFileProvider.capabilities); 078 } 079 080 /** 081 * Cleans up the connection to the server. 082 * 083 * @param client The FtpClient. 084 */ 085 private void closeConnection(final FtpClient client) { 086 try { 087 // Clean up 088 if (client.isConnected()) { 089 client.disconnect(); 090 } 091 } catch (final IOException e) { 092 // getLogger().warn("vfs.provider.ftp/close-connection.error", e); 093 VfsLog.warn(getLogger(), LOG, "vfs.provider.ftp/close-connection.error", e); 094 } 095 } 096 097 /** 098 * Creates an FTP client to use. 099 * 100 * @return An FTPCleint. 101 * @throws FileSystemException if an error occurs. 102 */ 103 public FtpClient getClient() throws FileSystemException { 104 FtpClient client = idleClient.getAndSet(null); 105 106 if (client == null || !client.isConnected()) { 107 client = createWrapper(); 108 } 109 110 return client; 111 } 112 113 /** 114 * Get the wrapper to access this file system. 115 * 116 * @return new instance. 117 * @throws FileSystemException if any error occurs. 118 * @since 2.1 119 */ 120 protected FTPClientWrapper createWrapper() throws FileSystemException { 121 return new FTPClientWrapper((GenericFileName) getRoot().getName(), getFileSystemOptions()); 122 } 123 124 /** 125 * Returns an FTP client after use. 126 * 127 * @param client The FTPClient. 128 */ 129 public void putClient(final FtpClient client) { 130 // Save client for reuse if none is idle. 131 if (!idleClient.compareAndSet(null, client)) { 132 // An idle client is already present so close the connection. 133 closeConnection(client); 134 } 135 } 136 137 /** 138 * Creates a file object. 139 */ 140 @Override 141 protected FileObject createFile(final AbstractFileName name) throws FileSystemException { 142 return new FtpFileObject(name, this, getRootName()); 143 } 144}