1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.application.viewstate; |
20 | |
|
21 | |
import java.io.Serializable; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.List; |
25 | |
import java.util.Map; |
26 | |
import java.util.logging.Logger; |
27 | |
import javax.faces.context.FacesContext; |
28 | |
import org.apache.commons.collections.map.LRUMap; |
29 | |
import org.apache.myfaces.shared.config.MyfacesConfig; |
30 | |
import org.apache.myfaces.spi.ViewScopeProvider; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | class SerializedViewCollection implements Serializable |
36 | |
{ |
37 | 0 | private static final Logger log = Logger.getLogger(SerializedViewCollection.class.getName()); |
38 | |
|
39 | 0 | private static final Object[] EMPTY_STATES = new Object[]{null, null}; |
40 | |
|
41 | |
private static final long serialVersionUID = -3734849062185115847L; |
42 | 0 | private final List<SerializedViewKey> _keys = |
43 | |
new ArrayList<SerializedViewKey>( |
44 | |
MyfacesConfig.INIT_PARAM_NUMBER_OF_VIEWS_IN_SESSION_DEFAULT); |
45 | 0 | private final Map<SerializedViewKey, Object> _serializedViews = |
46 | |
new HashMap<SerializedViewKey, Object>(); |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | private HashMap<SerializedViewKey, String> _viewScopeIds = null; |
56 | 0 | private HashMap<String, Integer> _viewScopeIdCounts = null; |
57 | |
|
58 | 0 | private final Map<SerializedViewKey, SerializedViewKey> _precedence = |
59 | |
new HashMap<SerializedViewKey, SerializedViewKey>(); |
60 | 0 | private Map<String, SerializedViewKey> _lastWindowKeys = null; |
61 | |
|
62 | |
public void put(FacesContext context, Object state, |
63 | |
SerializedViewKey key, SerializedViewKey previousRestoredKey) |
64 | |
{ |
65 | 0 | put(context, state, key, previousRestoredKey, null, null); |
66 | 0 | } |
67 | |
|
68 | |
public synchronized void put(FacesContext context, Object state, |
69 | |
SerializedViewKey key, SerializedViewKey previousRestoredKey, |
70 | |
ViewScopeProvider viewScopeProvider, String viewScopeId) |
71 | |
{ |
72 | 0 | if (state == null) |
73 | |
{ |
74 | 0 | state = EMPTY_STATES; |
75 | |
} |
76 | 0 | else if (state instanceof Object[] && |
77 | |
((Object[])state).length == 2 && |
78 | |
((Object[])state)[0] == null && |
79 | |
((Object[])state)[1] == null) |
80 | |
{ |
81 | |
|
82 | |
|
83 | 0 | state = null; |
84 | |
} |
85 | |
|
86 | 0 | if (_serializedViews.containsKey(key)) |
87 | |
{ |
88 | |
|
89 | 0 | _serializedViews.put(key, state); |
90 | |
|
91 | 0 | while (_keys.remove(key)) |
92 | |
{ |
93 | |
|
94 | |
} |
95 | 0 | _keys.add(key); |
96 | 0 | return; |
97 | |
} |
98 | |
|
99 | 0 | Integer maxCount = getNumberOfSequentialViewsInSession(context); |
100 | 0 | if (maxCount != null) |
101 | |
{ |
102 | 0 | if (previousRestoredKey != null) |
103 | |
{ |
104 | 0 | if (!_serializedViews.isEmpty()) |
105 | |
{ |
106 | 0 | _precedence.put((SerializedViewKey) key, previousRestoredKey); |
107 | |
} |
108 | |
else |
109 | |
{ |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | 0 | previousRestoredKey = null; |
116 | |
} |
117 | |
} |
118 | |
} |
119 | 0 | _serializedViews.put(key, state); |
120 | |
|
121 | 0 | if (viewScopeProvider != null && viewScopeId != null) |
122 | |
{ |
123 | 0 | if (_viewScopeIds == null) |
124 | |
{ |
125 | 0 | _viewScopeIds = new HashMap<SerializedViewKey, String>(); |
126 | |
} |
127 | 0 | _viewScopeIds.put(key, viewScopeId); |
128 | 0 | if (_viewScopeIdCounts == null) |
129 | |
{ |
130 | 0 | _viewScopeIdCounts = new HashMap<String, Integer>(); |
131 | |
} |
132 | 0 | Integer vscount = _viewScopeIdCounts.get(viewScopeId); |
133 | 0 | vscount = (vscount == null) ? 1 : vscount + 1; |
134 | 0 | _viewScopeIdCounts.put(viewScopeId, vscount); |
135 | |
} |
136 | |
|
137 | 0 | while (_keys.remove(key)) |
138 | |
{ |
139 | |
|
140 | |
} |
141 | 0 | _keys.add(key); |
142 | |
|
143 | 0 | if (previousRestoredKey != null && maxCount != null && maxCount > 0) |
144 | |
{ |
145 | 0 | int count = 0; |
146 | 0 | SerializedViewKey previousKey = (SerializedViewKey) key; |
147 | |
do |
148 | |
{ |
149 | 0 | previousKey = _precedence.get(previousKey); |
150 | 0 | count++; |
151 | |
} |
152 | 0 | while (previousKey != null && count < maxCount); |
153 | |
|
154 | 0 | if (previousKey != null) |
155 | |
{ |
156 | 0 | SerializedViewKey keyToRemove = (SerializedViewKey) previousKey; |
157 | |
|
158 | |
|
159 | |
|
160 | |
do |
161 | |
{ |
162 | 0 | while (_keys.remove(keyToRemove)) |
163 | |
{ |
164 | |
|
165 | |
} |
166 | |
|
167 | 0 | _serializedViews.remove(keyToRemove); |
168 | |
|
169 | 0 | if (viewScopeProvider != null && _viewScopeIds != null) |
170 | |
{ |
171 | 0 | String oldViewScopeId = _viewScopeIds.remove(keyToRemove); |
172 | 0 | if (oldViewScopeId != null) |
173 | |
{ |
174 | 0 | Integer vscount = _viewScopeIdCounts.get(oldViewScopeId); |
175 | 0 | vscount = vscount - 1; |
176 | 0 | if (vscount != null && vscount.intValue() < 1) |
177 | |
{ |
178 | 0 | _viewScopeIdCounts.remove(oldViewScopeId); |
179 | 0 | viewScopeProvider.destroyViewScopeMap(context, oldViewScopeId); |
180 | |
} |
181 | |
else |
182 | |
{ |
183 | 0 | _viewScopeIdCounts.put(oldViewScopeId, vscount); |
184 | |
} |
185 | |
} |
186 | |
} |
187 | |
|
188 | 0 | keyToRemove = _precedence.remove(keyToRemove); |
189 | |
} |
190 | 0 | while (keyToRemove != null); |
191 | |
} |
192 | |
} |
193 | 0 | int views = getNumberOfViewsInSession(context); |
194 | 0 | while (_keys.size() > views) |
195 | |
{ |
196 | 0 | key = _keys.remove(0); |
197 | 0 | if (maxCount != null && maxCount > 0) |
198 | |
{ |
199 | 0 | SerializedViewKey keyToRemove = (SerializedViewKey) key; |
200 | |
|
201 | |
|
202 | |
|
203 | |
do |
204 | |
{ |
205 | 0 | keyToRemove = _precedence.remove(keyToRemove); |
206 | |
} |
207 | 0 | while (keyToRemove != null); |
208 | |
} |
209 | |
|
210 | 0 | _serializedViews.remove(key); |
211 | |
|
212 | 0 | if (viewScopeProvider != null && _viewScopeIds != null) |
213 | |
{ |
214 | 0 | String oldViewScopeId = _viewScopeIds.remove(key); |
215 | 0 | if (oldViewScopeId != null) |
216 | |
{ |
217 | 0 | Integer vscount = _viewScopeIdCounts.get(oldViewScopeId); |
218 | 0 | vscount = vscount - 1; |
219 | 0 | if (vscount != null && vscount.intValue() < 1) |
220 | |
{ |
221 | 0 | _viewScopeIdCounts.remove(oldViewScopeId); |
222 | 0 | viewScopeProvider.destroyViewScopeMap(context, oldViewScopeId); |
223 | |
} |
224 | |
else |
225 | |
{ |
226 | 0 | _viewScopeIdCounts.put(oldViewScopeId, vscount); |
227 | |
} |
228 | |
} |
229 | 0 | } |
230 | |
} |
231 | 0 | } |
232 | |
|
233 | |
protected Integer getNumberOfSequentialViewsInSession(FacesContext context) |
234 | |
{ |
235 | 0 | return MyfacesConfig.getCurrentInstance(context.getExternalContext()).getNumberOfSequentialViewsInSession(); |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
protected int getNumberOfViewsInSession(FacesContext context) |
245 | |
{ |
246 | 0 | return MyfacesConfig.getCurrentInstance(context.getExternalContext()).getNumberOfViewsInSession(); |
247 | |
} |
248 | |
|
249 | |
public synchronized void putLastWindowKey(FacesContext context, String id, SerializedViewKey key) |
250 | |
{ |
251 | 0 | if (_lastWindowKeys == null) |
252 | |
{ |
253 | 0 | Integer i = getNumberOfSequentialViewsInSession(context); |
254 | 0 | int j = getNumberOfViewsInSession(context); |
255 | 0 | if (i != null && i.intValue() > 0) |
256 | |
{ |
257 | 0 | _lastWindowKeys = new LRUMap((j / i.intValue()) + 1); |
258 | |
} |
259 | |
else |
260 | |
{ |
261 | 0 | _lastWindowKeys = new LRUMap(j + 1); |
262 | |
} |
263 | |
} |
264 | 0 | _lastWindowKeys.put(id, key); |
265 | 0 | } |
266 | |
|
267 | |
public SerializedViewKey getLastWindowKey(FacesContext context, String id) |
268 | |
{ |
269 | 0 | if (_lastWindowKeys != null) |
270 | |
{ |
271 | 0 | return _lastWindowKeys.get(id); |
272 | |
} |
273 | 0 | return null; |
274 | |
} |
275 | |
|
276 | |
public Object get(SerializedViewKey key) |
277 | |
{ |
278 | 0 | Object value = _serializedViews.get(key); |
279 | 0 | if (value == null) |
280 | |
{ |
281 | 0 | if (_serializedViews.containsKey(key)) |
282 | |
{ |
283 | 0 | return EMPTY_STATES; |
284 | |
} |
285 | |
} |
286 | 0 | else if (value instanceof Object[] && |
287 | |
((Object[])value).length == 2 && |
288 | |
((Object[])value)[0] == null && |
289 | |
((Object[])value)[1] == null) |
290 | |
{ |
291 | |
|
292 | 0 | return null; |
293 | |
} |
294 | 0 | return value; |
295 | |
} |
296 | |
} |