basejetspeeduser.java

来自「jetspeed源代码」· Java 代码 · 共 783 行 · 第 1/2 页

JAVA
783
字号
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jetspeed.om.security;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Hashtable;
import javax.servlet.http.HttpSessionBindingEvent;

import org.apache.jetspeed.services.JetspeedUserManagement;
import org.apache.jetspeed.services.JetspeedAuthentication;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.services.resources.JetspeedResources;

import org.apache.turbine.om.security.User;
import org.apache.turbine.util.ObjectUtils;

/**
 * The default Jetspeed implementation of User interface.
 *
 * This basic implementation contains the functionality that is
 * expected to be common among all User implementations.
 * You are welcome to extend this class if you wish to have
 * custom functionality in your user objects (like accessor methods
 * for custom attributes). 
 *
 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a> 
 * @version $Id: BaseJetspeedUser.java,v 1.11 2004/02/23 03:14:12 jford Exp $
 */
public class BaseJetspeedUser /*extends SecurityObject*/ implements JetspeedUser
{
    /** The date on which the user account was created. */
    private Date createDate = null;
    /** The date on which the user last accessed the application. */
    private Date lastAccessDate = null;

    /** This is data that will survive a servlet engine restart. */
    private Hashtable permStorage = null;

    /** This is data that will not survive a servlet engine restart. */
    private Hashtable tempStorage = null;

    protected String name = "";

    protected boolean isNew = true;

    
    /**
     * Static initialization of the logger for this class
     */    
    private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedUser.class.getName());    
    
    /**
     * Constructor.
     * Create a new User and set the createDate.
     */
    public BaseJetspeedUser()
    {
        createDate = new Date();
        tempStorage = new Hashtable(10);
        permStorage = new Hashtable(10);
        setHasLoggedIn(Boolean.FALSE);
        setDisabled(false);
        isNew = true;
    }


    /**
      * Returns the primary principle for this User, the user id.
      *
      * @return the user id.
      */
    public String getUserId()
    {
        String tmp = null;
        try
        {
            tmp = (String) getPerm (JetspeedUser.USER_ID);
            if (tmp != null && tmp.length() == 0)
            {
                tmp = null;
            }
        }
        catch (Exception e)
        {
            logger.error("getUserId(): " + e.getMessage(), e);
        }
        return tmp;
    }

    public void setUserId(String id)
    {
        if (getUserId() == null)
        {
            setPerm(JetspeedUser.USER_ID, id);
        }
    }

    /**
     * Gets the access counter for a user during a session.
     *
     * @return The access counter for the user for the session.
     */
    public int getAccessCounterForSession()
    {
        int accessCounter = 0;
        try
        {
            Integer temp = (Integer) getTemp(User.SESSION_ACCESS_COUNTER);
            if(temp != null)
            {
                accessCounter = temp.intValue();
            }
        }
        catch (Exception e)
        {
            logger.debug("getAccessCounterForSession(): " + e.getMessage(), e);
        }
        
        return accessCounter;
    }

    /**
     * Gets the access counter for a user from perm storage.
     *
     * @return The access counter for the user.
     */
    public int getAccessCounter()
    {
        int accessCounter = 0;
        try
        {
            Integer temp = (Integer) getPerm(User.ACCESS_COUNTER);
            if(temp != null)
            {
                accessCounter = temp.intValue();
            }
        }
        catch (Exception e)
        {
            logger.debug("getAccessCounter(): " + e.getMessage(), e);
        }
        return accessCounter;
    }

    /**
     * Gets the create date for this User.  This is the time at which
     * the user object was created.
     *
     * @return A Java Date with the date of creation for the user.
     */
    public java.util.Date getCreateDate()
    {
        return createDate;
    }

    /**
     * Gets the last access date for this User.  This is the last time
     * that the user object was referenced.
     *
     * @return A Java Date with the last access date for the user.
     */
    public java.util.Date getLastAccessDate()
    {
        if (lastAccessDate == null)
        {
            setLastAccessDate();
        }
        return lastAccessDate;
    }

    /**
     * Get last login date/time for this user.
     *
     * @return A Java Date with the last login date for the user.
     */
    public java.util.Date getLastLogin()
    {
        return (java.util.Date) getPerm(User.LAST_LOGIN);
    }

    /**
     * Get password for this user.
     *
     * @return A String with the password for the user.
     */
    public String getPassword()
    {
        return (String) getPerm(User.PASSWORD);
    }

    /**
     * Get an object from permanent storage.
     *
     * @param name The object's name.
     * @return An Object with the given name.
     */
    public Object getPerm(String name)
    {
        return permStorage.get(name);
    }

    /**
     * Get an object from permanent storage; return default if value
     * is null.
     *
     * @param name The object's name.
     * @param def A default value to return.
     * @return An Object with the given name.
     */
    public Object getPerm(String name, Object def)
    {
        try
        {
            Object val = permStorage.get (name);
            return (val == null ? def : val);
        }
        catch (Exception e)
        {
            logger.error("getPerm(" + name + "): " + e.getMessage(), e);
            return def;
        }
    }

    /**
     * This should only be used in the case where we want to save the
     * data to the database.
     *
     * @return A Hashtable.
     */
    public Hashtable getPermStorage()
    {
        if (this.permStorage == null)
        {
            this.permStorage = new Hashtable();
        }
        return this.permStorage;
    }

    /**
     * Get an object from temporary storage.
     *
     * @param name The object's name.
     * @return An Object with the given name.
     */
    public Object getTemp(String name)
    {
        return tempStorage.get(name);
    }

    /**
     * Get an object from temporary storage; return default if value
     * is null.
     *
     * @param name The object's name.
     * @param def A default value to return.
     * @return An Object with the given name.
     */
    public Object getTemp(String name, Object def)
    {
        Object val;
        try
        {
            val = tempStorage.get(name);
            if (val == null)
            {
                val = def;
            }
        }
        catch (Exception e)
        {
            logger.error("getTemp(" + name + "): " + e.getMessage(), e);
            val = def;
        }
        return val;
    }

    /**
     * Returns the username for this user.  If this is defined, then
     * the user is considered logged in.
     *
     * @return A String with the username.
     */
    public String getUserName()
    {
        String tmp = null;
        try
        {
            tmp = (String) getPerm (User.USERNAME);
            if ( tmp.length() == 0 )
            {
                tmp = null;
            }
        }
        catch (Exception e)
        {
            logger.error("getUserName(): " + e.getMessage(), e);
        }
        return tmp;
    }

    /**
     * Returns the first name for this user.  If this is defined, then
     * the user is considered logged in.
     *
     * @return A String with the user's first name.
     */
    public String getFirstName()
    {
        String tmp = null;
        try
        {
            tmp = (String) getPerm (User.FIRST_NAME);
            if (tmp.length() == 0)
            {
                tmp = null;
            }
        }
        catch (Exception e)
        {
            logger.error("getFirstName(): " + e.getMessage(), e);
        }
        return tmp;
    }

    /**
     * Returns the last name for this user.  If this is defined, then
     * the user is considered logged in.
     *
     * @return A String with the user's last name.
     */
    public String getLastName()
    {
        String tmp = null;
        try
        {
            tmp = (String) getPerm (User.LAST_NAME);
            if (tmp.length() == 0)
                tmp = null;
        }
        catch (Exception e)
        {
            logger.error("getLastName(): " + e.getMessage(), e);
        }
        return tmp;
    }

    /**
     * The user is considered logged in if they have not timed out.
     *
     * @return Whether the user has logged in.
     */
    public boolean hasLoggedIn()
    {
        Boolean loggedIn = getHasLoggedIn();
        return (loggedIn != null && loggedIn.booleanValue());
    }

    /**
     * Returns the email address for this user.
     *
     * @return A String with the user's email address.
     */
    public String getEmail()
    {
        return (String)getPerm(User.EMAIL);
    }

    /**
     * Increments the permanent hit counter for the user.
     */
    public void incrementAccessCounter()
    {
        setAccessCounter(getAccessCounter() + 1);
    }

⌨️ 快捷键说明

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