1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.interceptor;
22
23 import org.apache.struts2.dispatcher.Dispatcher;
24
25 import com.opensymphony.xwork2.ActionInvocation;
26 import com.opensymphony.xwork2.inject.Inject;
27 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
28 import com.opensymphony.xwork2.util.profiling.UtilTimerStack;
29
30 import org.apache.struts2.StrutsConstants;
31
32 /***
33 * <!-- START SNIPPET: description -->
34 *
35 * Allows profiling to be enabled or disabled via request parameters, when
36 * devMode is enabled.
37 *
38 * <!-- END SNIPPET: description -->
39 *
40 *
41 * <!-- START SNIPPET: parameters -->
42 *
43 * <ul>
44 * <li>profilingKey</li>
45 * </ul>
46 *
47 * <!-- END SNIPPET: parameters -->
48 *
49 * <!-- START SNIPPET: extending -->
50 *
51 * none
52 *
53 * <!-- END SNIPPET: extending -->
54 *
55 * <pre>
56 * <!-- START SNIPPET: example -->
57 *
58 * // to change the profiling key
59 * <action ...>
60 * ...
61 * <interceptor-ref name="profiling">
62 * <param name="profilingKey">profilingKey</param>
63 * </interceptor-ref>
64 * ...
65 * </action>
66 *
67 * <!-- END SNIPPET: example -->
68 * </pre>
69 *
70 * @version $Date: 2006-11-23 18:31:52 +0100 (Do, 23. Nov 2006) $ $Id: ProfilingActivationInterceptor.java 478625 2006-11-23 17:31:52Z wsmoak $
71 */
72 public class ProfilingActivationInterceptor extends AbstractInterceptor {
73
74 private String profilingKey = "profiling";
75 private boolean devMode;
76
77 /***
78 * @return the profilingKey
79 */
80 public String getProfilingKey() {
81 return profilingKey;
82 }
83
84 /***
85 * @param profilingKey the profilingKey to set
86 */
87 public void setProfilingKey(String profilingKey) {
88 this.profilingKey = profilingKey;
89 }
90
91 @Inject(StrutsConstants.STRUTS_DEVMODE)
92 public void setDevMode(String mode) {
93 this.devMode = "true".equals(mode);
94 }
95
96 @Override
97 public String intercept(ActionInvocation invocation) throws Exception {
98 if (devMode) {
99 Object val = invocation.getInvocationContext().getParameters().get(profilingKey);
100 if (val != null) {
101 String sval = (val instanceof String ? (String)val : ((String[])val)[0]);
102 boolean enable = "yes".equalsIgnoreCase(sval) || "true".equalsIgnoreCase(sval);
103 UtilTimerStack.setActive(enable);
104 invocation.getInvocationContext().getParameters().remove(profilingKey);
105 }
106 }
107 return invocation.invoke();
108
109 }
110
111 }