⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usercontainer.java

📁 A Java web application, based on Struts and Hibernate, that serves as an online running log. Users m
💻 JAVA
字号:
/* @LICENSE_COPYRIGHT@ */package net.sf.irunninglog.servlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionBindingListener;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import net.sf.irunninglog.service.ICRUDService;import net.sf.irunninglog.service.IQueryService;import net.sf.irunninglog.service.ServiceFactory;/** * Utility object used to wrap any resources that might otherwise be stored in * the user's session.  This class provides a single access point for these * resources, and eliminated the need for other application components to * interact directly with a user's session.  The container for the current user * may be obtained from a request by calling the * <code>getUserContainer(HttpServletRequest)</code> method. * * <p/> * * As this class is mainly used to wrap access to the user's session, The * internal methods for initialzing and cleaning up resources are tied to the * events that bind the container to the session.  This class implements the * <code>HttpSessionBindingListener</code> to listen for these events and * respond appropriately by initializing/cleaning up any resources. * * <p/> * * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a> * @version $Revision: 1.1.1.1 $ $Date: 2005/06/23 01:48:59 $ * @since iRunningLog 1.0 * @see #getUserContainer(HttpServletRequest) * @see #valueBound(HttpSessionBindingEvent) * @see #valueUnbound(HttpSessionBindingEvent) */public final class UserContainer implements HttpSessionBindingListener {    /** <code>Log</code> instance for this class. */    private static final Log LOG = LogFactory.getLog(UserContainer.class);    /** The service implementation used to handle CRUD requests. */    private ICRUDService mCRUDService;    /** The service implementation used to handle query requests. */    private IQueryService mQueryService;    /** The current user's session. */    private HttpSession mSession;    /** The user name of the user whose session holds this container. */    private String mUserName;    /** Context under which the container is bound in the user's session. */    private static final String SESSION_CONTEXT = "UserContainer";    /**     * Get the container for the current user based on the user's request.     * This will obtain the current container from the session (creating and     * binding it to the session if needed).     *     * @param request The request (containing the user's session) being     *                processed     * @return The container bound to the current user's session     */    public static UserContainer getUserContainer(HttpServletRequest request) {        HttpSession session = request.getSession(false);        if (session == null) {            LOG.error("The request does not contain a valid session");            throw new IllegalArgumentException("The request does not contain a"                                               + " valid session");        }        UserContainer container =            (UserContainer) session.getAttribute(SESSION_CONTEXT);        if (container == null) {            container = new UserContainer(request);            if (LOG.isDebugEnabled()) {                LOG.debug("getUserContainer: Created a new container for '"                          + container.getUserName() + "'");            }            session.setAttribute(SESSION_CONTEXT, container);        }        return container;    }    /**     * Create a new container object, using a given request.     *     * @param request The request containing the current user's user name     */    private UserContainer(HttpServletRequest request) {        super();        if (request == null || request.getUserPrincipal() == null            || request.getUserPrincipal().getName() == null) {            LOG.error("Cannot create a UserContainer without a valid principal"                      + " name");            throw new NullPointerException("Cannot create a UserContainer "                                           + "without a valid principal name");        }        mUserName = request.getUserPrincipal().getName();        mSession = request.getSession(false);    }    /**     * Get the service object used to handle any CRUD (create, read, update,     * delete) actions.     *     * @return The service implementation used to perform CRUD actions     */    public ICRUDService getCRUDService() {        return mCRUDService;    }    /**     * Get the service object used to handle any query actions.     *     * @return The service implementation used to perform query actions     */    public IQueryService getQueryService() {        return mQueryService;    }    /**     * Get the current user's user name.  This will return the user name of the     * user who created this container (and in whose session this container     * is bound).     *     * @return The current user name     */    public String getUserName() {        return mUserName;    }    /**     * Log the current user out of the application.  This will invalidate the     * user's session and make it impossible for the user to interact with the     * applicaiton without logging in again.     */    public void logout() {        if (LOG.isDebugEnabled()) {            LOG.debug("logout: Logging '" + getUserName()                      + "' out of the application");        }        mSession.invalidate();    }    /**     * Perform any needed work when the container is bound to a session.     * Any resources needed by the container should be created/initialized     * here.     *     * @param event The event indicating that the container was     *              bound to a session     */    public void valueBound(HttpSessionBindingEvent event) {        if (LOG.isDebugEnabled()) {            LOG.debug("valueBound: Container for '" + mUserName                      + "' bound into session");        }        init();    }    /**     * Create and initialize any resources used by this container.     */    private void init() {        if (LOG.isDebugEnabled()) {            LOG.debug("init: Initializing container for '" + mUserName                      + "'");        }        String serviceName = ICRUDService.SERVICE_NAME;        mCRUDService =            (ICRUDService) ServiceFactory.newService(serviceName);        if (LOG.isDebugEnabled()) {            LOG.debug("init: Created CRUD Service " + mCRUDService);        }        serviceName = IQueryService.SERVICE_NAME;        mQueryService =            (IQueryService) ServiceFactory.newService(serviceName);        if (LOG.isDebugEnabled()) {            LOG.debug("init: Created Query Service " + mQueryService);        }    }    /**     * Perform any needed work when the container is unbound from a session.     * This should handle the work of cleaning up any resources used by the     * container.     *     * @param event The event indicating that the container was     *              unbound from a session     */    public void valueUnbound(HttpSessionBindingEvent event) {        if (LOG.isDebugEnabled()) {            LOG.debug("valueUnbound: Container for '" + mUserName                      + "' unbound from session");        }        cleanup();    }    /**     * Cleanup any resources used by this container.     */    private void cleanup() {        if (LOG.isDebugEnabled()) {            LOG.debug("cleanup: Cleaning up container for '" + mUserName + "'");        }        mCRUDService = null;        mQueryService = null;    }    /**     * Custom toString override to provide a meaningful <code>String</code>     * representation of this object.     *     * @return A <code>String</code> representation of this object     */    public String toString() {        StringBuffer buff = new StringBuffer();        buff.append(getClass());        buff.append("[userName='");        buff.append(getUserName());        buff.append("' CRUDService=");        buff.append(mCRUDService);        buff.append(" QueryService=");        buff.append(mQueryService);        buff.append(" session=");        buff.append(mSession);        buff.append("]");        return buff.toString();    }}

⌨️ 快捷键说明

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