View Javadoc

    1   package org.apache.turbine.services.pull.util;
    2
    3
    4   /*
    5    * Licensed to the Apache Software Foundation (ASF) under one
    6    * or more contributor license agreements.  See the NOTICE file
    7    * distributed with this work for additional information
    8    * regarding copyright ownership.  The ASF licenses this file
    9    * to you under the Apache License, Version 2.0 (the
    10   * "License"); you may not use this file except in compliance
    11   * with the License.  You may obtain a copy of the License at
    12   *
    13   *   http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing,
    16   * software distributed under the License is distributed on an
    17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    18   * KIND, either express or implied.  See the License for the
    19   * specific language governing permissions and limitations
    20   * under the License.
    21   */
    22
    23
    24  import java.util.Map;
    25  import java.util.HashMap;
    26  import java.util.Iterator;
    27
    28  import org.apache.turbine.services.pull.ApplicationTool;
    29
    30  /**
    31   * Pull tool designed to be used in the session scope for storage of
    32   * temporary data.  This tool should eliminate the need for the
    33   * {@link org.apache.turbine.om.security.User#setTemp} and
    34   * {@link org.apache.turbine.om.security.User#getTemp} methods.
    35   *
    36   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    37   * @version $Id: SessionData.java 1066955 2011-02-03 20:46:29Z ludwig $
    38   */
    39  public class SessionData implements ApplicationTool
    40  {
    41      /** Storage of user defined data */
    42      private Map<String, Object> dataStorage;
    43
    44      /**
    45       * Initialize the application tool.
    46       *
    47       * @param data initialization data
    48       */
    49      public void init(Object data)
    50      {
    51          dataStorage = new HashMap<String, Object>();
    52      }
    53
    54      /**
    55       * Refresh the application tool.
    56       */
    57      public void refresh()
    58      {
    59          // do nothing
    60      }
    61
    62      /**
    63       * Gets the data stored under the key.  Null will be returned if the
    64       * key does not exist or if null was stored under the key.
    65       * <p>
    66       * To check for a key with a null value use {@link #containsKey}.
    67       *
    68       * @param key key under which the data is stored.
    69       * @return <code>Object</code> stored under the key.
    70       */
    71      public Object get(String key)
    72      {
    73          return dataStorage.get(key);
    74      }
    75
    76      /**
    77       * Determines is a given key is stored.
    78       *
    79       * @param key  the key to check for
    80       * @return true if the key was found
    81       */
    82      public boolean containsKey(String key)
    83      {
    84          return dataStorage.containsKey(key);
    85      }
    86
    87      /**
    88       * Stores the data.  If the key already exists, the value will be
    89       * overwritten.
    90       *
    91       * @param key   key under which the data will be stored.
    92       * @param value data to store under the key.  Null values are allowed.
    93       */
    94      public void put(String key, Object value)
    95      {
    96          dataStorage.put(key, value);
    97      }
    98
    99      /**
    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 }