Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.j256.dstamp.web.resource;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import freemarker.cache.TemplateLoader;
- /**
- * Implementation of {@link TemplateLoader} that that does some things specific to our environment.
- * FileTemplateLoader was used as a reference implementation but it used the canonical name of the base-dir
- * which caused problems with the CMS symbolic link both at start up and when we roll to a new version.
- *
- * @author graywatson
- */
- public class LocalFileTemplateLoader implements TemplateLoader {
- public File baseDir;
- @Override
- public Object findTemplateSource(String name) {
- File source = new File(baseDir, name);
- if (source.isFile()) {
- return source;
- } else {
- return null;
- }
- }
- @Override
- public long getLastModified(Object templateSource) {
- if (templateSource instanceof File) {
- return new Long(((File) templateSource).lastModified());
- } else {
- throw new IllegalArgumentException("templateSource is an unknown type: " + templateSource.getClass());
- }
- }
- @Override
- public Reader getReader(Object templateSource, String encoding) throws IOException {
- if (templateSource instanceof File) {
- return new InputStreamReader(new FileInputStream((File) templateSource), encoding);
- } else {
- throw new IllegalArgumentException("templateSource is an unknown type: " + templateSource.getClass());
- }
- }
- @Override
- public void closeTemplateSource(Object templateSource) {
- // noop
- }
- @Required
- public void setBaseDir(File baseDir) {
- this.baseDir = baseDir;
- // it may not exist yet because CMS is going to download and create it
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement