View Javadoc

1   /*
2    * $Id: ProfilingActivationInterceptor.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * &lt;action ...&gt;
58   *   ...
59   *   &lt;interceptor-ref name="profiling"&gt;
60   *      &lt;param name="profilingKey"&gt;profilingKey&lt;/param&gt;
61   *   &lt;/interceptor-ref&gt;
62   *   ...
63   * &lt;/action&gt;
64   * <!-- END SNIPPET: example -->
65   * </pre>
66   *
67   * @version $Date: 2008-04-27 13:41:38 +0000 (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 }