usercontainer.java

来自「我在加拿大学习的一个比较复杂的在线银行程序.」· Java 代码 · 共 116 行

JAVA
116
字号
package com.ebusiness.ebank.security;/** * <p>Title: </p> * <p>Description: This class acts as a global container to keep track of the *                  users in eBank system. When a new user uses the system first time, *                  the new user's information will be loaded and added to the container. *                  The user's information will be cached *                  in this object until it is removed on the scheduled time or *                  when the user's session is being destroyed.</p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company:  eBusiness Inc., All right reserved</p> * @author unascribed * @version 1.0 */import java.util.HashSet;import java.util.HashMap;import java.util.Collections;import java.util.Date;import javax.servlet.http.HttpServletRequest;import javax.ejb.SessionContext;import com.ebusiness.ebank.exception.SystemException;public class UserContainer{    private static HashMap container = new HashMap();    private static String[] ebankRoles;    private static long refreshInterval = 3600000 * 12; //default to 12 hour    //This method is called by web tier    public static UserProfile getUserProfile(HttpServletRequest request) throws SystemException    {        String userID = request.getRemoteUser();        UserProfile user = (UserProfile)container.get(userID);        if (user != null)        {            if (!needRefresh(user))                return user;        }        HashSet roles = new HashSet();        for (int i = 0; i < ebankRoles.length; i++)        {            if (request.isUserInRole(ebankRoles[i]))                roles.add(ebankRoles[i]);        }        user = UserLoader.getInstance().load(userID, (String[])roles.toArray(new String[roles.size()]));        container.put(userID, user);        return user;    }    //This method is called by EJB tier    public static UserProfile getUserProfile(SessionContext ctx) throws SystemException    {        String userID = ctx.getCallerPrincipal().getName();        UserProfile user = (UserProfile)container.get(userID);        if (user != null)        {            if (!needRefresh(user))                return user;        }        HashSet roles = new HashSet();        for (int i = 0; i < ebankRoles.length; i++)        {            if (ctx.isCallerInRole(ebankRoles[i]))                roles.add(ebankRoles[i]);        }        user = UserLoader.getInstance().load(userID, (String[])roles.toArray(new String[roles.size()]));        //user = new UserProfile(userID, (String[])roles.toArray(new String[roles.size()]));        container.put(userID, user);        return user;    }    /**     * This method will only be called by sessionDestroyed method in a HttpSessionListener object,     * to release user from the container when the user's session is being destroyed.     * No other objects shall call this method     */    protected static void removeUser(String userID)    {        container.remove(userID);    }    //This method will only be called by a special object on a scheduled time    //to release users and refresh the user's information. No other objects shall call this method    protected static void removeAll()    {        container.clear();    }    //This method is called on the initialization of SignOnFilter    protected static void initEbankRoles(String[] roles)    {        ebankRoles = roles;    }    //This method is called on the initialization of SignOnFilter    protected static void initRefreshInterval(long interval)    {        refreshInterval = interval;    }    private static boolean needRefresh(UserProfile user)    {        Date currDate = new Date();        return currDate.getTime() - user.getCreationDate().getTime() > refreshInterval;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?