001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.monitoring.web.jsp;
019    
020    import org.apache.commons.monitoring.stopwatches.StopWatch;
021    
022    import javax.servlet.jsp.JspException;
023    import javax.servlet.jsp.tagext.TagSupport;
024    
025    import static org.apache.commons.monitoring.web.jsp.TagUtils.getScope;
026    
027    /**
028     * A JSP tag to counter JSP rendering performances
029     *
030     * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
031     */
032    public class StopTag extends TagSupport {
033        private String id;
034        private String scope;
035    
036        /**
037         * @param id the id to set
038         */
039        public void setId(String id) {
040            this.id = id;
041        }
042    
043        /**
044         * {@inheritDoc}
045         *
046         * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
047         */
048        @Override
049        public int doStartTag() throws JspException {
050            StopWatch stopWatch;
051            if (scope != null) {
052                stopWatch = (StopWatch) pageContext.getAttribute(id, getScope(scope));
053            } else {
054                stopWatch = (StopWatch) pageContext.getAttribute(id);
055            }
056            if (stopWatch == null) {
057                throw new JspException("No StopWatch under ID " + id + " and scope " + scope);
058            }
059            stopWatch.stop();
060            return EVAL_PAGE;
061        }
062    
063        /**
064         * @param scope the scope to set
065         */
066        public void setScope(final String scope) {
067            this.scope = scope;
068        }
069    }