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.servlet.jsp;
19  
20  import static org.apache.commons.monitoring.servlet.jsp.TagUtils.*;
21  
22  import java.io.IOException;
23  import java.util.List;
24  
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.tagext.TagSupport;
27  
28  import org.apache.commons.monitoring.Monitoring;
29  import org.apache.commons.monitoring.StopWatch;
30  import org.apache.commons.monitoring.Unit;
31  
32  /**
33   * A JSP tag to monitor JSP rendering performances
34   *
35   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
36   */
37  public class UnitTag
38      extends TagSupport
39  {
40      private String unit;
41  
42      private String id;
43  
44      private String name;
45  
46      /**
47       * {@inheritDoc}
48       *
49       * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
50       */
51      @Override
52      public int doStartTag()
53          throws JspException
54      {
55          StringBuffer out = new StringBuffer();
56          out.append( "<select" );
57          TagUtils.setAttribute( out, "name", name );
58          TagUtils.setAttribute( out, "id", id );
59          out.append( "'>" );
60          Unit u = Unit.get( unit );
61          for ( Unit derived : u.getPrimary().getDerived() )
62          {
63              out.append( "<option value='" );
64              out.append( derived.getName() );
65              out.append( "'" );
66              if ( derived.equals( u ) )
67              {
68                  out.append( " selected='selected'" );
69              }
70              out.append( ">" );
71              out.append( derived.getName() );
72              out.append( "</option>" );
73          }
74          out.append( "</select>" );
75  
76          try
77          {
78              pageContext.getOut().append( out.toString() );
79          }
80          catch ( IOException e )
81          {
82              throw new JspException( "UnitTag : " + e.getMessage() );
83          }
84  
85          return EVAL_PAGE;
86      }
87  
88      /**
89       * @param unit the unit to set
90       */
91      public void setUnit( String unit )
92      {
93          this.unit = unit;
94      }
95  
96      /**
97       * @param id the id to set
98       */
99      public void setId( String id )
100     {
101         this.id = id;
102     }
103 
104     /**
105      * @param name the name to set
106      */
107     public void setName( String name )
108     {
109         this.name = name;
110     }
111 
112 }