Advertisement
dig090

LocalFileTemplateLoader

Jun 10th, 2013
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. package com.j256.dstamp.web.resource;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.Reader;
  8.  
  9. import freemarker.cache.TemplateLoader;
  10.  
  11. /**
  12.  * Implementation of {@link TemplateLoader} that that does some things specific to our environment.
  13.  * FileTemplateLoader was used as a reference implementation but it used the canonical name of the base-dir
  14.  * which caused problems with the CMS symbolic link both at start up and when we roll to a new version.
  15.  *
  16.  * @author graywatson
  17.  */
  18. public class LocalFileTemplateLoader implements TemplateLoader {
  19.  
  20.     public File baseDir;
  21.  
  22.     @Override
  23.     public Object findTemplateSource(String name) {
  24.         File source = new File(baseDir, name);
  25.         if (source.isFile()) {
  26.             return source;
  27.         } else {
  28.             return null;
  29.         }
  30.     }
  31.  
  32.     @Override
  33.     public long getLastModified(Object templateSource) {
  34.         if (templateSource instanceof File) {
  35.             return new Long(((File) templateSource).lastModified());
  36.         } else {
  37.             throw new IllegalArgumentException("templateSource is an unknown type: " + templateSource.getClass());
  38.         }
  39.     }
  40.  
  41.     @Override
  42.     public Reader getReader(Object templateSource, String encoding) throws IOException {
  43.         if (templateSource instanceof File) {
  44.             return new InputStreamReader(new FileInputStream((File) templateSource), encoding);
  45.         } else {
  46.             throw new IllegalArgumentException("templateSource is an unknown type: " + templateSource.getClass());
  47.         }
  48.     }
  49.  
  50.     @Override
  51.     public void closeTemplateSource(Object templateSource) {
  52.         // noop
  53.     }
  54.  
  55.     @Required
  56.     public void setBaseDir(File baseDir) {
  57.         this.baseDir = baseDir;
  58.         // it may not exist yet because CMS is going to download and create it
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement