From: <jt...@hy...> - 2008-12-09 00:54:20
|
Author: jtravis Date: 2008-12-08 16:54:13 -0800 (Mon, 08 Dec 2008) New Revision: 11998 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=11998 Modified: trunk/ui_plugins/tmpl_render/Renderer.groovy Log: HHQ-2586. Reviewed by rmorgan. Cache handles to templates, since each template we create results in a new class getting thrown into the ClassLoader, which in turn well never get cleaned up ... wah wah waaaaaaaaaaah Modified: trunk/ui_plugins/tmpl_render/Renderer.groovy =================================================================== --- trunk/ui_plugins/tmpl_render/Renderer.groovy 2008-12-08 10:02:15 UTC (rev 11997) +++ trunk/ui_plugins/tmpl_render/Renderer.groovy 2008-12-09 00:54:13 UTC (rev 11998) @@ -1,4 +1,7 @@ import groovy.text.SimpleTemplateEngine +import groovy.text.Template +import org.apache.commons.logging.Log +import org.apache.commons.logging.LogFactory /** @@ -6,12 +9,37 @@ * primarily used through the RenditServer.renderTemplate() method */ class Renderer { + // We cache the templates here in a static context, because each + // creation of a template results in a new class -- and that's + // bad if classloaders aren't being cleaned up correctly. + private static final Map<String, Map> _templates = [:] + private final Log _log = LogFactory.getLog(Renderer.class.name) + + private Template getTemplate(File gspFile) { + if (!gspFile.isFile()) { + throw new RuntimeException("File [${gspFile.absolutePath}] not readable or not found"); + } + + long mtime = gspFile.lastModified() + synchronized(_templates) { + Map tmplMap = _templates.get(gspFile) + + if (!tmplMap || tmplMap.mtime != mtime) { + _log.info("Creating template for [${gspFile.absolutePath}]") + gspFile.withReader { reader -> + def eng = new SimpleTemplateEngine(Thread.currentThread().contextClassLoader) + Template t = eng.createTemplate(reader) + _templates[gspFile] = [mtime: mtime, template : t] + return t + } + } else { + return tmplMap.template + } + } + } + def render(File gspFile, Map params, Writer output) { - gspFile.withReader { reader -> - def eng = new SimpleTemplateEngine(Thread.currentThread().contextClassLoader) - def template = eng.createTemplate(reader) - template.make(params).writeTo(output) - } + getTemplate(gspFile).make(params).writeTo(output) output.flush() } } |