Commits

Anonymous committed d16cbc0

Extract template loader construction

Comments (0)

Files changed (4)

chempound-webapp/src/main/java/net/chempound/webapp/DefaultChempoundWebModule.java

 import net.chempound.webapp.plugins.PluginRegistry;
 import net.chempound.webapp.plugins.SkinRegistry;
 import net.chempound.webapp.sword.impl.SwordModule;
+import net.chempound.webapp.template.ClasspathTemplateLoaderFactory;
 import net.chempound.webapp.template.DefaultFreemarkerService;
+import net.chempound.webapp.template.TemplateLoaderFactory;
 import org.restlet.Application;
 import org.restlet.Component;
 import org.restlet.Context;
         install(getSwordModule());
 
         bind(FreemarkerService.class).to(DefaultFreemarkerService.class);
+        bind(TemplateLoaderFactory.class).to(ClasspathTemplateLoaderFactory.class);
 
         bind(PluginRegistry.class).to(DefaultPluginRegistry.class);
         bind(CssRegistry.class).to(DefaultCssRegistry.class);

chempound-webapp/src/main/java/net/chempound/webapp/template/ClasspathTemplateLoaderFactory.java

+package net.chempound.webapp.template;
+
+import freemarker.cache.ClassTemplateLoader;
+import freemarker.cache.TemplateLoader;
+
+import javax.inject.Singleton;
+
+@Singleton
+public class ClasspathTemplateLoaderFactory implements TemplateLoaderFactory {
+
+    private static final String DEFAULT_TEMPLATE_PATH = "/net/chempound/templates/";
+
+    @Override
+    public TemplateLoader createDefaultTemplateLoader() {
+        return new ClassTemplateLoader(getClass(), DEFAULT_TEMPLATE_PATH);
+    }
+
+    @Override
+    public TemplateLoader createPluginTemplateLoader(final String path, final Class<?> context) {
+        return new ClassTemplateLoader(context, path);
+    }
+
+    @Override
+    public TemplateLoader createSkinTemplateLoader(final String path, final Class<?> context) {
+        return new ClassTemplateLoader(context, path);
+    }
+}

chempound-webapp/src/main/java/net/chempound/webapp/template/DefaultFreemarkerService.java

 package net.chempound.webapp.template;
 
-import freemarker.cache.FileTemplateLoader;
+import freemarker.cache.TemplateLoader;
 import freemarker.template.*;
 import net.chempound.config.BaseUri;
-import net.chempound.config.ChempoundConfiguration;
 import net.chempound.services.FreemarkerService;
-import net.chempound.webapp.FileExtractor;
 import net.chempound.webapp.GlobalData;
 import net.chempound.webapp.plugins.CssRegistry;
-import org.apache.commons.io.FileUtils;
 
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 
 public class DefaultFreemarkerService implements FreemarkerService {
 
     private final Configuration freemarker;
-    private final File templateRoot;
+    private final TemplateLoaderFactory templateLoaderFactory;
     private final RoutingTemplateLoader templateLoader;
-    private final CssRegistry cssRegistry;
 
     @Inject
-    public DefaultFreemarkerService(final ChempoundConfiguration configuration, final GlobalData macroService, @BaseUri final URI baseUri, final CssRegistry cssRegistry) throws TemplateModelException, IOException {
-        this.cssRegistry = cssRegistry;
-        this.templateRoot = new File(configuration.getWorkspace(), "cache/templates");
+    public DefaultFreemarkerService(final GlobalData macroService, @BaseUri final URI baseUri, final TemplateLoaderFactory templateLoaderFactory, final CssRegistry cssRegistry) throws Exception {
+        this.templateLoaderFactory = templateLoaderFactory;
 
         this.freemarker = new Configuration();
         this.freemarker.setWhitespaceStripping(true);
             }
         });
 
-        final FileTemplateLoader defaultLoader = getDefaultTemplateLoader();
+        final TemplateLoader defaultLoader = this.templateLoaderFactory.createDefaultTemplateLoader();
         this.templateLoader = new RoutingTemplateLoader(defaultLoader);
         this.freemarker.setTemplateLoader(templateLoader);
     }
 
-    private FileTemplateLoader getDefaultTemplateLoader() throws IOException {
-        final File templateDir = getDefaultTemplateDir();
-        FileExtractor.extract(DefaultFreemarkerService.class, "/net/chempound/templates/", templateDir);
-        return new FileTemplateLoader(templateDir);
-    }
-
-    private File getDefaultTemplateDir() throws IOException {
-        final File templateDir = new File(templateRoot, "default");
-        FileUtils.forceMkdir(templateDir);
-        return templateDir;
-    }
-
+    @Override
     public Template getTemplate(final String name) throws IOException {
         return freemarker.getTemplate(name);
     }
 
+    @Override
     public void registerPlugin(final String name, final Class<?> context, final String path) throws IOException {
-        final File templateDir = getPluginTemplateDir(name);
-        FileExtractor.extract(context, path, templateDir);
-        templateLoader.addPlugin(name, new FileTemplateLoader(templateDir));
+        final TemplateLoader templateLoader = templateLoaderFactory.createPluginTemplateLoader(path, context);
+        this.templateLoader.addPlugin(name, templateLoader);
     }
 
+    @Override
     public void registerSkin(final String name, final Class<?> context, final String path) throws IOException {
-        final File templateDir = getSkinTemplateDir(name);
-        FileExtractor.extract(context, path, templateDir);
-        templateLoader.addPlugin(name, new FileTemplateLoader(templateDir));
-    }
-
-    private File getPluginTemplateDir(final String name) throws IOException {
-        final File templateDir = new File(templateRoot, "plugins/" + name);
-        FileUtils.forceMkdir(templateDir);
-        return templateDir;
-    }
-
-    private File getSkinTemplateDir(final String name) throws IOException {
-        final File templateDir = new File(templateRoot, "skins/" + name);
-        FileUtils.forceMkdir(templateDir);
-        return templateDir;
+        final TemplateLoader templateLoader = templateLoaderFactory.createSkinTemplateLoader(path, context);
+        this.templateLoader.addPlugin(name, templateLoader);
     }
 }

chempound-webapp/src/main/java/net/chempound/webapp/template/TemplateLoaderFactory.java

+package net.chempound.webapp.template;
+
+import freemarker.cache.TemplateLoader;
+
+public interface TemplateLoaderFactory {
+
+    TemplateLoader createDefaultTemplateLoader();
+
+    TemplateLoader createPluginTemplateLoader(String path, Class<?> context);
+
+    TemplateLoader createSkinTemplateLoader(String path, Class<?> context);
+}