/**
   * Create the ftpClient and authenticate with the resource.
   *
   * @throws FileResourceException if an exception occurs during the resource start-up
   */
  public void start()
      throws InvalidSecurityContextException, IllegalHostException, FileResourceException {

    ServiceContact serviceContact = getAndCheckServiceContact();

    String host = getServiceContact().getHost();
    int port = getServiceContact().getPort();
    if (port == -1) {
      port = 21;
    }

    if (getName() == null) {
      setName(host + ":" + port);
    }

    try {
      SecurityContext securityContext = getOrCreateSecurityContext("ftp", serviceContact);

      PasswordAuthentication credentials = getCredentialsAsPasswordAuthentication(securityContext);

      ftpClient = new FTPClient(host, port);

      String username = credentials.getUserName();
      String password = String.valueOf(credentials.getPassword());

      ftpClient.authorize(username, password);
      ftpClient.setType(Session.TYPE_IMAGE);
      setStarted(true);
    } catch (Exception e) {
      throw translateException("Error connecting to the FTP server at " + host + ":" + port, e);
    }
  }
  /**
   * Equivalent to ls command in the current directory
   *
   * @throws IOException
   * @throws FileResourceException
   */
  public Collection<GridFile> list() throws FileResourceException {
    List<GridFile> gridFileList = new ArrayList<GridFile>();
    try {
      ftpClient.setPassive();
      ftpClient.setLocalActive();
      ftpClient.setType(Session.TYPE_ASCII);

      Enumeration<?> list = ftpClient.list().elements();
      ftpClient.setType(Session.TYPE_IMAGE);

      while (list.hasMoreElements()) {
        gridFileList.add(createGridFile(list.nextElement()));
      }
      return gridFileList;
    } catch (Exception e) {
      throw translateException("Cannot list the elements of the current directory", e);
    }
  }
  /**
   * Equivalent to ls command on the given directory
   *
   * @throws FileResourceException
   */
  public Collection<GridFile> list(String directory) throws FileResourceException {

    // Store currentDir
    String currentDirectory = getCurrentDirectory();
    // Change directory
    setCurrentDirectory(directory);
    Collection<GridFile> list = null;
    try {
      ftpClient.setType(Session.TYPE_ASCII);
      list = list();
      ftpClient.setType(Session.TYPE_IMAGE);
    } catch (Exception e) {
      throw translateException("Error in list directory", e);
    }

    // Come back to original directory
    setCurrentDirectory(currentDirectory);
    return list;
  }