View Javadoc

1   /*
2    * $Id: ProfilingActivationInterceptor.java 478625 2006-11-23 17:31:52Z wsmoak $
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  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 12:31:52 -0500 (Thu, 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 }