basestatemanagerservice.java

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

JAVA
551
字号
            state = Collections.synchronizedMap(new HashMap());
            addState(key, state);
        }

        // get the old, if any
        Object old = getAttribute(key, name);
        
        // store the new
        state.put(name, value);

        // if there was an old value, unbind it
        if (old != null)
        {
            unBindAttributeValue(key, name, old);
        }

        // bind the new
        bindAttributeValue(key, name, value);

    }   // setAttribute

    /**
    * Remove the named state attribute of the keyed state, if it exists.
    * @param key The state key.
    * @param name The attribute name.
    */
    public void removeAttribute( String key, String name )
    {
        Map state = getState(key);
        if (state == null) return;

        // get the old, if any
        Object old = getAttribute(key, name);

        // remove
        state.remove(name);
        
        // if the state is now empty, remove it
        if (state.isEmpty())
        {
            removeState(key);
        }

        // if there was an old value, unbind it
        if (old != null)
        {
            unBindAttributeValue(key, name, old);
        }

    }   // removeAttribute

    /**
    * Remove all state attribute of the keyed state.
    * @param key The state key.
    */
    public void clear( String key )
    {
        Map state = getState(key);
        if (state == null) return;

        // notify all attribute and clear the state
        retireAttributes(key, state);
        
        // and forget about it
        removeState(key);

    }   // clear

    /**
    * Access an array of all names of attributes stored in the keyed state.
    * @param key The state key.
    * @return An array of all names of attributes stored in the keyed state.
    */
    public String[] getAttributeNames( String key )
    {
        Map state = (Map) getState(key);
        if (state == null) return null;
        if (state.size() == 0) return null;

        // put the names into an array for return
        return (String[]) state.keySet().toArray(new String[state.size()]);

    }   // getAttributeNames

    /**
    * Access an SessionState object with the given key.
    * @param key The SessionState key.
    * @return an SessionState object with the given key.
    */
    public SessionState getSessionState( String key )
    {
        return new MySessionState(key, this);

    }   // getSessionState

    /**
    * Access the SessionState object associated with the current request's http session.
	* The session id is used as the key.
    * @return an SessionState object associated with the current request's http session.
    */
    public SessionState getCurrentSessionState()
    {
        HttpSession session = (HttpSession) m_httpSessions.get(Thread.currentThread());
        if (session == null) return null;

        return getSessionState(session.getId());

    }   // getCurrentSessionState

    /**
    * Access the SessionState object associated with the current request's http session with the given key.
	* @param key The string to add to the session id to form the SessionState key.
    * @return an SessionState object associated with the current request's http session with the given key.
    */
    public SessionState getCurrentSessionState( String key )
    {
        HttpSession session = (HttpSession) m_httpSessions.get(Thread.currentThread());
        if (session == null) return null;

        return getSessionState(session.getId() + key);

    }   // getCurrentSessionState

    /**
    * Retire, forget about and clean up all states that start with the given key.
    * @param keyStart The beginning of the key of the states to clean up.
    */
    public synchronized void retireState( String keyStart )
    {
        // get the current state keys into an array
        String keys[] = getStateKeys(keyStart);
        if (keys == null) return;
        
        // clear them
        for (int i = 0; i < keys.length; i++)
        {
            clear(keys[i]);
        }

    }   // retireState

    /**
    * Set the "current" context for this thread -
    * Call this at the start of each request, and call %%% at the end.
    * getCurrentSession() uses this for the session state key.
    * @param session the HttpSession of the current request.
    */
    public void setCurrentContext( HttpSession session )
    {
        // store the session associated with this thread
        m_httpSessions.put(Thread.currentThread(), session);

    }   // setCurrentContext

    /**
    * Clear the "current context for this thread -
    * Call at the end of each request, balanced with calls to setCurrentContext()
    */
    public void clearCurrentContext()
    {
        // clear the session associated with this thread
        m_httpSessions.remove(Thread.currentThread());

    }    // clearCurrentContext

    /*******************************************************************************
    * SessionState implementation
    *******************************************************************************/

    /**
    * A SessionState implementation, as covers to this service, storing the key.
    */
    private class MySessionState
        implements SessionState
    {
        /** The state key. */
        private String m_key = null;

        /** The StateManagerService object. */
        private BaseStateManagerService m_service = null;

        /**
        * Construct.
        * @param key The state key.
        * @param service The JetspeedStateManagerService instance.
        */
        public MySessionState( String key,
                                BaseStateManagerService service)
        {
            m_key = key;
            m_service = service;

        }   // MySessionState

        /**
        * Access the named attribute.
        * @param name The attribute name.
        * @return The named attribute value.
        */
        public Object getAttribute( String name )
        {
            return m_service.getAttribute(m_key, name);

        }   // getAttribute

        /**
        * Set the named attribute value to the provided object.
        * @param name The attribute name.
        * @param value The value of the attribute (any object type).
        */
        public void setAttribute( String name, Object value )
        {
            m_service.setAttribute(m_key, name, value);

        }   // setAttribute

        /**
        * Remove the named attribute, if it exists.
        * @param name The attribute name.
        */
        public void removeAttribute( String name )
        {
            m_service.removeAttribute(m_key, name);

        }   // removeAttribute

        /**
        * Remove all attributes.
        */
        public void clear()
        {
            m_service.clear(m_key);

        }   // clear

        /**
        * Access an array of all names of attributes stored in the SessionState.
        * @return An array of all names of attribute stored in the SessionState.
        */
        public String[] getAttributeNames()
        {
            return m_service.getAttributeNames(m_key);

        }   // getAttributeNames

        /**
        * Access the full unique StateManager key for the SessionState.
        * @return the full unique StateManager key for the SessionState.
        */
        public String getKey()
        {
            return m_key;

        }   // getKey

        /**
        * Retire, forget about and clean up this state.
        */
        public void retire()
        {
            m_service.retireState(m_key);

        }   // retire

    }   // class MySessionState

}   // BaseStateManagerService

/**********************************************************************************
*
* $Header: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/statemanager/BaseStateManagerService.java,v 1.5 2004/02/23 03:38:28 jford Exp $
*
**********************************************************************************/

⌨️ 快捷键说明

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