Skip to content

Commit

Permalink
Implementation for GRAILS-5163 (debug mode for gsp templates; adds de…
Browse files Browse the repository at this point in the history
…bug info as html comments to output when "?debugTemplates"/"&debugTemplates" is appended to the url)
  • Loading branch information
lhotari committed Sep 1, 2010
1 parent ad0b2c0 commit 8c5a306
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Expand Up @@ -383,5 +383,9 @@ public synchronized boolean shouldReload(PrivilegedAction<Resource> resourceCall
}
return false;
}

public boolean isPrecompiledMode() {
return precompiledMode;
}

}
Expand Up @@ -25,9 +25,12 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.text.DateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -56,11 +59,13 @@
class GroovyPageWritable implements Writable {

private static final Log LOG = LogFactory.getLog(GroovyPageWritable.class);

private static final String ATTRIBUTE_NAME_DEBUG_TEMPLATES_ID_COUNTER = "org.codehaus.groovy.grails.web.pages.DEBUG_TEMPLATES_COUNTER";
private HttpServletResponse response;
private HttpServletRequest request;
private GroovyPageMetaInfo metaInfo;
private boolean showSource;
private boolean debugTemplates;
private AtomicInteger debugTemplatesIdCounter;
private GrailsWebRequest webRequest;

private ServletContext context;
Expand All @@ -81,8 +86,20 @@ public GroovyPageWritable(GroovyPageMetaInfo metaInfo) {
context = webRequest.getServletContext();
this.metaInfo = metaInfo;
showSource = shouldShowGroovySource();
debugTemplates = shouldDebugTemplates();
if(debugTemplates) {
debugTemplatesIdCounter=(AtomicInteger)request.getAttribute(ATTRIBUTE_NAME_DEBUG_TEMPLATES_ID_COUNTER);
if(debugTemplatesIdCounter==null) {
debugTemplatesIdCounter=new AtomicInteger(0);
request.setAttribute(ATTRIBUTE_NAME_DEBUG_TEMPLATES_ID_COUNTER, debugTemplatesIdCounter);
}
}
}

private boolean shouldDebugTemplates() {
return request.getParameter("debugTemplates") != null && Environment.getCurrent() == Environment.DEVELOPMENT;
}

private boolean shouldShowGroovySource() {
return request.getParameter("showSource") != null &&
(Environment.getCurrent() == Environment.DEVELOPMENT) &&
Expand Down Expand Up @@ -167,12 +184,36 @@ public Writer writeTo(Writer out) throws IOException {
page.setGspTagLibraryLookup(metaInfo.getTagLibraryLookup());
page.setHtmlParts(metaInfo.getHtmlParts());
page.initRun(out, webRequest);
int debugId=0;
long debugStartTimeMs=0;
if(debugTemplates) {
debugId=debugTemplatesIdCounter.incrementAndGet();
out.write("<!-- GSP #");
out.write(String.valueOf(debugId));
out.write(" START template: ");
out.write(page.getGroovyPageFileName());
out.write(" precompiled: ");
out.write(String.valueOf(metaInfo.isPrecompiledMode()));
out.write(" lastmodified: ");
out.write(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date(metaInfo.getLastModified())));
out.write(" -->");
debugStartTimeMs=System.currentTimeMillis();
}
try {
page.run();
}
finally {
page.cleanup();
}
if(debugTemplates) {
out.write("<!-- GSP #");
out.write(String.valueOf(debugId));
out.write(" END template: ");
out.write(page.getGroovyPageFileName());
out.write(" rendering time: ");
out.write(String.valueOf(System.currentTimeMillis() - debugStartTimeMs));
out.write(" ms -->");
}
request.setAttribute(GrailsApplicationAttributes.PAGE_SCOPE, oldBinding);
}
return out;
Expand Down

0 comments on commit 8c5a306

Please sign in to comment.