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.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
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$
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      @Override
50      public void init(Object data)
51      {
52          dataStorage = new HashMap<>();
53      }
54  
55      /**
56       * Refresh the application tool.
57       */
58      @Override
59      public void refresh()
60      {
61          // do nothing
62      }
63  
64      /**
65       * Gets the data stored under the key.  Null will be returned if the
66       * key does not exist or if null was stored under the key.
67       * <p>
68       * To check for a key with a null value use {@link #containsKey}.
69       *
70       * @param key key under which the data is stored.
71       * @return <code>Object</code> stored under the key.
72       */
73      public Object get(String key)
74      {
75          return dataStorage.get(key);
76      }
77  
78      /**
79       * Determines is a given key is stored.
80       *
81       * @param key  the key to check for
82       * @return true if the key was found
83       */
84      public boolean containsKey(String key)
85      {
86          return dataStorage.containsKey(key);
87      }
88  
89      /**
90       * Stores the data.  If the key already exists, the value will be
91       * overwritten.
92       *
93       * @param key   key under which the data will be stored.
94       * @param value data to store under the key.  Null values are allowed.
95       */
96      public void put(String key, Object value)
97      {
98          dataStorage.put(key, value);
99      }
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 }