Package us.jyg.freshet.util

Source Code of us.jyg.freshet.util.UserHandler

package us.jyg.freshet.util;


import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import us.jyg.freshet.dao.model.User;

/**
*
* @author jason
*
*/
public class UserHandler {

    private static final Log log = LogFactory.getLog(UserHandler.class);

 
  static UserHandler instance = null;
 
  // a list of UserBeans
  Map userMap = Collections.synchronizedMap(new HashMap());
  List guestList = Collections.synchronizedList(new ArrayList());
  static boolean questOk = false;
  static boolean localhostOk = false;

  private UserHandler() {
    log.info("Creating new UserHandler");
  }
 
  public static UserHandler getInstance() {
    if (instance == null)
      instance = new UserHandler();
    if ("true".equals(System.getProperty("freshet.fluid.guestOk")))
      questOk = true;
    if ("true".equals(System.getProperty("freshet.fluid.localhostOk"))) {
      localhostOk = true;
    }
    return instance;
  }
   
  public boolean login(String name, String password, String host) throws LoginException {
    try {
      host = InetAddress.getByName(host).getHostAddress();
    } catch (Exception e) {
      log.error("cannot get local host name, weird.");
    }
 
   
    if ("quest".equals(name)) {
      if (host != null) {
        guestList.add(host);
        log.info( host + "added to guest list");
        return true;
      } else {
        String err = "Trying to login guest with a null host, weird";
        log.error(err);
        throw new LoginException(err);
      }
    }
     
    if (name == null || host == null || password == null)
      throw new LoginException( "name = \""+ name +"\" host = \""+ host + "\" passwd = " + ((password == null)?"null":"not_null"));
   
//    if (canAuthenticate(name, password)) {
//        UserBean bean = new UserBean();
//        bean.setName(new String(name));
//        bean.setHost(new String(host));
//        userMap.put(host, bean);
//        log.info( name + "@" + host + " added to user map");
//        return true;
//    } else
      return false;
  }
 
  public void setHostConnected(String host) {
    try {
      host = InetAddress.getByName(host).getHostAddress();
    } catch (Exception e) {
      log.error("cannot get local host name, weird - bailing on auth.");
    }
 
    log.info( host + " connects to listen");
   
    if (userMap.containsKey(host))
      ((User)(userMap.get(host))).setConnected(true);
  }
 
  public void setHostDisconnected(String host) {
    try {
      host = InetAddress.getByName(host).getHostAddress();
    } catch (Exception e) {
      log.error("cannot get local host name, weird - bailing on auth.");
    }
     
    log.info( host + "listener disconnects");
    if (userMap.containsKey(host))
      ((User)(userMap.get(host))).setConnected(false);
  }
 
  public void logout(String host) {
    try {
      host = InetAddress.getByName(host).getHostAddress();
    } catch (Exception e) {
      log.warn("cannot get local host name, weird - bailing on auth.");
    }
   
    if (userMap.containsKey(host)) {
        User user = (User)userMap.get(host);
       
        if (userMap.remove(host) == null)
          log.error( "could not remove " + user.getUsername() + "@" + host + " from user map,  weird.");
        else
          log.info( user.getUsername() + "@" + host + " logs out");
    } else 
      log.error( host + " logs out  but is not in the user map, weird.");
  }
 
  public boolean hostIsAuthenticated(String host) {
      return true;
//   
//    try {
//      host = InetAddress.getByName(host).getHostAddress();
//    } catch (Exception e) {
//      log.warn("cannot get local host name, weird.");
//    }
// 
//    if (localhostOk && getServerHost().equals(host))
//      return true;
//    if (userMap.containsKey(host) || guestList.contains(host))
//      return true;
//    else
//      return false;
  }
 
  public boolean userIsAuthenticated(String user, String host) {
    return true;
//    for(Iterator i = userMap.values().iterator();i.hasNext();) {
//      if (((UserBean)i.next()).getName().equals(user)) {
//        // log.debug(user + "fully authenticated");
//        return true;
//      }
//    }
//    // if localhostOk is true, but the user doesn't exist and the client is
//    // on the localhost, authenticate anyway
//    if (localhostOk && getServerHost().equals(host)) {
//        // log.debug(user + " authenticated via host"); 
//        return true;
//    }
//   
//    return false;
  }
 
  // TODO make canAuthenticate() do some real authentication
  private  boolean canAuthenticate(String name, String password) {
    return true;
  }
 
  public Map getUserMap() {
    return userMap;
  }
 
  public List guestList() {
    return guestList;
  }
}
TOP

Related Classes of us.jyg.freshet.util.UserHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.