View Javadoc

    1   /*
    2    * Copyright 2004-2005 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.net.ftp.parser;
    17
    18  import org.apache.commons.net.ftp.Configurable;
    19  import org.apache.commons.net.ftp.FTPClientConfig;
    20  import org.apache.commons.net.ftp.FTPFileEntryParser;
    21
    22
    23  /***
    24   * This is the default implementation of the
    25   * FTPFileEntryParserFactory interface.  This is the
    26   * implementation that will be used by
    27   * org.apache.commons.net.ftp.FTPClient.listFiles()
    28   * if no other implementation has been specified.
    29   *
    30   * @see org.apache.commons.net.ftp.FTPClient#listFiles
    31   * @see org.apache.commons.net.ftp.FTPClient#setParserFactory
    32   */
    33  public class DefaultFTPFileEntryParserFactory
    34      implements FTPFileEntryParserFactory
    35  {
    36  	private FTPClientConfig config = null;
    37
    38  	/***
    39       * This default implementation of the FTPFileEntryParserFactory
    40       * interface works according to the following logic:
    41       * First it attempts to interpret the supplied key as a fully
    42       * qualified classname of a class implementing the
    43       * FTPFileEntryParser interface.  If that succeeds, a parser
    44       * object of this class is instantiated and is returned;
    45       * otherwise it attempts to interpret the key as an identirier
    46       * commonly used by the FTP SYST command to identify systems.
    47       * <p/>
    48       * If <code>key</code> is not recognized as a fully qualified
    49       * classname known to the system, this method will then attempt
    50       * to see whether it <b>contains</b> a string identifying one of
    51       * the known parsers.  This comparison is <b>case-insensitive</b>.
    52       * The intent here is where possible, to select as keys strings
    53       * which are returned by the SYST command on the systems which
    54       * the corresponding parser successfully parses.  This enables
    55       * this factory to be used in the auto-detection system.
    56       * <p/>
    57       *
    58       * @param key    should be a fully qualified classname corresponding to
    59       *               a class implementing the FTPFileEntryParser interface<br/>
    60       *               OR<br/>
    61       *               a string containing (case-insensitively) one of the
    62       *               following keywords:
    63       *               <ul>
    64       *               <li>{@link FTPClientConfig#SYST_UNIX UNIX}</li>
    65       *               <li>{@link FTPClientConfig#SYST_NT WINDOWS}</li>
    66       *               <li>{@link FTPClientConfig#SYST_OS2 OS/2}</li>
    67       *               <li>{@link FTPClientConfig#SYST_OS400 OS/400}</li>
    68       *               <li>{@link FTPClientConfig#SYST_VMS VMS}</li>
    69       *               <li>{@link FTPClientConfig#SYST_MVS MVS}</li>
    70       *               </ul>
    71       * @return the FTPFileEntryParser corresponding to the supplied key.
    72       * @throws ParserInitializationException thrown if for any reason the factory cannot resolve
    73       *                   the supplied key into an FTPFileEntryParser.
    74       * @see FTPFileEntryParser
    75       */
    76      public FTPFileEntryParser createFileEntryParser(String key)
    77      {
    78          Class parserClass = null;
    79          FTPFileEntryParser parser = null;
    80          try
    81          {
    82              parserClass = Class.forName(key);
    83              parser = (FTPFileEntryParser) parserClass.newInstance();
    84          }
    85          catch (ClassNotFoundException e)
    86          {
    87              String ukey = null;
    88              if (null != key)
    89              {
    90                  ukey = key.toUpperCase();
    91              }
    92              if (ukey.indexOf(FTPClientConfig.SYST_UNIX) >= 0)
    93              {
    94                  parser = createUnixFTPEntryParser();
    95              }
    96              else if (ukey.indexOf(FTPClientConfig.SYST_VMS) >= 0)
    97              {
    98                  parser = createVMSVersioningFTPEntryParser();
    99              }
    100             else if (ukey.indexOf(FTPClientConfig.SYST_NT) >= 0)
    101             {
    102                 parser = createNTFTPEntryParser();
    103             }
    104             else if (ukey.indexOf(FTPClientConfig.SYST_OS2) >= 0)
    105             {
    106                 parser = createOS2FTPEntryParser();
    107             }
    108             else if (ukey.indexOf(FTPClientConfig.SYST_OS400) >= 0)
    109             {
    110                 parser = createOS400FTPEntryParser();
    111             }
    112             else if (ukey.indexOf(FTPClientConfig.SYST_MVS) >= 0)
    113             {
    114                 parser = createMVSEntryParser();
    115         	}
    116             else
    117             {
    118                 throw new ParserInitializationException("Unknown parser type: " + key);
    119             }
    120         }
    121         catch (ClassCastException e)
    122         {
    123             throw new ParserInitializationException(parserClass.getName()
    124                 + " does not implement the interface "
    125                 + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
    126         }
    127         catch (Throwable e)
    128         {
    129             throw new ParserInitializationException("Error initializing parser", e);
    130         }
    131
    132         if (parser instanceof Configurable) {
    133             ((Configurable)parser).configure(this.config);
    134         }
    135         return parser;
    136     }
    137
    138     /***
    139      * <p>Implementation extracts a key from the supplied
    140      * {@link  FTPClientConfig FTPClientConfig}
    141      * parameter and creates an object implementing the
    142      * interface FTPFileEntryParser and uses the supplied configuration
    143      * to configure it.
    144      * </p><p>
    145      * Note that this method will generally not be called in scenarios
    146      * that call for autodetection of parser type but rather, for situations
    147      * where the user knows that the server uses a non-default configuration
    148      * and knows what that configuration is.
    149      * </p>
    150      * @param config  A {@link  FTPClientConfig FTPClientConfig}
    151      * used to configure the parser created
    152      *
    153      * @return the @link  FTPFileEntryParser FTPFileEntryParser} so created.
    154      * @exception ParserInitializationException
    155      *                   Thrown on any exception in instantiation
    156      * @since 1.4
    157      */
    158 	public FTPFileEntryParser createFileEntryParser(FTPClientConfig config)
    159 	throws ParserInitializationException
    160 	{
    161 	    this.config = config;
    162 		String key = config.getServerSystemKey();
    163 		return createFileEntryParser(key);
    164 	}
    165
    166
    167     public FTPFileEntryParser createUnixFTPEntryParser()
    168     {
    169         return (FTPFileEntryParser) new UnixFTPEntryParser();
    170     }
    171
    172     public FTPFileEntryParser createVMSVersioningFTPEntryParser()
    173     {
    174         return (FTPFileEntryParser) new VMSVersioningFTPEntryParser();
    175     }
    176
    177     public FTPFileEntryParser createNTFTPEntryParser()
    178     {
    179     	if (config != null && FTPClientConfig.SYST_NT.equals(
    180     	        config.getServerSystemKey()))
    181     	{
    182             return new NTFTPEntryParser();
    183     	} else {
    184             return new CompositeFileEntryParser(new FTPFileEntryParser[]
    185 	   	        {
    186 	   	            new NTFTPEntryParser(),
    187 	   	            new UnixFTPEntryParser()
    188 	   	        });
    189     	}
    190     }
    191
    192      public FTPFileEntryParser createOS2FTPEntryParser()
    193     {
    194         return (FTPFileEntryParser) new OS2FTPEntryParser();
    195     }
    196
    197     public FTPFileEntryParser createOS400FTPEntryParser()
    198     {
    199     	if (config != null &&
    200     	        FTPClientConfig.SYST_OS400.equals(config.getServerSystemKey()))
    201     	{
    202             return new OS400FTPEntryParser();
    203     	} else {
    204 	        return new CompositeFileEntryParser(new FTPFileEntryParser[]
    205 	            {
    206 	                new OS400FTPEntryParser(),
    207 	                new UnixFTPEntryParser()
    208 	            });
    209     	}
    210     }
    211
    212     public FTPFileEntryParser createMVSEntryParser()
    213     {
    214         return new MVSFTPEntryParser();
    215     }
    216
    217
    218
    219 }
    220