1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.interceptor;
23
24 import java.io.Serializable;
25 import java.util.IdentityHashMap;
26 import java.util.Map;
27
28 import org.apache.struts2.ServletActionContext;
29 import org.apache.struts2.StrutsException;
30 import org.apache.struts2.dispatcher.SessionMap;
31
32 import com.opensymphony.xwork2.ActionContext;
33 import com.opensymphony.xwork2.ActionInvocation;
34 import com.opensymphony.xwork2.ActionProxy;
35 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
36 import com.opensymphony.xwork2.interceptor.PreResultListener;
37 import com.opensymphony.xwork2.util.ValueStack;
38 import com.opensymphony.xwork2.util.logging.Logger;
39 import com.opensymphony.xwork2.util.logging.LoggerFactory;
40
41 /***
42 * <!-- START SNIPPET: description -->
43 *
44 * This is designed to solve a few simple issues related to wizard-like functionality in Struts. One of those issues is
45 * that some applications have a application-wide parameters commonly used, such <i>pageLen</i> (used for records per
46 * page). Rather than requiring that each action check if such parameters are supplied, this interceptor can look for
47 * specified parameters and pull them out of the session.
48 *
49 * <p/> This works by setting listed properties at action start with values from session/application attributes keyed
50 * after the action's class, the action's name, or any supplied key. After action is executed all the listed properties
51 * are taken back and put in session or application context.
52 *
53 * <p/> To make sure that each execution of the action is consistent it makes use of session-level locking. This way it
54 * guarantees that each action execution is atomic at the session level. It doesn't guarantee application level
55 * consistency however there has yet to be enough reasons to do so. Application level consistency would also be a big
56 * performance overkill.
57 *
58 * <p/> Note that this interceptor takes a snapshot of action properties just before result is presented (using a {@link
59 * PreResultListener}), rather than after action is invoked. There is a reason for that: At this moment we know that
60 * action's state is "complete" as it's values may depend on the rest of the stack and specifically - on the values of
61 * nested interceptors.
62 *
63 * <!-- END SNIPPET: description -->
64 *
65 * <p/> <u>Interceptor parameters:</u>
66 *
67 * <!-- START SNIPPET: parameters -->
68 *
69 * <ul>
70 *
71 * <li>session - a list of action properties to be bound to session scope</li>
72 *
73 * <li>application - a list of action properties to be bound to application scope</li>
74 *
75 * <li>key - a session/application attribute key prefix, can contain following values:</li>
76 *
77 * <ul>
78 *
79 * <li>CLASS - that creates a unique key prefix based on action namespace and action class, it's a default value</li>
80 *
81 * <li>ACTION - creates a unique key prefix based on action namespace and action name</li>
82 *
83 * <li>any other value is taken literally as key prefix</li>
84 *
85 * </ul>
86 *
87 * <li>type - with one of the following</li>
88 *
89 * <ul>
90 *
91 * <li>start - means it's a start action of the wizard-like action sequence and all session scoped properties are reset
92 * to their defaults</li>
93 *
94 * <li>end - means that session scoped properties are removed from session after action is run</li>
95 *
96 * <li>any other value or no value means that it's in-the-middle action that is set with session properties before it's
97 * executed, and it's properties are put back to session after execution</li>
98 *
99 * </ul>
100 *
101 * <li>sessionReset - name of a parameter (defaults to 'session.reset') which if set, causes all session values to be reset to action's default values or application
102 * scope values, note that it is similar to type="start" and in fact it does the same, but in our team it is sometimes
103 * semantically preferred. We use session scope in two patterns - sometimes there are wizard-like action sequences that
104 * have start and end, and sometimes we just want simply reset current session values.</li>
105 *
106 * <li>reset - boolean, defaults to false, if set, it has the same effect as setting all session values to be reset to action's default values or application.</li>
107 *
108 * <li>autoCreateSession - boolean value, sets if the session should be automatically created.</li>
109 * </ul>
110 *
111 * <!-- END SNIPPET: parameters -->
112 *
113 * <p/> <u>Extending the interceptor:</u>
114 *
115 * <p/>
116 *
117 * <!-- START SNIPPET: extending -->
118 *
119 * There are no know extension points for this interceptor.
120 *
121 * <!-- END SNIPPET: extending -->
122 *
123 * <p/> <u>Example code:</u>
124 *
125 * <pre>
126 * <!-- START SNIPPET: example -->
127 * <!-- As the filter and orderBy parameters are common for all my browse-type actions,
128 * you can move control to the scope interceptor. In the session parameter you can list
129 * action properties that are going to be automatically managed over session. You can
130 * do the same for application-scoped variables-->
131 * <action name="someAction" class="com.examples.SomeAction">
132 * <interceptor-ref name="basicStack"/>
133 * <interceptor-ref name="hibernate"/>
134 * <interceptor-ref name="scope">
135 * <param name="session">filter,orderBy</param>
136 * <param name="autoCreateSession">true</param>
137 * </interceptor-ref>
138 * <result name="success">good_result.ftl</result>
139 * </action>
140 * <!-- END SNIPPET: example -->
141 * </pre>
142 *
143 */
144 public class ScopeInterceptor extends AbstractInterceptor implements PreResultListener {
145
146 private static final long serialVersionUID = 9120762699600054395L;
147
148 private static final Logger LOG = LoggerFactory.getLogger(ScopeInterceptor.class);
149
150 private String[] application = null;
151 private String[] session = null;
152 private String key;
153 private String type = null;
154 private boolean autoCreateSession = true;
155 private String sessionReset = "session.reset";
156 private boolean reset = false;
157
158 /***
159 * Sets a list of application scoped properties
160 *
161 * @param s A comma-delimited list
162 */
163 public void setApplication(String s) {
164 if (s != null) {
165 application = s.split(" *, *");
166 }
167 }
168
169 /***
170 * Sets a list of session scoped properties
171 *
172 * @param s A comma-delimited list
173 */
174 public void setSession(String s) {
175 if (s != null) {
176 session = s.split(" *, *");
177 }
178 }
179
180 /***
181 * Sets if the session should be automatically created
182 *
183 * @param value True if it should be created
184 */
185 public void setAutoCreateSession(String value) {
186 if (value != null && value.length() > 0) {
187 this.autoCreateSession = Boolean.valueOf(value).booleanValue();
188 }
189 }
190
191 private String getKey(ActionInvocation invocation) {
192 ActionProxy proxy = invocation.getProxy();
193 if (key == null || "CLASS".equals(key)) {
194 return "struts.ScopeInterceptor:" + proxy.getAction().getClass();
195 } else if ("ACTION".equals(key)) {
196 return "struts.ScopeInterceptor:" + proxy.getNamespace() + ":" + proxy.getActionName();
197 }
198 return key;
199 }
200
201 /***
202 * The constructor
203 */
204 public ScopeInterceptor() {
205 super();
206 }
207
208
209 private static class NULLClass implements Serializable {
210 public String toString() {
211 return "NULL";
212 }
213 public boolean equals(Object obj) {
214 return obj == null || (obj instanceof NULLClass);
215 }
216 }
217
218 private static final Object NULL = new NULLClass();
219
220 private static final Object nullConvert(Object o) {
221 if (o == null) {
222 return NULL;
223 }
224
225 if (o == NULL || NULL.equals(o)) {
226 return null;
227 }
228
229 return o;
230 }
231
232 private static Map locks = new IdentityHashMap();
233
234 static final void lock(Object o, ActionInvocation invocation) throws Exception {
235 synchronized (o) {
236 int count = 3;
237 Object previous = null;
238 while ((previous = locks.get(o)) != null) {
239 if (previous == invocation) {
240 return;
241 }
242 if (count-- <= 0) {
243 locks.remove(o);
244 o.notify();
245
246 throw new StrutsException("Deadlock in session lock");
247 }
248 o.wait(10000);
249 }
250 ;
251 locks.put(o, invocation);
252 }
253 }
254
255 static final void unlock(Object o) {
256 synchronized (o) {
257 locks.remove(o);
258 o.notify();
259 }
260 }
261
262 protected void after(ActionInvocation invocation, String result) throws Exception {
263 Map ses = ActionContext.getContext().getSession();
264 if ( ses != null) {
265 unlock(ses);
266 }
267 }
268
269
270 protected void before(ActionInvocation invocation) throws Exception {
271 invocation.addPreResultListener(this);
272 Map ses = ActionContext.getContext().getSession();
273 if (ses == null && autoCreateSession) {
274 ses = new SessionMap(ServletActionContext.getRequest());
275 ActionContext.getContext().setSession(ses);
276 }
277
278 if ( ses != null) {
279 lock(ses, invocation);
280 }
281
282 String key = getKey(invocation);
283 Map app = ActionContext.getContext().getApplication();
284 final ValueStack stack = ActionContext.getContext().getValueStack();
285
286 if (LOG.isDebugEnabled()) {
287 LOG.debug("scope interceptor before");
288 }
289
290 if (application != null)
291 for (int i = 0; i < application.length; i++) {
292 String string = application[i];
293 Object attribute = app.get(key + string);
294 if (attribute != null) {
295 if (LOG.isDebugEnabled()) {
296 LOG.debug("application scoped variable set " + string + " = " + String.valueOf(attribute));
297 }
298
299 stack.setValue(string, nullConvert(attribute));
300 }
301 }
302
303 if (ActionContext.getContext().getParameters().get(sessionReset) != null) {
304 return;
305 }
306
307 if (reset) {
308 return;
309 }
310
311 if (ses == null) {
312 LOG.debug("No HttpSession created... Cannot set session scoped variables");
313 return;
314 }
315
316 if (session != null && (!"start".equals(type))) {
317 for (int i = 0; i < session.length; i++) {
318 String string = session[i];
319 Object attribute = ses.get(key + string);
320 if (attribute != null) {
321 if (LOG.isDebugEnabled()) {
322 LOG.debug("session scoped variable set " + string + " = " + String.valueOf(attribute));
323 }
324 stack.setValue(string, nullConvert(attribute));
325 }
326 }
327 }
328 }
329
330 public void setKey(String key) {
331 this.key = key;
332 }
333
334
335
336
337 public void beforeResult(ActionInvocation invocation, String resultCode) {
338 String key = getKey(invocation);
339 Map app = ActionContext.getContext().getApplication();
340 final ValueStack stack = ActionContext.getContext().getValueStack();
341
342 if (application != null)
343 for (int i = 0; i < application.length; i++) {
344 String string = application[i];
345 Object value = stack.findValue(string);
346 if (LOG.isDebugEnabled()) {
347 LOG.debug("application scoped variable saved " + string + " = " + String.valueOf(value));
348 }
349
350
351 app.put(key + string, nullConvert(value));
352 }
353
354 boolean ends = "end".equals(type);
355
356 Map ses = ActionContext.getContext().getSession();
357 if (ses != null) {
358
359 if (session != null) {
360 for (int i = 0; i < session.length; i++) {
361 String string = session[i];
362 if (ends) {
363 ses.remove(key + string);
364 } else {
365 Object value = stack.findValue(string);
366
367 if (LOG.isDebugEnabled()) {
368 LOG.debug("session scoped variable saved " + string + " = " + String.valueOf(value));
369 }
370
371
372
373 ses.put(key + string, nullConvert(value));
374 }
375 }
376 }
377 unlock(ses);
378 } else {
379 LOG.debug("No HttpSession created... Cannot save session scoped variables.");
380 }
381 if (LOG.isDebugEnabled()) {
382 LOG.debug("scope interceptor after (before result)");
383 }
384 }
385
386 /***
387 * @return The type of scope operation, "start" or "end"
388 */
389 public String getType() {
390 return type;
391 }
392
393 /***
394 * Sets the type of scope operation
395 *
396 * @param type Either "start" or "end"
397 */
398 public void setType(String type) {
399 type = type.toLowerCase();
400 if ("start".equals(type) || "end".equals(type)) {
401 this.type = type;
402 } else {
403 throw new IllegalArgumentException("Only start or end are allowed arguments for type");
404 }
405 }
406
407 /***
408 * @return Gets the session reset parameter name
409 */
410 public String getSessionReset() {
411 return sessionReset;
412 }
413
414 /***
415 * @param sessionReset The session reset parameter name
416 */
417 public void setSessionReset(String sessionReset) {
418 this.sessionReset = sessionReset;
419 }
420
421
422
423
424 public String intercept(ActionInvocation invocation) throws Exception {
425 String result = null;
426 Map ses = ActionContext.getContext().getSession();
427 before(invocation);
428 try {
429 result = invocation.invoke();
430 after(invocation, result);
431 } finally {
432 if (ses != null) {
433 unlock(ses);
434 }
435 }
436
437 return result;
438 }
439
440 /***
441 * @return True if the scope is reset
442 */
443 public boolean isReset() {
444 return reset;
445 }
446
447 /***
448 * @param reset True if the scope should be reset
449 */
450 public void setReset(boolean reset) {
451 this.reset = reset;
452 }
453 }