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            @Override
024            public void release()   {
025                    super.release();
026                    label=null;
027                    time=0L;
028                    variable=null;
029            }
030    
031            /** Label of the Stopwatch
032            * @param label sets the Label of the Stopwatch
033            **/
034            public void setLabel(String label)      {
035                    this.label=label;
036            }
037    
038            /**
039             * Variable Name to write result to it
040             * @param variable  variable name
041             */
042            public void setVariable(String variable)        {
043                    this.variable=variable;
044            }
045    
046    
047            @Override
048            public int doStartTag() {
049                    time=System.currentTimeMillis();
050                    return EVAL_BODY_INCLUDE;
051            }
052    
053            @Override
054            public int doEndTag() throws PageException      {
055                    long exe = (System.currentTimeMillis()-time);
056                    
057                    if(variable!=null) {
058                        pageContext.setVariable(variable,new Double(exe));
059                    }
060                    else {
061                            DumpTable table = new DumpTable("#ff9900","#ffcc00","#000000");
062                            table.appendRow(1,new SimpleDumpData(label==null?"Stopwatch":label),new SimpleDumpData(exe));
063                            DumpWriter writer=pageContext.getConfig().getDefaultDumpWriter(DumpWriter.DEFAULT_RICH);
064                            try {
065                                    
066                                    pageContext.forceWrite(writer.toString(pageContext,table,true));
067                            } 
068                            catch (IOException e) {}
069                    }
070                    return EVAL_PAGE;
071            }
072    
073            @Override
074            public void doInitBody()        {
075                    
076            }
077    
078            @Override
079            public int doAfterBody()        {
080                    return SKIP_BODY;
081            }
082    }