001    package railo.runtime.tag;
002    
003    import java.io.IOException;
004    
005    import railo.runtime.dump.DumpTable;
006    import railo.runtime.dump.DumpWriter;
007    import railo.runtime.dump.SimpleDumpData;
008    import railo.runtime.exp.PageException;
009    import railo.runtime.ext.tag.BodyTagImpl;
010    
011    /**
012    * Stops the time from starttag to endtag
013    *
014    *
015    *
016    **/
017    public final class Stopwatch extends BodyTagImpl {
018    
019            private String label;
020            private long time;
021        private String variable;
022    
023            /**
024            * @see javax.servlet.jsp.tagext.Tag#release()
025            */
026            public void release()   {
027                    super.release();
028                    label=null;
029                    time=0L;
030                    variable=null;
031            }
032    
033            /** Label of the Stopwatch
034            * @param label sets the Label of the Stopwatch
035            **/
036            public void setLabel(String label)      {
037                    this.label=label;
038            }
039    
040            /**
041             * Variable Name to write result to it
042             * @param variable  variable name
043             */
044            public void setVariable(String variable)        {
045                    this.variable=variable;
046            }
047    
048    
049            /**
050            * @see javax.servlet.jsp.tagext.Tag#doStartTag()
051            */
052            public int doStartTag() {
053                    time=System.currentTimeMillis();
054                    return EVAL_BODY_INCLUDE;
055            }
056    
057            /**
058            * @throws PageException
059             * @see javax.servlet.jsp.tagext.Tag#doEndTag()
060            */
061            public int doEndTag() throws PageException      {
062                    long exe = (System.currentTimeMillis()-time);
063                    
064                    if(variable!=null) {
065                        pageContext.setVariable(variable,new Double(exe));
066                    }
067                    else {
068                            DumpTable table = new DumpTable("#ff9900","#ffcc00","#000000");
069                            table.appendRow(1,new SimpleDumpData(label==null?"Stopwatch":label),new SimpleDumpData(exe));
070                            DumpWriter writer=pageContext.getConfig().getDefaultDumpWriter();
071                            try {
072                                    
073                                    pageContext.forceWrite(writer.toString(pageContext,table,true));
074                            } 
075                            catch (IOException e) {}
076                    }
077                    return EVAL_PAGE;
078            }
079    
080            /**
081            * @see javax.servlet.jsp.tagext.BodyTag#doInitBody()
082            */
083            public void doInitBody()        {
084                    
085            }
086    
087            /**
088            * @see javax.servlet.jsp.tagext.BodyTag#doAfterBody()
089            */
090            public int doAfterBody()        {
091                    return SKIP_BODY;
092            }
093    }