View Javadoc

1   /*
2    * $Id: CreateSessionInterceptor.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.interceptor;
19  
20  import org.apache.struts2.dispatcher.Dispatcher;
21  
22  import com.opensymphony.xwork2.ActionInvocation;
23  import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
24  import com.opensymphony.xwork2.util.profiling.UtilTimerStack;
25  
26  /***
27   * Allows profiling to be enabled or disabled via request parameters, when
28   * devMode is enabled.
29   */
30  public class ProfilingActivationInterceptor extends AbstractInterceptor {
31  
32      private String profilingKey = "profiling";
33      
34      /***
35       * @return the profilingKey
36       */
37      public String getProfilingKey() {
38          return profilingKey;
39      }
40  
41      /***
42       * @param profilingKey the profilingKey to set
43       */
44      public void setProfilingKey(String profilingKey) {
45          this.profilingKey = profilingKey;
46      }
47  
48      @Override
49      public String intercept(ActionInvocation invocation) throws Exception {
50          if (Dispatcher.getInstance().isDevMode()) {
51              Object val = invocation.getInvocationContext().getParameters().get(profilingKey);
52              if (val != null) {
53                  String sval = (val instanceof String ? (String)val : ((String[])val)[0]);
54                  boolean enable = "yes".equalsIgnoreCase(sval) || "true".equalsIgnoreCase(sval);
55                  UtilTimerStack.setActive(enable);
56                  invocation.getInvocationContext().getParameters().remove(profilingKey);
57              }
58          }
59          return invocation.invoke();
60          
61      }
62  
63  }