1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-20 06:35:50 +02:00

Use Mutex to avoid parallel FTP directory queries

This commit is contained in:
Martin Oberhuber 2006-08-17 10:21:06 +00:00
parent ec66da7bf4
commit 003e578773

View file

@ -36,6 +36,7 @@ import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.rse.services.Mutex;
import org.eclipse.rse.services.clientserver.NamePatternMatcher;
import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
import org.eclipse.rse.services.files.AbstractFileService;
@ -50,6 +51,9 @@ import sun.net.ftp.FtpClient;
public class FTPService extends AbstractFileService implements IFileService, IFTPService
{
private FTPClientService _ftpClient;
private Mutex _mutex = new Mutex();
private long _mutexTimeout = 5000; //max.5 seconds to obtain dir channel
private String _userHome;
private IFTPDirectoryListingParser _ftpPropertiesUtil;
@ -151,6 +155,7 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
public IHostFile getFile(IProgressMonitor monitor, String remoteParent, String fileName)
{
//No Mutex lock needed here because internalFetch() does the lock
IHostFile[] matches = internalFetch(monitor, remoteParent, fileName, FILE_TYPE_FILES_AND_FOLDERS);
if (matches != null && matches.length > 0)
{
@ -174,6 +179,8 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
}
NamePatternMatcher filematcher = new NamePatternMatcher(fileFilter, true, true);
List results = new ArrayList();
if (_mutex.waitForLock(monitor, _mutexTimeout))
{
try
{
FtpClient ftp = getFTPClient();
@ -212,6 +219,11 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
{
e.printStackTrace();
}
finally
{
_mutex.release();
}
}
return (IHostFile[])results.toArray(new IHostFile[results.size()]);
}
@ -271,9 +283,11 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
public boolean download(IProgressMonitor monitor, String remoteParent, String remoteFile, File localFile, boolean isBinary, String hostEncoding)
{
FtpClient ftp = getFTPClient();
if (_mutex.waitForLock(monitor, _mutexTimeout))
{
try
{
FtpClient ftp = getFTPClient();
ftp.cd(remoteParent);
/*
if (isBinary)
@ -308,13 +322,18 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
totalWrote += readCount;
}
bos.close();
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
return true;
finally
{
_mutex.release();
}
}
return false;
}
public IHostFile getUserHome()
@ -336,14 +355,20 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
*/
public boolean delete(IProgressMonitor monitor, String remoteParent, String fileName) {
boolean hasSucceeded = false;
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
try {
getFTPClient().cd(remoteParent);
int returnedValue = getFTPClient().sendCommand("DELE " + fileName);
hasSucceeded = (returnedValue > 0);
} catch (IOException e) {
}
catch (IOException e) {
// Changing folder raised an exception
hasSucceeded = false;
}
finally {
_mutex.release();
}
}
return hasSucceeded;
}
@ -352,11 +377,18 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
*/
public boolean rename(IProgressMonitor monitor, String remoteParent, String oldName, String newName) {
boolean hasSucceeded = false;
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
try {
int returnedValue = getFTPClient().sendCommand("RNFR " + remoteParent + getSeparator() + oldName);
if (returnedValue > 0) {
returnedValue = getFTPClient().sendCommand("RNTO " + remoteParent + getSeparator() + newName);
hasSucceeded = (returnedValue > 0);
}
}
finally {
_mutex.release();
}
}
return hasSucceeded;
}
@ -365,11 +397,18 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
*/
public boolean rename(IProgressMonitor monitor, String remoteParent, String oldName, String newName, IHostFile oldFile) {
boolean hasSucceeded = false;
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
try {
int returnedValue = getFTPClient().sendCommand("RNFR " + remoteParent + getSeparator() + oldName);
if (returnedValue > 0) {
returnedValue = getFTPClient().sendCommand("RNTO " + remoteParent + getSeparator() + newName);
hasSucceeded = (returnedValue > 0);
}
}
finally {
_mutex.release();
}
}
return hasSucceeded;
}
@ -378,11 +417,18 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
*/
public boolean move(IProgressMonitor monitor, String srcParent, String srcName, String tgtParent, String tgtName) {
boolean hasSucceeded = false;
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
try {
int returnedValue = getFTPClient().sendCommand("RNFR " + srcParent + getSeparator() + srcName);
if (returnedValue > 0) {
returnedValue = getFTPClient().sendCommand("RNTO " + tgtParent + getSeparator() + tgtName);
hasSucceeded = (returnedValue > 0);
}
}
finally {
_mutex.release();
}
}
return hasSucceeded;
}
@ -390,6 +436,8 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
* @see org.eclipse.rse.services.files.IFileService#createFolder(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String)
*/
public IHostFile createFolder(IProgressMonitor monitor, String remoteParent, String folderName)
{
if (_mutex.waitForLock(monitor, _mutexTimeout))
{
try
{
@ -401,6 +449,15 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
{
e.printStackTrace();
}
finally
{
_mutex.release();
}
}
//TODO getFile() will acquire the lock again after having released it
//For optimization, Mutex should notice when the _same_ thread tries
//to acquire a lock that it already holds again. To do so, the current
//locking thread would need to be remembered.
return getFile(monitor, remoteParent, folderName);
}
@ -408,13 +465,19 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
* @see org.eclipse.rse.services.files.IFileService#createFile(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String)
*/
public IHostFile createFile(IProgressMonitor monitor, String remoteParent, String fileName) {
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
try {
File tempFile = File.createTempFile("ftp", "temp");
tempFile.deleteOnExit();
upload(monitor, tempFile, remoteParent, fileName, true, null, null);
} catch (Exception e) {
}
catch (Exception e) {
e.printStackTrace();
}
finally {
_mutex.release();
}
}
return getFile(monitor, remoteParent, fileName);
}