001    package org.apache.turbine.services.pull.util;
002    
003    
004    /*
005     * Licensed to the Apache Software Foundation (ASF) under one
006     * or more contributor license agreements.  See the NOTICE file
007     * distributed with this work for additional information
008     * regarding copyright ownership.  The ASF licenses this file
009     * to you under the Apache License, Version 2.0 (the
010     * "License"); you may not use this file except in compliance
011     * with the License.  You may obtain a copy of the License at
012     *
013     *   http://www.apache.org/licenses/LICENSE-2.0
014     *
015     * Unless required by applicable law or agreed to in writing,
016     * software distributed under the License is distributed on an
017     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018     * KIND, either express or implied.  See the License for the
019     * specific language governing permissions and limitations
020     * under the License.
021     */
022    
023    
024    import java.util.Map;
025    import java.util.HashMap;
026    import java.util.Iterator;
027    
028    import org.apache.turbine.services.pull.ApplicationTool;
029    
030    /**
031     * Pull tool designed to be used in the session scope for storage of
032     * temporary data.  This tool should eliminate the need for the
033     * {@link org.apache.turbine.om.security.User#setTemp} and
034     * {@link org.apache.turbine.om.security.User#getTemp} methods.
035     *
036     * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
037     * @version $Id: SessionData.java 1066955 2011-02-03 20:46:29Z ludwig $
038     */
039    public class SessionData implements ApplicationTool
040    {
041        /** Storage of user defined data */
042        private Map<String, Object> dataStorage;
043    
044        /**
045         * Initialize the application tool.
046         *
047         * @param data initialization data
048         */
049        public void init(Object data)
050        {
051            dataStorage = new HashMap<String, Object>();
052        }
053    
054        /**
055         * Refresh the application tool.
056         */
057        public void refresh()
058        {
059            // do nothing
060        }
061    
062        /**
063         * Gets the data stored under the key.  Null will be returned if the
064         * key does not exist or if null was stored under the key.
065         * <p>
066         * To check for a key with a null value use {@link #containsKey}.
067         *
068         * @param key key under which the data is stored.
069         * @return <code>Object</code> stored under the key.
070         */
071        public Object get(String key)
072        {
073            return dataStorage.get(key);
074        }
075    
076        /**
077         * Determines is a given key is stored.
078         *
079         * @param key  the key to check for
080         * @return true if the key was found
081         */
082        public boolean containsKey(String key)
083        {
084            return dataStorage.containsKey(key);
085        }
086    
087        /**
088         * Stores the data.  If the key already exists, the value will be
089         * overwritten.
090         *
091         * @param key   key under which the data will be stored.
092         * @param value data to store under the key.  Null values are allowed.
093         */
094        public void put(String key, Object value)
095        {
096            dataStorage.put(key, value);
097        }
098    
099        /**
100         * Clears all data
101         */
102        public void clear()
103        {
104            dataStorage.clear();
105        }
106    
107        /**
108         * Gets a iterator for the keys.
109         *
110         * @return <code>Iterator</code> for the keys
111         */
112        public Iterator<String> iterator()
113        {
114            return dataStorage.keySet().iterator();
115        }
116    }