/* (non-Javadoc)
  * @see com.aptana.ide.core.ftp.IFTPConnectionFileManager#init(java.lang.String, int, org.eclipse.core.runtime.IPath, java.lang.String, char[], boolean, java.lang.String, java.lang.String, java.lang.String)
  */
 public void init(
     String host,
     int port,
     IPath basePath,
     String login,
     char[] password,
     boolean passive,
     String transferType,
     String encoding,
     String timezone) {
   Assert.isTrue(ftpClient == null, Messages.FTPConnectionFileManager_already_initialized);
   try {
     this.pool = new FTPClientPool(this);
     ftpClient = createFTPClient();
     this.host = host;
     this.port = port;
     this.login = login;
     this.password = (password == null) ? new char[0] : password;
     this.basePath = basePath != null ? basePath : Path.ROOT;
     this.authId = Policy.generateAuthId("FTP", login, host, port); // $NON-NLS-1$
     this.transferType = transferType;
     this.timezone = timezone != null && timezone.length() == 0 ? null : timezone;
     initFTPClient(ftpClient, passive, encoding);
   } catch (Exception e) {
     IdeLog.logImportant(
         FTPPlugin.getDefault(), Messages.FTPConnectionFileManager_initialization_failed, e);
     ftpClient = null;
   }
 }
  @SuppressWarnings("deprecation")
  protected void getherServerInfo(ConnectionContext context, IProgressMonitor monitor) {
    Policy.checkCanceled(monitor);
    monitor.subTask(Messages.FTPConnectionFileManager_gethering_server_info);
    serverFeatures = null;
    try {
      String[] features = ftpClient.features();
      if (features != null && features.length > 0) {
        serverFeatures = new ArrayList<String>();
        for (int i = 0; i < features.length; ++i) {
          String feature = features[i].trim();
          if (feature.indexOf(' ') > 0) {
            feature = feature.substring(0, feature.indexOf(' '));
          }
          serverFeatures.add(feature);
        }
      }
    } catch (Exception e) {
    }
    try {
      String[] validCodes = {"214"}; // $NON-NLS-1$
      FTPReply reply = ftpClient.sendCommand("SITE HELP"); // $NON-NLS-1$
      ftpClient.validateReply(reply, validCodes);
      if (serverFeatures == null) {
        serverFeatures = new ArrayList<String>();
      }
      String[] data = reply.getReplyData();
      for (int i = 0; i < data.length; ++i) {
        String cmd = data[i].trim();
        if (cmd.startsWith("214")) { // $NON-NLS-1$
          continue;
        }
        serverFeatures.add(MessageFormat.format("SITE {0}", cmd)); // $NON-NLS-1$
      }
    } catch (Exception e) {
      e.getCause();
    }

    Policy.checkCanceled(monitor);
    FTPFile[] rootFiles = null;
    try {
      rootFiles = listFiles(Path.ROOT, monitor);
    } catch (Exception e) {
    }

    if (context != null && context.getBoolean(ConnectionContext.DETECT_TIMEZONE)) {
      serverTimeZoneShift = Integer.MIN_VALUE;
    } else if (timezone != null) {
      TimeZone tz = TimeZone.getTimeZone(timezone);
      if (tz != null) {
        long time = new Date().getTime();
        serverTimeZoneShift = TimeZone.getDefault().getOffset(time) - tz.getOffset(time);
      }
    }
    if (serverTimeZoneShift == Integer.MIN_VALUE) {
      Policy.checkCanceled(monitor);
      try {
        changeCurrentDir(Path.ROOT);
        FTPFile file = null;
        if (rootFiles != null) {
          for (FTPFile ftpFile : rootFiles) {
            if (ftpFile.isFile()
                && !ftpFile.getName().startsWith(".ht")
                && !(ftpFile.lastModified().getHours() == 0
                    && ftpFile.lastModified().getMinutes() == 0
                    && ftpFile.lastModified().getSeconds() == 0)) {
              file = ftpFile;
              break;
            }
          }
        }
        if (file == null && !Path.ROOT.equals(basePath)) {
          FTPFile[] ftpFiles = listFiles(basePath, monitor);
          for (FTPFile ftpFile : ftpFiles) {
            if (ftpFile.isFile()
                && !ftpFile.getName().startsWith(".ht")
                && !(ftpFile.lastModified().getHours() == 0
                    && ftpFile.lastModified().getMinutes() == 0
                    && ftpFile.lastModified().getSeconds() == 0)) {
              file = ftpFile;
              break;
            }
          }
        }
        Date lastModifiedLocal = null;
        if (file == null) {
          changeCurrentDir(basePath);
          lastModifiedLocal = new Date();
          ftpClient.put(new ByteArrayInputStream(new byte[] {}), TMP_TIMEZONE_CHECK);
          for (FTPFile ftpFile : listFiles(basePath, monitor)) {
            if (TMP_TIMEZONE_CHECK.equals(ftpFile.getName())) {
              file = ftpFile;
              break;
            }
          }
        }
        if (file != null) {
          Date lastModifiedServerInLocalTZ = file.lastModified();
          if (serverSupportsFeature("MDTM")) { // $NON-NLS-1$
            Date lastModifiedLocalTZ = ftpClient.modtime(file.getName());
            if (lastModifiedLocalTZ != null) {
              // align to minutes
              serverTimeZoneShift =
                  (lastModifiedLocalTZ.getTime() - lastModifiedLocalTZ.getTime() % 60000)
                      - (lastModifiedServerInLocalTZ.getTime()
                          - lastModifiedServerInLocalTZ.getTime() % 60000);
            }
          }
          if (serverTimeZoneShift == Integer.MIN_VALUE) {
            serverTimeZoneShift =
                (lastModifiedLocal.getTime() - lastModifiedLocal.getTime() % 60000)
                    - (lastModifiedServerInLocalTZ.getTime()
                        - lastModifiedServerInLocalTZ.getTime() % 60000);
            // align to 1/4 hour
            long rem = serverTimeZoneShift % 900000;
            if (rem < 450000) {
              serverTimeZoneShift -= rem;
            } else {
              serverTimeZoneShift += (900000 - rem);
            }
          }
          if (TMP_TIMEZONE_CHECK.equals(file.getName())) {
            ftpClient.delete(file.getName());
          }
          if (context != null) {
            Calendar cal = Calendar.getInstance();
            int rawOffset =
                (int)
                    (cal.get(Calendar.ZONE_OFFSET)
                        + cal.get(Calendar.DST_OFFSET)
                        - serverTimeZoneShift);
            context.put(ConnectionContext.SERVER_TIMEZONE, TimeZone.getAvailableIDs(rawOffset));
          }
        }
      } catch (OperationCanceledException e) {
        throw e;
      } catch (Exception e) {
        IdeLog.logImportant(
            FTPPlugin.getDefault(), Messages.FTPConnectionFileManager_server_tz_check, e);
      }
      if (serverTimeZoneShift == Integer.MIN_VALUE) {
        Calendar cal = Calendar.getInstance();
        serverTimeZoneShift = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
      }
    }

    hasServerInfo = true;
  }
  /* (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();
    }
  }