Skip to content

Commit

Permalink
Re-use Freemarker Config
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Apr 23, 2014
1 parent b6dd963 commit 90e606a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Expand Up @@ -14,6 +14,8 @@
import org.jboss.forge.addon.templates.Template;
import org.jboss.forge.addon.templates.TemplateGenerator;

import freemarker.template.Configuration;

/**
* A Freemarker implementation of a {@link TemplateGenerator}
*
Expand All @@ -24,6 +26,7 @@ public class FreemarkerTemplateGenerator implements TemplateGenerator
{
@Inject
private ResourceTemplateLoader loader;
private Configuration config;

@Override
public boolean handles(Class<? extends Template> type)
Expand All @@ -34,7 +37,17 @@ public boolean handles(Class<? extends Template> type)
@Override
public Template create(Resource<?> template, Class<? extends Template> type)
{
return new FreemarkerTemplateImpl(loader, template);
return new FreemarkerTemplateImpl(loader, template, getConfiguration());
}

private Configuration getConfiguration()
{
if (config == null)
{
config = new Configuration();
config.setTemplateLoader(loader);
}
return config;
}

}
Expand Up @@ -7,6 +7,7 @@
import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.templates.AbstractTemplate;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;

/**
Expand All @@ -19,16 +20,15 @@
*/
class FreemarkerTemplateImpl extends AbstractTemplate implements FreemarkerTemplate
{
private final freemarker.template.Configuration freemarkerConfig;
private final freemarker.template.Configuration config;
private final ResourceTemplateLoader loader;

public FreemarkerTemplateImpl(ResourceTemplateLoader loader, Resource<?> resource)
public FreemarkerTemplateImpl(ResourceTemplateLoader loader, Resource<?> resource, Configuration config)
{
super(resource);
this.loader = loader;

freemarkerConfig = new freemarker.template.Configuration();
freemarkerConfig.setTemplateLoader(loader);
this.config = config;
}

@Override
Expand Down Expand Up @@ -62,6 +62,6 @@ public void process(Object model, Writer output) throws IOException
@Override
public freemarker.template.Configuration getFreemarkerConfig()
{
return freemarkerConfig;
return config;
}
}

0 comments on commit 90e606a

Please sign in to comment.