View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.monitoring.web.jsp;
19  
20  import org.apache.commons.monitoring.Role;
21  import org.apache.commons.monitoring.counters.Counter;
22  import org.apache.commons.monitoring.repositories.Repository;
23  import org.apache.commons.monitoring.stopwatches.StopWatch;
24  
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.tagext.TagSupport;
27  
28  import static org.apache.commons.monitoring.web.jsp.TagUtils.getScope;
29  
30  /**
31   * A JSP tag to counter JSP rendering performances
32   *
33   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
34   */
35  public class StartTag extends TagSupport {
36      private String id;
37      private String scope;
38      private String name;
39  
40      public void setId(String id) {
41          this.id = id;
42      }
43  
44      @Override
45      public int doStartTag() throws JspException {
46          final StopWatch stopWatch = Repository.INSTANCE.start(Repository.INSTANCE.getCounter(new Counter.Key(Role.JSP, name)));
47          if (scope != null) {
48              pageContext.setAttribute(id, stopWatch, getScope(scope));
49          } else {
50              pageContext.setAttribute(id, stopWatch);
51          }
52          return EVAL_PAGE;
53      }
54  
55      public void setScope(String scope) {
56          this.scope = scope;
57      }
58  
59      public void setName(String name) {
60          this.name = name;
61      }
62  }