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