mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-19 22:25:52 +02:00
Use Mutex to avoid parallel FTP directory queries
This commit is contained in:
parent
ec66da7bf4
commit
003e578773
1 changed files with 163 additions and 100 deletions
|
@ -36,6 +36,7 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.core.runtime.IProgressMonitor;
|
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.NamePatternMatcher;
|
||||||
import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
|
import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
|
||||||
import org.eclipse.rse.services.files.AbstractFileService;
|
import org.eclipse.rse.services.files.AbstractFileService;
|
||||||
|
@ -50,6 +51,9 @@ import sun.net.ftp.FtpClient;
|
||||||
public class FTPService extends AbstractFileService implements IFileService, IFTPService
|
public class FTPService extends AbstractFileService implements IFileService, IFTPService
|
||||||
{
|
{
|
||||||
private FTPClientService _ftpClient;
|
private FTPClientService _ftpClient;
|
||||||
|
private Mutex _mutex = new Mutex();
|
||||||
|
private long _mutexTimeout = 5000; //max.5 seconds to obtain dir channel
|
||||||
|
|
||||||
private String _userHome;
|
private String _userHome;
|
||||||
private IFTPDirectoryListingParser _ftpPropertiesUtil;
|
private IFTPDirectoryListingParser _ftpPropertiesUtil;
|
||||||
|
|
||||||
|
@ -151,6 +155,7 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
|
||||||
|
|
||||||
public IHostFile getFile(IProgressMonitor monitor, String remoteParent, String fileName)
|
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);
|
IHostFile[] matches = internalFetch(monitor, remoteParent, fileName, FILE_TYPE_FILES_AND_FOLDERS);
|
||||||
if (matches != null && matches.length > 0)
|
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);
|
NamePatternMatcher filematcher = new NamePatternMatcher(fileFilter, true, true);
|
||||||
List results = new ArrayList();
|
List results = new ArrayList();
|
||||||
|
if (_mutex.waitForLock(monitor, _mutexTimeout))
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
FtpClient ftp = getFTPClient();
|
FtpClient ftp = getFTPClient();
|
||||||
|
@ -212,6 +219,11 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_mutex.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
return (IHostFile[])results.toArray(new IHostFile[results.size()]);
|
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)
|
public boolean download(IProgressMonitor monitor, String remoteParent, String remoteFile, File localFile, boolean isBinary, String hostEncoding)
|
||||||
{
|
{
|
||||||
FtpClient ftp = getFTPClient();
|
if (_mutex.waitForLock(monitor, _mutexTimeout))
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
FtpClient ftp = getFTPClient();
|
||||||
ftp.cd(remoteParent);
|
ftp.cd(remoteParent);
|
||||||
/*
|
/*
|
||||||
if (isBinary)
|
if (isBinary)
|
||||||
|
@ -308,13 +322,18 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
|
||||||
totalWrote += readCount;
|
totalWrote += readCount;
|
||||||
}
|
}
|
||||||
bos.close();
|
bos.close();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return true;
|
finally
|
||||||
|
{
|
||||||
|
_mutex.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IHostFile getUserHome()
|
public IHostFile getUserHome()
|
||||||
|
@ -336,14 +355,20 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
|
||||||
*/
|
*/
|
||||||
public boolean delete(IProgressMonitor monitor, String remoteParent, String fileName) {
|
public boolean delete(IProgressMonitor monitor, String remoteParent, String fileName) {
|
||||||
boolean hasSucceeded = false;
|
boolean hasSucceeded = false;
|
||||||
|
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
|
||||||
try {
|
try {
|
||||||
getFTPClient().cd(remoteParent);
|
getFTPClient().cd(remoteParent);
|
||||||
int returnedValue = getFTPClient().sendCommand("DELE " + fileName);
|
int returnedValue = getFTPClient().sendCommand("DELE " + fileName);
|
||||||
hasSucceeded = (returnedValue > 0);
|
hasSucceeded = (returnedValue > 0);
|
||||||
} catch (IOException e) {
|
}
|
||||||
|
catch (IOException e) {
|
||||||
// Changing folder raised an exception
|
// Changing folder raised an exception
|
||||||
hasSucceeded = false;
|
hasSucceeded = false;
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
_mutex.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
return hasSucceeded;
|
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) {
|
public boolean rename(IProgressMonitor monitor, String remoteParent, String oldName, String newName) {
|
||||||
boolean hasSucceeded = false;
|
boolean hasSucceeded = false;
|
||||||
|
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
|
||||||
|
try {
|
||||||
int returnedValue = getFTPClient().sendCommand("RNFR " + remoteParent + getSeparator() + oldName);
|
int returnedValue = getFTPClient().sendCommand("RNFR " + remoteParent + getSeparator() + oldName);
|
||||||
if (returnedValue > 0) {
|
if (returnedValue > 0) {
|
||||||
returnedValue = getFTPClient().sendCommand("RNTO " + remoteParent + getSeparator() + newName);
|
returnedValue = getFTPClient().sendCommand("RNTO " + remoteParent + getSeparator() + newName);
|
||||||
hasSucceeded = (returnedValue > 0);
|
hasSucceeded = (returnedValue > 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
_mutex.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
return hasSucceeded;
|
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) {
|
public boolean rename(IProgressMonitor monitor, String remoteParent, String oldName, String newName, IHostFile oldFile) {
|
||||||
boolean hasSucceeded = false;
|
boolean hasSucceeded = false;
|
||||||
|
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
|
||||||
|
try {
|
||||||
int returnedValue = getFTPClient().sendCommand("RNFR " + remoteParent + getSeparator() + oldName);
|
int returnedValue = getFTPClient().sendCommand("RNFR " + remoteParent + getSeparator() + oldName);
|
||||||
if (returnedValue > 0) {
|
if (returnedValue > 0) {
|
||||||
returnedValue = getFTPClient().sendCommand("RNTO " + remoteParent + getSeparator() + newName);
|
returnedValue = getFTPClient().sendCommand("RNTO " + remoteParent + getSeparator() + newName);
|
||||||
hasSucceeded = (returnedValue > 0);
|
hasSucceeded = (returnedValue > 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
_mutex.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
return hasSucceeded;
|
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) {
|
public boolean move(IProgressMonitor monitor, String srcParent, String srcName, String tgtParent, String tgtName) {
|
||||||
boolean hasSucceeded = false;
|
boolean hasSucceeded = false;
|
||||||
|
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
|
||||||
|
try {
|
||||||
int returnedValue = getFTPClient().sendCommand("RNFR " + srcParent + getSeparator() + srcName);
|
int returnedValue = getFTPClient().sendCommand("RNFR " + srcParent + getSeparator() + srcName);
|
||||||
if (returnedValue > 0) {
|
if (returnedValue > 0) {
|
||||||
returnedValue = getFTPClient().sendCommand("RNTO " + tgtParent + getSeparator() + tgtName);
|
returnedValue = getFTPClient().sendCommand("RNTO " + tgtParent + getSeparator() + tgtName);
|
||||||
hasSucceeded = (returnedValue > 0);
|
hasSucceeded = (returnedValue > 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
_mutex.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
return hasSucceeded;
|
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)
|
* @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)
|
public IHostFile createFolder(IProgressMonitor monitor, String remoteParent, String folderName)
|
||||||
|
{
|
||||||
|
if (_mutex.waitForLock(monitor, _mutexTimeout))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -401,6 +449,15 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
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);
|
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)
|
* @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) {
|
public IHostFile createFile(IProgressMonitor monitor, String remoteParent, String fileName) {
|
||||||
|
if (_mutex.waitForLock(monitor, _mutexTimeout)) {
|
||||||
try {
|
try {
|
||||||
File tempFile = File.createTempFile("ftp", "temp");
|
File tempFile = File.createTempFile("ftp", "temp");
|
||||||
tempFile.deleteOnExit();
|
tempFile.deleteOnExit();
|
||||||
upload(monitor, tempFile, remoteParent, fileName, true, null, null);
|
upload(monitor, tempFile, remoteParent, fileName, true, null, null);
|
||||||
} catch (Exception e) {
|
}
|
||||||
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
_mutex.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
return getFile(monitor, remoteParent, fileName);
|
return getFile(monitor, remoteParent, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue