001package 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
024import java.util.HashMap;
025import java.util.Iterator;
026import java.util.Map;
027
028import 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$
038 */
039public 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    @Override
050    public void init(Object data)
051    {
052        dataStorage = new HashMap<>();
053    }
054
055    /**
056     * Refresh the application tool.
057     */
058    @Override
059    public void refresh()
060    {
061        // do nothing
062    }
063
064    /**
065     * Gets the data stored under the key.  Null will be returned if the
066     * key does not exist or if null was stored under the key.
067     * <p>
068     * To check for a key with a null value use {@link #containsKey}.
069     *
070     * @param key key under which the data is stored.
071     * @return <code>Object</code> stored under the key.
072     */
073    public Object get(String key)
074    {
075        return dataStorage.get(key);
076    }
077
078    /**
079     * Determines is a given key is stored.
080     *
081     * @param key  the key to check for
082     * @return true if the key was found
083     */
084    public boolean containsKey(String key)
085    {
086        return dataStorage.containsKey(key);
087    }
088
089    /**
090     * Stores the data.  If the key already exists, the value will be
091     * overwritten.
092     *
093     * @param key   key under which the data will be stored.
094     * @param value data to store under the key.  Null values are allowed.
095     */
096    public void put(String key, Object value)
097    {
098        dataStorage.put(key, value);
099    }
100
101    /**
102     * Clears all data
103     */
104    public void clear()
105    {
106        dataStorage.clear();
107    }
108
109    /**
110     * Gets a iterator for the keys.
111     *
112     * @return <code>Iterator</code> for the keys
113     */
114    public Iterator<String> iterator()
115    {
116        return dataStorage.keySet().iterator();
117    }
118}