use of groovy.text.Template in project groovy by apache.
the class TemplateServlet method findCachedTemplate.
/**
* Find a cached template for a given key. If a <code>File</code> is passed then
* any cached object is validated against the File to determine if it is out of
* date
* @param key a unique key for the template, such as a file's absolutePath or a URL.
* @param file a file to be used to determine if the cached template is stale. May be null.
* @return The cached template, or null if there was no cached entry, or the entry was stale.
*/
private Template findCachedTemplate(String key, File file) {
Template template = null;
/*
* Test cache for a valid template bound to the key.
*/
if (verbose) {
log("Looking for cached template by key \"" + key + "\"");
}
TemplateCacheEntry entry = (TemplateCacheEntry) cache.get(key);
if (entry != null) {
if (entry.validate(file)) {
if (verbose) {
log("Cache hit! " + entry);
}
template = entry.template;
} else {
if (verbose) {
log("Cached template " + key + " needs recompilation! " + entry);
}
}
} else {
if (verbose) {
log("Cache miss for " + key);
}
}
return template;
}
use of groovy.text.Template in project groovy by apache.
the class TemplateServlet method getTemplate.
/**
* Gets the template created by the underlying engine parsing the request.
*
* <p>
* This method looks up a simple (weak) hash map for an existing template
* object that matches the source URL. If there is no cache entry, a new one is
* created by the underlying template engine. This new instance is put
* to the cache for consecutive calls.
*
* @return The template that will produce the response text.
* @param url The URL containing the template source..
* @throws ServletException If the request specified an invalid template source URL
*/
protected Template getTemplate(URL url) throws ServletException {
String key = url.toString();
Template template = findCachedTemplate(key, null);
// Template not cached or the source file changed - compile new template!
if (template == null) {
try {
template = createAndStoreTemplate(key, url.openConnection().getInputStream(), null);
} catch (Exception e) {
throw new ServletException("Creation of template failed: " + e, e);
}
}
return template;
}
use of groovy.text.Template in project groovy by apache.
the class BaseTemplate method fragment.
/**
* Renders an embedded template as a fragment. Fragments are cached in a template, meaning that
* if you use the same fragment in a template, it will only be compiled once, but once <b>per template
* instance</b>. This is less performant than using {@link #layout(java.util.Map, String)}.
*
* @param model model to be passed to the template
* @param templateText template body
* @return this template instance
* @throws IOException
* @throws ClassNotFoundException
*/
public Object fragment(Map model, String templateText) throws IOException, ClassNotFoundException {
Template template = cachedFragments.get(templateText);
if (template == null) {
template = engine.createTemplate(new StringReader(templateText));
cachedFragments.put(templateText, template);
}
template.make(model).writeTo(out);
return this;
}
use of groovy.text.Template in project groovy by apache.
the class GroovyDocTemplateEngine method applyRootDocTemplate.
String applyRootDocTemplate(String template, GroovyRootDoc rootDoc) {
String templateWithBindingApplied = "";
try {
Template t = docTemplates.get(template);
if (t == null) {
t = engine.createTemplate(resourceManager.getReader(template));
docTemplates.put(template, t);
}
Map<String, Object> binding = new HashMap<String, Object>();
binding.put("rootDoc", rootDoc);
binding.put("props", properties);
templateWithBindingApplied = t.make(binding).toString();
} catch (Exception e) {
System.out.println("Error processing root doc template");
e.printStackTrace();
}
return templateWithBindingApplied;
}
use of groovy.text.Template in project hale by halestudio.
the class Request method init.
/**
* Initialize the cache.
*/
private void init() {
File cacheDir = null;
try {
// this will throw up in non-OSGi environments
cacheDir = PlatformUtil.getInstanceLocation();
} catch (Throwable t) {
cacheEnabled = false;
}
if (cacheEnabled) {
try {
if (cacheDir == null) {
cacheDir = new File(System.getProperty("java.io.tmpdir"));
}
// create the configuration from the template
SimpleTemplateEngine engine = new SimpleTemplateEngine(Request.class.getClassLoader());
Template template = engine.createTemplate(Request.class.getResource("ehcache.xml"));
Map<String, Object> binding = new HashMap<>();
// replace the cache directory
binding.put("cache_dir", cacheDir.getAbsolutePath());
ByteArrayOutputStream data = new ByteArrayOutputStream();
try (Writer writer = new OutputStreamWriter(data, "UTF-8")) {
template.make(binding).writeTo(writer);
}
// initialize the cache manager
if (HaleCacheManager.create(new ByteArrayInputStream(data.toByteArray())).getCache(CACHE_NAME) != null) {
return;
}
// create a Cache instance - providing cachePath has no effect
Cache cache = new Cache(CACHE_NAME, 300, MemoryStoreEvictionPolicy.LRU, true, null, true, 0, 0, true, 0, null);
// add it to CacheManger
HaleCacheManager.getInstance().addCache(cache);
} catch (Exception e) {
log.error("Cache initialization failed", e);
}
}
}
Aggregations