001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.tag; 020 021import java.io.IOException; 022 023import lucee.runtime.dump.DumpTable; 024import lucee.runtime.dump.DumpWriter; 025import lucee.runtime.dump.SimpleDumpData; 026import lucee.runtime.exp.PageException; 027import lucee.runtime.ext.tag.BodyTagImpl; 028 029/** 030* Stops the time from starttag to endtag 031* 032* 033* 034**/ 035public final class Stopwatch extends BodyTagImpl { 036 037 private String label; 038 private long time; 039 private String variable; 040 041 @Override 042 public void release() { 043 super.release(); 044 label=null; 045 time=0L; 046 variable=null; 047 } 048 049 /** Label of the Stopwatch 050 * @param label sets the Label of the Stopwatch 051 **/ 052 public void setLabel(String label) { 053 this.label=label; 054 } 055 056 /** 057 * Variable Name to write result to it 058 * @param variable variable name 059 */ 060 public void setVariable(String variable) { 061 this.variable=variable; 062 } 063 064 065 @Override 066 public int doStartTag() { 067 time=System.currentTimeMillis(); 068 return EVAL_BODY_INCLUDE; 069 } 070 071 @Override 072 public int doEndTag() throws PageException { 073 long exe = (System.currentTimeMillis()-time); 074 075 if(variable!=null) { 076 pageContext.setVariable(variable,new Double(exe)); 077 } 078 else { 079 DumpTable table = new DumpTable("#ff9900","#ffcc00","#000000"); 080 table.appendRow(1,new SimpleDumpData(label==null?"Stopwatch":label),new SimpleDumpData(exe)); 081 DumpWriter writer=pageContext.getConfig().getDefaultDumpWriter(DumpWriter.DEFAULT_RICH); 082 try { 083 084 pageContext.forceWrite(writer.toString(pageContext,table,true)); 085 } 086 catch (IOException e) {} 087 } 088 return EVAL_PAGE; 089 } 090 091 @Override 092 public void doInitBody() { 093 094 } 095 096 @Override 097 public int doAfterBody() { 098 return SKIP_BODY; 099 } 100}