KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > statemanager > JetspeedStateManagerService


1 /*
2  * Copyright 2000-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 // package
18
package org.apache.jetspeed.services.statemanager;
19
20 // imports
21
import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Vector JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.jetspeed.services.statemanager.BaseStateManagerService;
29
30 /**
31 * <p>JetspeedStateManagerService is an implementation of the BaseStateManagerService
32 * which manages the states stored in a local Map (synchronized HashMap).</p>
33 * <p>Note: This implementation of the StateManagerService is independent of all other
34 * services; but it has no automatic way to retire no longer used state. If the
35 * application does not explicitly retire the states created, they will hang around
36 * forever. (see clear() and retireState() of the StateManagerService).</p>
37 * @version $Revision: 1.6 $
38 * @see org.apache.jetspeed.services.statemanager.BaseStateManagerService
39 * @see org.apache.jetspeed.services.statemanager.StateManagerService
40 * @see org.apache.jetspeed.services.statemanager.SessionState
41 * @author <a HREF="mailto:ggolden@apache.org">Glenn R. Golden</a>
42 */

43 public class JetspeedStateManagerService
44     extends BaseStateManagerService
45 {
46     /** Store each set of state parameters by state key
47         (each is a HashMap keyed by parameter name) */

48     private Map JavaDoc m_states = null;
49
50     /**
51     * Initialize the states storage.
52     */

53     protected void initStates()
54     {
55         // create our states map synchronized
56
m_states = Collections.synchronizedMap(new HashMap JavaDoc());
57
58     } // initStates
59

60     /**
61     * Cleanup the states storage.
62     */

63     protected void shutdownStates()
64     {
65         m_states.clear();
66         m_states = null;
67
68     } // shutdownStates
69

70     /**
71     * Access the Map which is the set of attributes for a state.
72     * @param key The state key.
73     * @return The Map which is the set of attributes for a state.
74     */

75     protected Map JavaDoc getState( String JavaDoc key )
76     {
77         return (Map JavaDoc) m_states.get(key);
78
79     } // getState
80

81     /**
82     * Add a new state to the states we are managing.
83     * @param key The state key.
84     * @param state The Map which is the set of attributes for the state.
85     */

86     protected void addState( String JavaDoc key, Map JavaDoc state )
87     {
88         m_states.put(key, state);
89
90     } // addState
91

92     /**
93     * Remove a state from the states we are managing.
94     * @param key The state key.
95     */

96     protected void removeState( String JavaDoc key )
97     {
98         m_states.remove(key);
99
100     } // removeState
101

102     /**
103     * Access an array of the keys of all states managed, those that start with the parameter.
104     * @param start The starting string used to select the keys.
105     * @return an array of the keys of all states managed.
106     */

107     protected String JavaDoc[] getStateKeys( String JavaDoc start )
108     {
109         // collect for return
110
Vector JavaDoc rv = new Vector JavaDoc();
111
112         // get the entire set of keys to iterate over
113
Set JavaDoc allStateKeys = m_states.keySet();
114         synchronized (m_states)
115         {
116             Iterator JavaDoc i = allStateKeys.iterator();
117             while (i.hasNext())
118             {
119                 String JavaDoc key = (String JavaDoc) i.next();
120                 
121                 // if this matches our pattern
122
if (key.startsWith(start))
123                 {
124                     rv.add(key);
125                 }
126             }
127         }
128
129         if (rv.size() == 0) return null;
130
131         return (String JavaDoc[]) rv.toArray(new String JavaDoc[rv.size()]);
132
133     } // getStateKeys
134

135 } // JetspeedStateManagerService
136

137 /**********************************************************************************
138 *
139 * $Header: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/statemanager/JetspeedStateManagerService.java,v 1.6 2004/02/23 03:38:28 jford Exp $
140 *
141 **********************************************************************************/

142
143
Popular Tags