diff --git a/rse/plugins/org.eclipse.rse.services.files.ftp/META-INF/MANIFEST.MF b/rse/plugins/org.eclipse.rse.services.files.ftp/META-INF/MANIFEST.MF index 2252e159318..3395fc1904d 100644 --- a/rse/plugins/org.eclipse.rse.services.files.ftp/META-INF/MANIFEST.MF +++ b/rse/plugins/org.eclipse.rse.services.files.ftp/META-INF/MANIFEST.MF @@ -7,6 +7,7 @@ Bundle-Activator: org.eclipse.rse.services.files.ftp.Activator Bundle-Vendor: %providerName Bundle-Localization: plugin Require-Bundle: org.eclipse.core.runtime, - org.eclipse.rse.services + org.eclipse.rse.services, + org.apache.commons_net Eclipse-LazyStart: true Export-Package: org.eclipse.rse.services.files.ftp diff --git a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPClientService.java b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPClientService.java deleted file mode 100644 index 5a89d819eda..00000000000 --- a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPClientService.java +++ /dev/null @@ -1,37 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2006 IBM Corporation. All rights reserved. - * This program and the accompanying materials are made available under the terms - * of the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * - * Initial Contributors: - * The following IBM employees contributed to the Remote System Explorer - * component that contains this file: David McKnight, Kushal Munir, - * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, - * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley. - * - * Contributors: - * {Name} (company) - description of contribution. - ********************************************************************************/ - -package org.eclipse.rse.services.files.ftp; - -import java.io.IOException; - -import sun.net.ftp.FtpClient; - -public class FTPClientService extends FtpClient -{ - public int sendCommand(String command) - { - try - { - return issueCommand(command); - } - catch (IOException e) - { - e.printStackTrace(); - return -1; - } - } -} \ No newline at end of file diff --git a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPHostFile.java b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPHostFile.java index 45ab13300fe..a7b086ca5d2 100644 --- a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPHostFile.java +++ b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPHostFile.java @@ -12,53 +12,63 @@ * * Contributors: * Michael Berger (IBM) - Fixing 140408 - FTP upload does not work + * Javier Montalvo Orús (Symbian) - Migrate to jakarta commons net FTP client ********************************************************************************/ package org.eclipse.rse.services.files.ftp; + import java.io.File; +import org.apache.commons.net.ftp.FTPFile; import org.eclipse.rse.services.clientserver.archiveutils.ArchiveHandlerManager; import org.eclipse.rse.services.files.IHostFile; public class FTPHostFile implements IHostFile { + private String _name; private String _parentPath; - private boolean _isDirectory = false; - private boolean _isRoot = false; - private boolean _isArchive = false; - private long _lastModified = 0; - private long _size = 0; - private boolean _exists = false; + private boolean _isDirectory; + private boolean _isArchive; + private long _lastModified; + private long _size; + private boolean _canRead; + private boolean _canWrite; + private boolean _isRoot; + private boolean _exists; public FTPHostFile(String parentPath, String name, boolean isDirectory, boolean isRoot, long lastModified, long size, boolean exists) { _parentPath = parentPath; _name = name; _isDirectory = isDirectory; - _isRoot = isRoot; _lastModified = lastModified; _size = size; _isArchive = internalIsArchive(); + _canRead = true; + _canWrite = false; + _isRoot = isRoot; _exists = exists; } - - public String getName() + + public FTPHostFile(String parentPath, FTPFile ftpFile) { - return _name; + _parentPath = parentPath; + _name = ftpFile.getName(); + _isDirectory = ftpFile.isDirectory(); + _lastModified = ftpFile.getTimestamp().getTimeInMillis(); + _size = ftpFile.getSize(); + _isArchive = internalIsArchive(); + _canRead = ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION); + _canWrite = ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION); + _isRoot = false; + _exists = true; } - public boolean isHidden() + public long getSize() { - String name = getName(); - return name.charAt(0) == '.'; - - } - - public String getParentPath() - { - return _parentPath; + return _size; } public boolean isDirectory() @@ -71,13 +81,20 @@ public class FTPHostFile implements IHostFile return !(_isDirectory || _isRoot); } - public boolean isRoot() + public String getName() { - return _isRoot; + return _name; } - public boolean exists() - { + public boolean canRead() { + return _canRead; + } + + public boolean canWrite() { + return _canWrite; + } + + public boolean exists() { return _exists; } @@ -96,16 +113,32 @@ public class FTPHostFile implements IHostFile } } - public long getSize() - { - return _size; - } - public long getModifiedDate() { return _lastModified; } + public String geParentPath() { + return _parentPath; + } + + public boolean isArchive() { + return _isArchive; + } + + public boolean isHidden() { + return false; + } + + public boolean isRoot() { + return _parentPath==null; + + } + + public String getParentPath() { + return _parentPath; + } + public void renameTo(String newAbsolutePath) { int i = newAbsolutePath.lastIndexOf("/"); @@ -120,29 +153,12 @@ public class FTPHostFile implements IHostFile } _isArchive = internalIsArchive(); - - } - + protected boolean internalIsArchive() { return ArchiveHandlerManager.getInstance().isArchive(new File(getAbsolutePath())) && !ArchiveHandlerManager.isVirtual(getAbsolutePath()); } - public boolean isArchive() - { - return _isArchive; - } - - //TODO implement this - public boolean canRead() { - return true; - } - - //TODO implement this - public boolean canWrite() { - return true; - } - } \ No newline at end of file diff --git a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPLinuxDirectoryListingParser.java b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPLinuxDirectoryListingParser.java deleted file mode 100644 index 45ef3994cc6..00000000000 --- a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPLinuxDirectoryListingParser.java +++ /dev/null @@ -1,80 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2006 IBM Corporation. All rights reserved. - * This program and the accompanying materials are made available under the terms - * of the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * - * Initial Contributors: - * The following IBM employees contributed to the Remote System Explorer - * component that contains this file: David McKnight, Kushal Munir, - * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, - * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley. - * - * Contributors: - * Michael Berger (IBM) - Fixing 140408 - FTP upload does not work - ********************************************************************************/ - -package org.eclipse.rse.services.files.ftp; - -import java.text.DateFormat; -import java.util.Calendar; -import java.util.Locale; - -public class FTPLinuxDirectoryListingParser implements IFTPDirectoryListingParser -{ - - public FTPHostFile getFTPHostFile(String line, String parentPath) - { - // Note this assumes that text is always formatted the same way - if (line == null) return null; - String[] tokens = line.split("\\s+", 9); - if (tokens.length < 9) return null; - String name = tokens[8]; - boolean isDirectory = line.charAt(0) == 'd'; - long length = 0; - long lastMod = 0; - if (tokens.length > 4) - { - try - { - length = Long.parseLong(tokens[tokens.length - 5]); - } - catch (NumberFormatException e) {} - - try - { - int i = tokens.length - 4; - int j = tokens.length - 3; - int k = tokens.length - 2; - String time = ""; - String year = ""; - if (tokens[k].indexOf(":") == -1) - { - time = "11:59 PM"; - year = tokens[k]; - } - else - { - String[] parts = tokens[k].split(":"); - int hours = Integer.parseInt(parts[0]); - boolean morning = hours < 12; // assumes midnight is 00:00 - if (morning) - { - if (hours == 0) - { - hours = 12; - } - } - time = hours + ":" + parts[1] + (morning? " AM" : " PM"); - year = "" + (Calendar.getInstance().get(Calendar.YEAR)); - } - - String date = tokens[i] + " " + tokens[j] + ", " + year + " " + time; - lastMod = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, new Locale("EN", "US")).parse(date).getTime(); - } - catch (Exception e) {} - } - return new FTPHostFile(parentPath, name, isDirectory, false, lastMod, length, true); - } - -} \ No newline at end of file diff --git a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPService.java b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPService.java index 907005fc100..515038610b6 100644 --- a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPService.java +++ b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/FTPService.java @@ -16,62 +16,57 @@ * delete, move and rename. * Javier Montalvo Orus (Symbian) - Bug 140348 - FTP did not use port number * Michael Berger (IBM) - Fixing 140404 - FTP new file creation does not work + * Javier Montalvo Orús (Symbian) - Migrate to jakarta commons net FTP client ********************************************************************************/ package org.eclipse.rse.services.files.ftp; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; -import java.net.URL; -import java.net.URLConnection; import java.util.ArrayList; import java.util.List; +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPFile; 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; import org.eclipse.rse.services.files.IFileService; import org.eclipse.rse.services.files.IHostFile; -import sun.net.TelnetInputStream; -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 FTPClient _ftpClient; + private String _userHome; - private IFTPDirectoryListingParser _ftpPropertiesUtil; private transient String _hostname; private transient String _userId; private transient String _password; private transient int _portNumber; - private URLConnection _urlConnection; - - public FTPService() - { - } + /* + * (non-Javadoc) + * @see org.eclipse.rse.services.IService#getName() + */ public String getName() { return FTPServiceResources.FTP_File_Service_Name; } + /* + * (non-Javadoc) + * @see org.eclipse.rse.services.IService#getDescription() + */ public String getDescription() { return FTPServiceResources.FTP_File_Service_Description; @@ -95,19 +90,18 @@ public class FTPService extends AbstractFileService implements IFileService, IFT { _password = password; } - public void connect() throws Exception { - FtpClient ftp = getFTPClient(); + FTPClient ftp = getFTPClient(); if (_portNumber == 0) { - ftp.openServer(_hostname); + ftp.connect(_hostname); } else { - ftp.openServer(_hostname, _portNumber); + ftp.connect(_hostname, _portNumber); } ftp.login(_userId, _password); - _userHome = ftp.pwd(); + _userHome = ftp.printWorkingDirectory(); } protected void reconnect() @@ -125,114 +119,107 @@ public class FTPService extends AbstractFileService implements IFileService, IFT { try { - getFTPClient().closeServer(); + getFTPClient().logout(); _ftpClient = null; } catch (Exception e) { - // e.printStackTrace(); _ftpClient = null; } } - public IFTPDirectoryListingParser getDirListingParser() - { - if (_ftpPropertiesUtil == null) - { - _ftpPropertiesUtil = new FTPLinuxDirectoryListingParser(); - } - return _ftpPropertiesUtil; - } - - public FTPClientService getFTPClient() + public FTPClient getFTPClient() { if (_ftpClient == null) { - _ftpClient = new FTPClientService(); + _ftpClient = new FTPClient(); } - return _ftpClient; + return _ftpClient; } + /* + * (non-Javadoc) + * @see org.eclipse.rse.services.files.IFileService#getFile(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String) + */ 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) { return matches[0]; } else { - return new FTPHostFile(remoteParent, fileName, false, false, 0, 0, false); + return null; } } + public boolean isConnected() { - return getFTPClient().serverIsOpen(); + return getFTPClient().isConnected(); } + /* + * (non-Javadoc) + * @see org.eclipse.rse.services.files.AbstractFileService#internalFetch(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, int) + */ protected IHostFile[] internalFetch(IProgressMonitor monitor, String parentPath, String fileFilter, int fileType) { + if (fileFilter == null) { fileFilter = "*"; } + NamePatternMatcher filematcher = new NamePatternMatcher(fileFilter, true, true); List results = new ArrayList(); - if (_mutex.waitForLock(monitor, _mutexTimeout)) + + try { + FTPClient ftp = getFTPClient(); try { - FtpClient ftp = getFTPClient(); - try - { - ftp.noop(); - } - catch (Exception e) - { - //e.printStackTrace(); - disconnect(); - - // probably timed out - reconnect(); - ftp = getFTPClient(); - } - - ftp.cd(parentPath); - TelnetInputStream stream = ftp.list(); - BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); - String line = reader.readLine(); - while (line != null) - { - FTPHostFile node = getDirListingParser().getFTPHostFile(line, parentPath); - if (node != null && filematcher.matches(node.getName())) - { - if (isRightType(fileType, node)) - { - results.add(node); - } - } - line = reader.readLine(); - } + ftp.noop(); } catch (Exception e) - { - e.printStackTrace(); - } - finally { - _mutex.release(); + disconnect(); + + reconnect(); + ftp = getFTPClient(); + } + + ftp.changeWorkingDirectory(parentPath); + + FTPFile[] ftpFiles = ftp.listFiles(); + + for(int i=0; i 0); - } - catch (IOException e) { - // Changing folder raised an exception - hasSucceeded = false; - } - finally { - _mutex.release(); - } + + try { + getFTPClient().cwd(remoteParent); + int returnedValue = getFTPClient().dele(fileName); + hasSucceeded = (returnedValue > 0); } + catch (IOException e) { + // Changing folder raised an exception + hasSucceeded = false; + } + return hasSucceeded; } @@ -376,20 +352,16 @@ public class FTPService extends AbstractFileService implements IFileService, IFT * @see org.eclipse.rse.services.files.IFileService#rename(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, java.lang.String) */ 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(); - } + + try { + + getFTPClient().rename(remoteParent + getSeparator() + oldName, remoteParent + getSeparator() + newName); + + } catch (IOException e) { + return false; } - return hasSucceeded; + + return true; } /* (non-Javadoc) @@ -397,18 +369,9 @@ 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(); - } - } + + oldFile.renameTo(newName); + return hasSucceeded; } @@ -417,18 +380,19 @@ 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); - } + + try { + + int returnedValue; + returnedValue = getFTPClient().sendCommand("RNFR " + srcParent + getSeparator() + srcName); + + if (returnedValue > 0) { + returnedValue = getFTPClient().sendCommand("RNTO " + tgtParent + getSeparator() + tgtName); + hasSucceeded = (returnedValue > 0); } - finally { - _mutex.release(); - } - } + + }catch (IOException e) {} + return hasSucceeded; } @@ -437,27 +401,13 @@ public class FTPService extends AbstractFileService implements IFileService, IFT */ public IHostFile createFolder(IProgressMonitor monitor, String remoteParent, String folderName) { - if (_mutex.waitForLock(monitor, _mutexTimeout)) + try { - try - { - FTPClientService ftp = getFTPClient(); - ftp.cd(remoteParent); - ftp.sendCommand("MKD " + folderName); - } - catch (Exception e) - { - e.printStackTrace(); - } - finally - { - _mutex.release(); - } + FTPClient ftp = getFTPClient(); + ftp.makeDirectory(folderName); } - //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. + catch (Exception e) {} + return getFile(monitor, remoteParent, folderName); } @@ -465,19 +415,16 @@ 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) { - e.printStackTrace(); - } - finally { - _mutex.release(); - } + + try { + File tempFile = File.createTempFile("ftp", "temp"); + tempFile.deleteOnExit(); + upload(monitor, tempFile, remoteParent, fileName, true, null, null); } + catch (Exception e) { + e.printStackTrace(); + } + return getFile(monitor, remoteParent, fileName); } @@ -490,7 +437,7 @@ public class FTPService extends AbstractFileService implements IFileService, IFT public boolean copy(IProgressMonitor monitor, String srcParent, String srcName, String tgtParent, String tgtName) { - return false; + return move(monitor, srcParent, srcName, tgtParent, tgtName); } public boolean copyBatch(IProgressMonitor monitor, String[] srcParents, String[] srcNames, String tgtParent) throws SystemMessageException diff --git a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/IFTPDirectoryListingParser.java b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/IFTPDirectoryListingParser.java deleted file mode 100644 index 29ba5db3a0a..00000000000 --- a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/services/files/ftp/IFTPDirectoryListingParser.java +++ /dev/null @@ -1,36 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2006 IBM Corporation. All rights reserved. - * This program and the accompanying materials are made available under the terms - * of the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * - * Initial Contributors: - * The following IBM employees contributed to the Remote System Explorer - * component that contains this file: David McKnight, Kushal Munir, - * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, - * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley. - * - * Contributors: - * {Name} (company) - description of contribution. - ********************************************************************************/ - -package org.eclipse.rse.services.files.ftp; - -/** - * Implementers of this interface provide a way to get - * information about file properties from an FTP directory - * listing in a way - * that might be specific to a certain system type. - * @author mjberger - * - */ -public interface IFTPDirectoryListingParser -{ - /** - * Return an FTPHostFile representing a line from an FTP directory listing - * @param line The line of text from the directory listing - * @param parentPath The directory that this is a listing of - * @return null if the line is not well formed - */ - public FTPHostFile getFTPHostFile(String line, String parentPath); -} \ No newline at end of file