KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > debug > TimerTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.debug;
11
12 import java.io.IOException JavaDoc;
13 import javax.servlet.jsp.JspTagException JavaDoc;
14
15 import java.util.*;
16
17 import org.mmbase.bridge.jsp.taglib.ContextReferrerTag;
18 import org.mmbase.bridge.jsp.taglib.util.Attribute;
19
20 import org.mmbase.util.logging.Logger;
21 import org.mmbase.util.logging.Logging;
22
23
24 class LongContainer {
25     long value;
26     LongContainer() {
27         value = 0;
28     }
29 }
30
31 /**
32  * Times how long the executing of the body took, and logs this.
33  * Child elements can also time theirselves, and appear in the 'total
34  * times' overview.
35  *
36  * @author Michiel Meeuwissen
37  * @version $Id: TimerTag.java,v 1.8 2004/06/30 17:51:55 michiel Exp $
38  */

39
40 public class TimerTag extends ContextReferrerTag {
41
42     private static final Logger log = Logging.getLoggerInstance(TimerTag.class);
43
44     private List timers;
45     private List timerIds;
46     private Map totalTimes;
47
48     private int lastTimer;
49     private Attribute name = Attribute.NULL;
50
51     public void setName(String JavaDoc n) throws JspTagException JavaDoc {
52         name = getAttribute(n);
53     }
54
55     /**
56      * Starts a timer.
57      *
58      * @param id An id which optionally can be null. Can e.g. be getId().
59      * @param id2 Another id which cannot be null. Something descriptive.
60      * @return an integer handle, which you need to remember to halt the timer.
61      */

62
63
64     public int startTimer(String JavaDoc id, String JavaDoc id2) throws JspTagException JavaDoc {
65         if (id == null) {
66             return startTimer(id2);
67         } else {
68             return startTimer(id + ":" + id2);
69         }
70     }
71
72     /**
73      *
74      */

75
76     public int startTimer(String JavaDoc id) throws JspTagException JavaDoc {
77         if (log.isDebugEnabled()) {
78             log.debug("Starting timer " + name.getString(this) + ": " + id);
79         }
80         timers.add(new Long JavaDoc(System.currentTimeMillis()));
81         if (totalTimes.get(id) == null) {
82             totalTimes.put(id, new LongContainer());
83         }
84         timerIds.add(id);
85         return timers.size() - 1;
86     }
87
88     /**
89      * Stops the timer identified by the handle, and logs and returns the result in second.
90      */

91
92     public long haltTimer(int handle) throws JspTagException JavaDoc {
93         long duration = System.currentTimeMillis() - ((Long JavaDoc)timers.get(handle)).longValue();
94         String JavaDoc id = (String JavaDoc)timerIds.get(handle);
95         if (log.isDebugEnabled()) {
96             log.debug("Timer " + (name != Attribute.NULL ? name.getString(this) + ":" : "") + id + ": " + (double)duration / 1000 + " s");
97         }
98         ((LongContainer)totalTimes.get(id)).value += duration;
99         return duration;
100     }
101
102     /**
103      * Initialize timer.
104      */

105     public int doStartTag() throws JspTagException JavaDoc {
106         log.info("Starting timer " + name.getString(this));
107         timers = new ArrayList(1);
108         timerIds = new ArrayList(1);
109         totalTimes = new HashMap();
110         lastTimer = 0;
111         startTimer(getId(), getClass().getName());
112         return EVAL_BODY_BUFFERED;
113     }
114
115     /**
116      *
117      */

118
119     public int doAfterBody() throws JspTagException JavaDoc {
120         haltTimer(0);
121         String JavaDoc result = "Timer " + name.getString(this) + " totals:\n";
122         Iterator i = totalTimes.keySet().iterator();
123
124         while (i.hasNext()) {
125             String JavaDoc key = (String JavaDoc)i.next();
126             result += " " + key + ": " + (double)(((LongContainer) totalTimes.get(key)).value) + " ms\n";
127         }
128         log.info(result);
129
130         try {
131             if (bodyContent != null) {
132                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
133             }
134             return SKIP_BODY;
135         } catch (IOException JavaDoc ioe){
136             throw new JspTagException JavaDoc(ioe.toString());
137         }
138     }
139     public int doEndTag() throws JspTagException JavaDoc {
140         timers = null;
141         timerIds = null;
142         totalTimes = null;
143         return super.doEndTag();
144     }
145
146 }
147
148
Popular Tags