private static void throwFileNotFound(FTPException e, IPath path)
     throws FileNotFoundException, FTPException {
   int code = e.getReplyCode();
   if (code == 550 || code == 450) {
     throw new FileNotFoundException(path.toPortableString());
   }
   throw e;
 }
 private FTPFile[] listFiles(IPath dirPath, IProgressMonitor monitor)
     throws IOException, ParseException, FTPException {
   FTPFile[] ftpFiles = null;
   if (statSupported != Boolean.FALSE) {
     try {
       ftpFiles = ftpSTAT(dirPath.addTrailingSeparator().toPortableString());
     } catch (MalformedReplyException e) {
       statSupported = Boolean.FALSE;
     } catch (FTPException e) {
       if (e.getReplyCode() == 501 || e.getReplyCode() == 502 || e.getReplyCode() == 504) {
         statSupported = null;
       } else if (e.getReplyCode() != 500) {
         throwFileNotFound(e, dirPath);
       }
     }
     if (ftpFiles == null || ftpFiles.length == 0) {
       if (statSupported == null) {
         statSupported = Boolean.FALSE;
       }
       return ftpLIST(dirPath, monitor);
     } else if (ftpFiles[0].isLink()) {
       return ftpLIST(dirPath, monitor);
     } else if (statSupported == null) {
       statSupported = Boolean.TRUE;
     }
   } else {
     ftpFiles = ftpLIST(dirPath, monitor);
   }
   if (fileFactory.getSystem().toUpperCase().startsWith(WINDOWS_STR) && ftpFiles != null) {
     for (FTPFile ftpFile : ftpFiles) {
       if (ftpFile.getPermissions() == null) {
         ftpFile.setPermissions("-rw-r-----"); // $NON-NLS-1$
       }
     }
   }
   return ftpFiles;
 }
  /* (non-Javadoc)
   * @see com.aptana.ide.core.io.vfs.IConnectionFileManager#connect(org.eclipse.core.runtime.IProgressMonitor)
   */
  public void connect(IProgressMonitor monitor) throws CoreException {
    Assert.isTrue(ftpClient != null, Messages.FTPConnectionFileManager_not_initialized);
    monitor = Policy.monitorFor(monitor);
    try {
      cwd = null;
      cleanup();

      ConnectionContext context = CoreIOPlugin.getConnectionContext(this);

      if (messageLogWriter == null) {
        if (context != null) {
          Object object = context.get(ConnectionContext.COMMAND_LOG);
          if (object instanceof PrintWriter) {
            messageLogWriter = (PrintWriter) object;
          } else if (object instanceof OutputStream) {
            messageLogWriter = new PrintWriter((OutputStream) object);
          }
        }
        if (messageLogWriter == null) {
          messageLogWriter = FTPPlugin.getDefault().getFTPLogWriter();
        }
        if (messageLogWriter != null) {
          messageLogWriter.println(StringUtils.format("---------- FTP {0} ----------", host));
          setMessageLogger(ftpClient, messageLogWriter);
        }
      } else {
        messageLogWriter.println(
            StringUtils.format("---------- RECONNECTING - FTP {0} ----------", host));
      }

      monitor.beginTask(
          Messages.FTPConnectionFileManager_establishing_connection, IProgressMonitor.UNKNOWN);
      ftpClient.setRemoteHost(host);
      ftpClient.setRemotePort(port);
      while (true) {
        monitor.subTask(Messages.FTPConnectionFileManager_connecting);
        ftpClient.connect();
        if (password.length == 0
            && !IFTPConstants.LOGIN_ANONYMOUS.equals(login)
            && (context == null || !context.getBoolean(ConnectionContext.NO_PASSWORD_PROMPT))) {
          getOrPromptPassword(
              StringUtils.format(Messages.FTPConnectionFileManager_ftp_auth, host),
              Messages.FTPConnectionFileManager_specify_password);
        }
        Policy.checkCanceled(monitor);
        monitor.subTask(Messages.FTPConnectionFileManager_authenticating);
        try {
          ftpClient.login(login, String.copyValueOf(password));
        } catch (FTPException e) {
          Policy.checkCanceled(monitor);
          if (ftpClient.getLastValidReply() == null
              || "331".equals(ftpClient.getLastValidReply().getReplyCode())) { // $NON-NLS-1$
            if (context != null && context.getBoolean(ConnectionContext.NO_PASSWORD_PROMPT)) {
              throw new CoreException(
                  new Status(
                      Status.ERROR,
                      FTPPlugin.PLUGIN_ID,
                      StringUtils.format("Authentication failed: {0}", e.getLocalizedMessage()),
                      e));
            }
            promptPassword(
                StringUtils.format(Messages.FTPConnectionFileManager_ftp_auth, host),
                Messages.FTPConnectionFileManager_invalid_password);
            safeQuit();
            continue;
          }
          throw e;
        }
        break;
      }

      Policy.checkCanceled(monitor);
      changeCurrentDir(basePath);

      ftpClient.setType(
          IFTPConstants.TRANSFER_TYPE_ASCII.equals(transferType)
              ? FTPTransferType.ASCII
              : FTPTransferType.BINARY);

      if ((hasServerInfo
              || (context != null && context.getBoolean(ConnectionContext.QUICK_CONNECT)))
          && !(context != null && context.getBoolean(ConnectionContext.DETECT_TIMEZONE))) {
        return;
      }
      getherServerInfo(context, monitor);

    } catch (OperationCanceledException e) {
      safeQuit();
      throw e;
    } catch (CoreException e) {
      safeQuit();
      throw e;
    } catch (UnknownHostException e) {
      safeQuit();
      throw new CoreException(
          new Status(
              Status.ERROR,
              FTPPlugin.PLUGIN_ID,
              "Host name not found: " + e.getLocalizedMessage(),
              e));
    } catch (FileNotFoundException e) {
      safeQuit();
      throw new CoreException(
          new Status(
              Status.ERROR,
              FTPPlugin.PLUGIN_ID,
              "Remote folder not found: " + e.getLocalizedMessage(),
              e));
    } catch (Exception e) {
      safeQuit();
      throw new CoreException(
          new Status(
              Status.ERROR,
              FTPPlugin.PLUGIN_ID,
              Messages.FTPConnectionFileManager_connection_failed + e.getLocalizedMessage(),
              e));
    } finally {
      monitor.done();
    }
  }