001: /*
002: * $Id: ProfilingActivationInterceptor.java 478625 2006-11-23 17:31:52Z wsmoak $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.interceptor;
022:
023: import org.apache.struts2.dispatcher.Dispatcher;
024:
025: import com.opensymphony.xwork2.ActionInvocation;
026: import com.opensymphony.xwork2.inject.Inject;
027: import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
028: import com.opensymphony.xwork2.util.profiling.UtilTimerStack;
029:
030: import org.apache.struts2.StrutsConstants;
031:
032: /**
033: * <!-- START SNIPPET: description -->
034: *
035: * Allows profiling to be enabled or disabled via request parameters, when
036: * devMode is enabled.
037: *
038: * <!-- END SNIPPET: description -->
039: *
040: *
041: * <!-- START SNIPPET: parameters -->
042: *
043: * <ul>
044: * <li>profilingKey</li>
045: * </ul>
046: *
047: * <!-- END SNIPPET: parameters -->
048: *
049: * <!-- START SNIPPET: extending -->
050: *
051: * none
052: *
053: * <!-- END SNIPPET: extending -->
054: *
055: * <pre>
056: * <!-- START SNIPPET: example -->
057: *
058: * // to change the profiling key
059: * <action ...>
060: * ...
061: * <interceptor-ref name="profiling">
062: * <param name="profilingKey">profilingKey</param>
063: * </interceptor-ref>
064: * ...
065: * </action>
066: *
067: * <!-- END SNIPPET: example -->
068: * </pre>
069: *
070: * @version $Date: 2006-11-23 12:31:52 -0500 (Thu, 23 Nov 2006) $ $Id: ProfilingActivationInterceptor.java 478625 2006-11-23 17:31:52Z wsmoak $
071: */
072: public class ProfilingActivationInterceptor extends AbstractInterceptor {
073:
074: private String profilingKey = "profiling";
075: private boolean devMode;
076:
077: /**
078: * @return the profilingKey
079: */
080: public String getProfilingKey() {
081: return profilingKey;
082: }
083:
084: /**
085: * @param profilingKey the profilingKey to set
086: */
087: public void setProfilingKey(String profilingKey) {
088: this .profilingKey = profilingKey;
089: }
090:
091: @Inject(StrutsConstants.STRUTS_DEVMODE)
092: public void setDevMode(String mode) {
093: this .devMode = "true".equals(mode);
094: }
095:
096: @Override
097: public String intercept(ActionInvocation invocation)
098: throws Exception {
099: if (devMode) {
100: Object val = invocation.getInvocationContext()
101: .getParameters().get(profilingKey);
102: if (val != null) {
103: String sval = (val instanceof String ? (String) val
104: : ((String[]) val)[0]);
105: boolean enable = "yes".equalsIgnoreCase(sval)
106: || "true".equalsIgnoreCase(sval);
107: UtilTimerStack.setActive(enable);
108: invocation.getInvocationContext().getParameters()
109: .remove(profilingKey);
110: }
111: }
112: return invocation.invoke();
113:
114: }
115:
116: }
|