From ddf02094dd71c256cf52e61a168766339ee60b04 Mon Sep 17 00:00:00 2001 From: Javier Montalvo Orus Date: Mon, 14 May 2007 17:36:48 +0000 Subject: [PATCH] improved autodetection of FTPListingParser --- .../services/files/ftp/FTPService.java | 59 ++-------- .../ftp/parser/IFTPClientConfigFactory.java | 6 +- .../plugin.xml | 39 ++++--- .../schema/ftpFileEntryParser.exsd | 57 ++++++++-- .../ftp/parser/FTPClientConfigFactory.java | 103 +++++++++++++++++- 5 files changed, 189 insertions(+), 75 deletions(-) diff --git a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java index 6dd6b81bc81..b8633f6f515 100644 --- a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java +++ b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java @@ -39,6 +39,7 @@ * Javier Montalvo Orus (Symbian) - Fixing 174828 - [ftp] Folders are attempted to be removed as files * Javier Montalvo Orus (Symbian) - Fixing 176216 - [api] FTP sould provide API to allow clients register their own FTPListingParser * Martin Oberhuber (Wind River) - [186128] Move IProgressMonitor last in all API + * Javier Montalvo Orus (Symbian) - improved autodetection of FTPListingParser ********************************************************************************/ package org.eclipse.rse.internal.services.files.ftp; @@ -83,7 +84,6 @@ public class FTPService extends AbstractFileService implements IFileService, IFT private Mutex _commandMutex = new Mutex(); - private String _parser; private String _userHome; private transient String _hostName; private transient String _userId; @@ -287,61 +287,24 @@ public class FTPService extends AbstractFileService implements IFileService, IFT //System parser - _parser = _ftpPropertySet.getPropertyValue("parser"); //$NON-NLS-1$ + String systemName = _ftpClient.getSystemName(); - if(!_parser.equalsIgnoreCase("AUTO")) //$NON-NLS-1$ + _ftpClient.setParserFactory(_entryParserFactory); + + FTPClientConfig config = _entryParserFactory.getFTPClientConfig(_ftpPropertySet.getPropertyValue("parser"),systemName); //$NON-NLS-1$ + + if(config!=null) { - - _ftpClient.setParserFactory(_entryParserFactory); - _ftpClient.configure(_entryParserFactory.getFTPClientConfig(_parser)); - + _ftpClient.configure(config); } else { - //try to guess - - String systemName = _ftpClient.getSystemName().toUpperCase(); - if(systemName.indexOf(' ')!=-1) - { - systemName = systemName.substring(0,systemName.indexOf(' ')); - } - - //FTPClientConfig.SYST_NT = "WINDOWS" - if(systemName.startsWith(FTPClientConfig.SYST_NT)) - { - _ftpClient.setParserFactory(_entryParserFactory); - _ftpClient.configure(_entryParserFactory.getFTPClientConfig("WinNT")); //$NON-NLS-1$ - }else - //FTPClientConfig.SYST_MVS = "MVS" - if(systemName.startsWith(FTPClientConfig.SYST_MVS)) - { - _ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_MVS)); - }else - //FTPClientConfig.SYST_OS2 = "OS/2" - if(systemName.startsWith(FTPClientConfig.SYST_OS2)) - { - _ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_OS2)); - }else - //FTPClientConfig.SYST_OS400 = "OS/400" - if(systemName.startsWith(FTPClientConfig.SYST_OS400)) - { - _ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_OS400)); - }else - //FTPClientConfig.SYST_VMS = "VMS" - if(systemName.startsWith(FTPClientConfig.SYST_VMS)) - { - _ftpClient.setParserFactory(_entryParserFactory); - _ftpClient.configure(_entryParserFactory.getFTPClientConfig("VMS")); //$NON-NLS-1$ - }else - //Default UNIX-like parsing - //FTPClientConfig.SYST_UNIX = "UNIX" - { - _ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX)); - } + //UNIX parsing by default if no suitable parser found + _ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX)); } + // Initial active/passive mode. This action will be refreshed later using setDataConnectionMode() - if(_ftpPropertySet.getPropertyValue("passive").equalsIgnoreCase("true")) //$NON-NLS-1$ //$NON-NLS-2$ { _ftpClient.enterLocalPassiveMode(); diff --git a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/parser/IFTPClientConfigFactory.java b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/parser/IFTPClientConfigFactory.java index a492f67b855..5a46aaf2a73 100644 --- a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/parser/IFTPClientConfigFactory.java +++ b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/parser/IFTPClientConfigFactory.java @@ -6,6 +6,7 @@ * * Contributors: * Javier Montalvo Orus (Symbian) - initial API and implementation + * Javier Montalvo Orus (Symbian) - improved autodetection of FTPListingParser ********************************************************************************/ package org.eclipse.rse.internal.services.files.ftp.parser; @@ -20,10 +21,11 @@ public interface IFTPClientConfigFactory extends FTPFileEntryParserFactory { /** * - * @param key name attribute of the extension point to be returned + * @param parser Parser selected from the FTP Settings. This setting is "AUTO" by default, performing a parser discovery + * @param systemName String returned by the host from the FTP SYST command, describing the host * @return FTPClientConfig instance created from the attributes passed in the extension point */ - public FTPClientConfig getFTPClientConfig(String key); + public FTPClientConfig getFTPClientConfig(String parser, String systemName); /** * Returns a Set of key names diff --git a/rse/plugins/org.eclipse.rse.subsystems.files.ftp/plugin.xml b/rse/plugins/org.eclipse.rse.subsystems.files.ftp/plugin.xml index 4fff8ce37ec..acde7bf0190 100644 --- a/rse/plugins/org.eclipse.rse.subsystems.files.ftp/plugin.xml +++ b/rse/plugins/org.eclipse.rse.subsystems.files.ftp/plugin.xml @@ -17,6 +17,7 @@ Martin Oberhuber (Wind River) - add FTP Only system type Javier Montalvo Orus (Symbian) - [plan] Improve Discovery and Autodetect in RSE Javier Montalvo Orus (Symbian) - Fixing 176216 - [api] FTP sould provide API to allow clients register their own FTPListingParser Martin Oberhuber (Wind River) - [186523] Move subsystemConfigurations from UI to core +Javier Montalvo Orus (Symbian) - improved autodetection of FTPListingParser --> @@ -49,34 +50,46 @@ Martin Oberhuber (Wind River) - [186523] Move subsystemConfigurations from UI to + ftpSystemTypes=".*[Uu][Nn][Ii][Xx].*" + id="org.eclipse.rse.ftp.parser.Unix" + label="%FTPParser.UNIX" + priority="100"> + ftpSystemTypes=".*[Mm][Vv][Ss].*" + id="org.eclipse.rse.ftp.parser.MVS" + label="%FTPParser.MVS" + priority="100"> + ftpSystemTypes=".*[Ww][Ii][Nn][Dd][Oo][Ww][Ss].*" + id="org.eclipse.rse.ftp.parser.WinNT" + label="%FTPParser.WinNT" + priority="100"> + ftpSystemTypes=".*[Oo][Ss]/2.*" + id="org.eclipse.rse.ftp.parser.OS2" + label="%FTPParser.OS2" + priority="100"> + ftpSystemTypes=".*[Oo][Ss]/400.*" + id="org.eclipse.rse.ftp.parser.OS400" + label="%FTPParser.OS400" + priority="100"> + class="org.eclipse.rse.internal.subsystems.files.ftp.parser.RSEVMSFTPEntryParser" + ftpSystemTypes=".*[Vv][Mm][Ss].*" + id="org.eclipse.rse.ftp.parser.VMS" + label="%FTPParser.VMS" + priority="100"> diff --git a/rse/plugins/org.eclipse.rse.subsystems.files.ftp/schema/ftpFileEntryParser.exsd b/rse/plugins/org.eclipse.rse.subsystems.files.ftp/schema/ftpFileEntryParser.exsd index 1879b733ce6..f1818ac22b9 100644 --- a/rse/plugins/org.eclipse.rse.subsystems.files.ftp/schema/ftpFileEntryParser.exsd +++ b/rse/plugins/org.eclipse.rse.subsystems.files.ftp/schema/ftpFileEntryParser.exsd @@ -51,7 +51,7 @@ The string attributes <code>defaultDateFormatStr</code> <code> - + @@ -61,7 +61,7 @@ The string attributes <code>defaultDateFormatStr</code> <code> - Name that will be displayed in the UI + Name that will be displayed in the FTP Settings dialog @@ -71,7 +71,7 @@ The string attributes <code>defaultDateFormatStr</code> <code> - + Extension of <code>org.apache.commons.net.ftp.Configurable,org.apache.commons.net.ftp.FTPFileEntryParser</code> implementing the parser for the specific server @@ -81,35 +81,75 @@ The string attributes <code>defaultDateFormatStr</code> <code> - + This property specifies the main date format that will be used by a parser configured by this configuration to parse file timestamps. If this is not specified, such a parser will use as a default value, the most commonly used format which will be in as used in <code>en_US</code> locales. This should be in the format described for <code>java.text.SimpleDateFormat</code>. property. - + This property specifies a secondary date format that will be used by a parser configured by this configuration to parse file timestamps, typically those less than a year old. If this is not specified, such a parser will not attempt to parse using an alternate format. +This is used primarily in unix-based systems. +This should be in the format described for <code>java.text.SimpleDateFormat</code>. - + This property allows user to specify a <a href="http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt"> +two-letter ISO-639 language code</a> that will be used to configure the set of month names used by the file timestamp parser. +If neither this nor the shortMonthNames is specified, parsing will assume English month names, which may or may not be significant, depending on whether the date format(s) specified via defaultDateFormatStr and/or recentDateFormatStr are using + numeric or alphabetic month names. +If the code supplied is not supported here, <code>en_US</code> +month names will be used. We are supporting here those language +codes which, when a <code> java.util.Locale</code> is constucted +using it, and a <code>java.text.SimpleDateFormat</code> is +constructed using that Locale, the array returned by the +SimpleDateFormat's <code>getShortMonths()</code> method consists solely of three 8-bit ASCII character strings. Additionally, +languages which do not meet this requirement are included if a +common alternative set of short month names is known to be used. +This means that users who can tell us of additional such encodings +may get them added to the list of supported languages by contacting +the jakarta-commons-net team. +<strong> Please note that this attribute will NOT be used to determine a locale-based date format for the language. </strong> +Experience has shown that many if not most FTP servers outside the +United States employ the standard <code>en_US</code> date format +orderings of <code>MMM d yyyy</code> and <code>MMM d HH:mm</code> +and attempting to deduce this automatically here would cause more +problems than it would solve. The date format must be changed +via the defaultDateFormatStr and/or recentDateFormatStr parameters. - + This property allows the user to specify a set of month names used by the server that is different from those that may be specified using the setServerLanguageCode property. +This should be a string containing twelve strings each composed of +three characters, delimited by pipe (|) characters. Currently, +only 8-bit ASCII characters are known to be supported. For example, a set of month names used by a hypothetical Icelandic FTP server might conceivably be specified as <code>"jan|feb|mar|apr|ma&#xED;|j&#xFA;n|j&#xFA;l|&#xE1;g&#xFA;|sep|okt|n&#xF3;v|des"</code>. - + This property allows a time zone to be specified corresponding to that known to be used by an FTP server in file listings. This might be particularly useful to clients such as Ant that try to use these timestamps for dependency checking. This should be one of the identifiers used by <code>java.util.TimeZone</code> to refer to time zones, for example, <code>America/Chicago</code> or <code>Asia/Rangoon</code>. + + + + + + + Regular expression used to match with the string returned by the FTP SYST command to autodetect the parser if not specified by the user + + + + + + + Priority used if more than one extension matches the regular expression given by ftpSystemTypes. @@ -193,6 +233,7 @@ available at http://www.eclipse.org/legal/epl-v10.html Contributors: Javier Montalvo Orus (Symbian) - initial API and implementation + Javier Montalvo Orus (Symbian) - added ftpSystemTypes and priority diff --git a/rse/plugins/org.eclipse.rse.subsystems.files.ftp/src/org/eclipse/rse/internal/subsystems/files/ftp/parser/FTPClientConfigFactory.java b/rse/plugins/org.eclipse.rse.subsystems.files.ftp/src/org/eclipse/rse/internal/subsystems/files/ftp/parser/FTPClientConfigFactory.java index ace5a0bde97..3777295fe4d 100644 --- a/rse/plugins/org.eclipse.rse.subsystems.files.ftp/src/org/eclipse/rse/internal/subsystems/files/ftp/parser/FTPClientConfigFactory.java +++ b/rse/plugins/org.eclipse.rse.subsystems.files.ftp/src/org/eclipse/rse/internal/subsystems/files/ftp/parser/FTPClientConfigFactory.java @@ -6,6 +6,7 @@ * * Contributors: * Javier Montalvo Orus (Symbian) - initial API and implementation + * Javier Montalvo Orus (Symbian) - improved autodetection of FTPListingParser ********************************************************************************/ package org.eclipse.rse.internal.subsystems.files.ftp.parser; @@ -13,6 +14,7 @@ package org.eclipse.rse.internal.subsystems.files.ftp.parser; import java.util.Hashtable; import java.util.Set; import java.util.TreeSet; +import java.util.regex.Pattern; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPFileEntryParser; @@ -60,17 +62,104 @@ public class FTPClientConfigFactory implements IFTPClientConfigFactory { * (non-Javadoc) * @see org.eclipse.rse.internal.services.files.ftp.parser.IFTPClientConfigFactory#getFTPClientConfig(java.lang.String) */ - public FTPClientConfig getFTPClientConfig(String key) + public FTPClientConfig getFTPClientConfig(String parser, String systemName) { - if(!ftpConfig.containsKey(key)) + + FTPClientConfig ftpClientConfig = null; + + if(parser.equals("AUTO")) //$NON-NLS-1$ { + int priorityInt = Integer.MAX_VALUE; + int previousPriorityInt = Integer.MAX_VALUE; + IConfigurationElement selectedCofiguration = null; + IConfigurationElement[] ce = ep.getConfigurationElements(); for (int i = 0; i < ce.length; i++) { + String ftpSystemTypes = ce[i].getAttribute("ftpSystemTypes"); //$NON-NLS-1$ + if(ftpSystemTypes!=null) + { + Pattern ftpSystemTypesRegex = Pattern.compile(ftpSystemTypes); + if(ftpSystemTypesRegex.matcher(systemName).matches()) + { + //try to get priority otherwise assigning Integer.MAX_VALUE + String priority = ce[i].getAttribute("priority"); //$NON-NLS-1$ + if(priority!=null) + { + priorityInt = Integer.parseInt(priority); + } + else + { + priorityInt = Integer.MAX_VALUE; + } + + if(priorityInt < previousPriorityInt) + { + selectedCofiguration = ce[i]; + previousPriorityInt = priorityInt; + } + } + } + } + + //process the selected IConfigurationElement + if(selectedCofiguration != null) + { + FTPClientConfig config = null; + + //populate tables + String clas = selectedCofiguration.getAttribute("class"); //$NON-NLS-1$ + + if(!ftpFileEntryParser.containsKey(clas)) + { + FTPFileEntryParser entryParser=null; + try { + entryParser = (FTPFileEntryParser)selectedCofiguration.createExecutableExtension("class"); //$NON-NLS-1$ + } catch (CoreException e) { + throw new ParserInitializationException(e.getMessage()); + } + + ftpFileEntryParser.put(clas, entryParser); + } + + String defaultDateFormatStr = selectedCofiguration.getAttribute("defaultDateFormatStr"); //$NON-NLS-1$ + String recentDateFormatStr = selectedCofiguration.getAttribute("recentDateFormatStr"); //$NON-NLS-1$ + String serverLanguageCode = selectedCofiguration.getAttribute("serverLanguageCode"); //$NON-NLS-1$ + String shortMonthNames = selectedCofiguration.getAttribute("shortMonthNames"); //$NON-NLS-1$ + String serverTimeZoneId = selectedCofiguration.getAttribute("serverTimeZoneId"); //$NON-NLS-1$ + + config = new FTPClientConfig(clas); + + //not necessary checking for null, as null is valid input + config.setDefaultDateFormatStr(defaultDateFormatStr); + config.setRecentDateFormatStr(recentDateFormatStr); + config.setServerLanguageCode(serverLanguageCode); + config.setShortMonthNames(shortMonthNames); + config.setServerTimeZoneId(serverTimeZoneId); + + //not necessary storing in the hashtable, as discovered will not be reused + ftpClientConfig = config; + + } + } + + + if(ftpClientConfig==null) + { + if(ftpConfig.containsKey(parser)) + { + //restore parser from hashtable + ftpClientConfig = (FTPClientConfig)ftpConfig.get(parser); + } + else + { + IConfigurationElement[] ce = ep.getConfigurationElements(); + for (int i = 0; i < ce.length; i++) + { String label = ce[i].getAttribute("label"); //$NON-NLS-1$ - if(label.equals(key)) + if(label.equals(parser)) { FTPClientConfig config = null; @@ -103,13 +192,19 @@ public class FTPClientConfigFactory implements IFTPClientConfigFactory { config.setServerTimeZoneId(serverTimeZoneId); ftpConfig.put(label, config); + + ftpClientConfig = (FTPClientConfig)ftpConfig.get(parser); + + break; } } + } + } - return (FTPClientConfig)ftpConfig.get(key); + return ftpClientConfig; } /*