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.Role;
021    import org.apache.commons.monitoring.counters.Counter;
022    import org.apache.commons.monitoring.repositories.Repository;
023    import org.apache.commons.monitoring.stopwatches.StopWatch;
024    
025    import javax.servlet.jsp.JspException;
026    import javax.servlet.jsp.tagext.TagSupport;
027    
028    import static org.apache.commons.monitoring.web.jsp.TagUtils.getScope;
029    
030    /**
031     * A JSP tag to counter JSP rendering performances
032     *
033     * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
034     */
035    public class StartTag extends TagSupport {
036        private String id;
037        private String scope;
038        private String name;
039    
040        public void setId(String id) {
041            this.id = id;
042        }
043    
044        @Override
045        public int doStartTag() throws JspException {
046            final StopWatch stopWatch = Repository.INSTANCE.start(Repository.INSTANCE.getCounter(new Counter.Key(Role.JSP, name)));
047            if (scope != null) {
048                pageContext.setAttribute(id, stopWatch, getScope(scope));
049            } else {
050                pageContext.setAttribute(id, stopWatch);
051            }
052            return EVAL_PAGE;
053        }
054    
055        public void setScope(String scope) {
056            this.scope = scope;
057        }
058    
059        public void setName(String name) {
060            this.name = name;
061        }
062    }