/* remove invalid entries...without hostmask or without logins */
 private CopyOnWriteArrayList<AuthenticationInfo> cleanup(List<AuthenticationInfo> input) {
   if (input == null || input.size() == 0) {
     return null;
   }
   CopyOnWriteArrayList<AuthenticationInfo> ret = new CopyOnWriteArrayList<AuthenticationInfo>();
   for (AuthenticationInfo item : input) {
     if (StringUtils.isEmpty(item.getHostmask())) {
       continue;
     }
     if (StringUtils.isEmpty(item.getPassword()) && StringUtils.isEmpty(item.getPassword())) {
       continue;
     }
     ret.add(item);
   }
   return ret;
 }
  public void validate(Login login, String url) {

    if (StringUtils.isEmpty(url)) {
      return;
    }
    AuthenticationInfo.Type type = null;
    if (url.startsWith("ftp")) {
      type = Type.FTP;
    } else if (url.startsWith("http")) {
      type = Type.HTTP;
    } else {
      Log.L.info("Unknown Protocoll: " + url);
      return;
    }

    String urlHost = Browser.getHost(url, true);
    for (AuthenticationInfo info : list) {
      if (!info.isEnabled()) {
        continue;
      }
      String authHost = info.getHostmask();
      if (info.getType().equals(type) && !StringUtils.isEmpty(authHost)) {
        boolean contains = false;
        if (authHost.length() > urlHost.length()) {
          /* hostMask of AuthenticationInfo is longer */
          contains = authHost.contains(urlHost);
        } else {
          /* hostMask of urlHost is longer */
          contains = urlHost.contains(authHost);
        }
        if (contains) {
          if (StringUtils.equals(info.getUsername(), login.getUsername())
              && StringUtils.equals(info.getPassword(), login.getPassword())) {
            info.setLastValidated(System.currentTimeMillis());
          }
        }
      }
    }
  }
  public List<Login> getSortedLoginsList(String url) {
    if (StringUtils.isEmpty(url)) {
      return null;
    }
    AuthenticationInfo.Type type = null;
    if (url.startsWith("ftp")) {
      type = Type.FTP;
    } else if (url.startsWith("http")) {
      type = Type.HTTP;
    } else {
      Log.L.info("Unknown Protocoll: " + url);
      return null;
    }
    final ArrayList<AuthenticationInfo> possibleInfos = new ArrayList<AuthenticationInfo>();

    String urlHost = Browser.getHost(url, true);
    for (AuthenticationInfo info : list) {
      if (!info.isEnabled()) {
        continue;
      }
      final String authHost = info.getHostmask();
      if (info.getType().equals(type) && !StringUtils.isEmpty(authHost)) {
        boolean contains = false;
        if (authHost.length() > urlHost.length()) {
          /* hostMask of AuthenticationInfo is longer */
          contains = authHost.contains(urlHost);
        } else {
          /* hostMask of urlHost is longer */
          contains = urlHost.contains(authHost);
        }
        if (contains) {
          possibleInfos.add(info);
        }
      }
    }
    try {
      Collections.sort(
          possibleInfos,
          new Comparator<AuthenticationInfo>() {

            @Override
            public int compare(AuthenticationInfo o1, AuthenticationInfo o2) {
              int ret = Integer.compare(o2.getHostmask().length(), o1.getHostmask().length());
              if (ret == 0) {
                ret = Long.compare(o2.getLastValidated(), o1.getLastValidated());
              }
              if (ret == 0) {
                ret = Long.compare(o2.getCreated(), o1.getCreated());
              }
              return ret;
            }
          });

    } catch (Throwable e) {
      logger.log(e);
    }
    final ArrayList<Login> ret = new ArrayList<Login>();
    for (AuthenticationInfo info : possibleInfos) {
      ret.add(new Login(info.getUsername(), info.getPassword()));
    }
    return ret;
  }