Question

I am trying to create two separate freemarker Configuration instances. I have abstract class like this

public abstract class AbstractFreemarkerConfiguration {

Logger logger = LoggerFactory.getLogger(AbstractFreemarkerConfiguration.class);

Configuration cfg = new Configuration();

public AbstractFreemarkerConfiguration() throws IOException, TemplateModelException {
    logger.info("Initializing Freemarker Config");
    cfg.setDefaultEncoding("UTF-8");
    setTemplateExceptionHandler();
    cfg.setSharedVariable("layout", getLayoutDirectives());
    cfg.setTemplateUpdateDelay(0);
    cfg.setLocalizedLookup(false);

}

@Autowired
@Qualifier("databaseTemplateLoader")
public void setTemplateLoader(TemplateLoader loader){
    logger.info("Setting hibernate teplate loader");
    cfg.setTemplateLoader(loader);

}

public abstract void setTemplateExceptionHandler();
}

And then I have two classes like this

@Component("freemarkerProcessor")
public class FreemarkerTemplateProcessor extends AbstractFreemarkerConfiguration implements TemplateProcessor {

public FreemarkerTemplateProcessor() throws IOException, TemplateModelException {
    super();
}

private Environment process(Object template) {
    Writer writer = new NullWriter();
    //do stuff
}

@Override
public List<String> getIncludes(Object template) {
    //do stuff
}

@Override
public void setTemplateExceptionHandler() {
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
}

}

and second one is basically same but setting Template Exception Handler to RETHROW_HANDLER

My problem is, that when I do this and I get exception in that one with IGNORE HANDLER it will go through RETHROW instead ignoring it.

In debug I determined that I have to separated instances of Configuration and TemplateExpetionHandler so I dont know why I am getting exceptions with IGNORE HANDLER.

Is there some global cashe of Configuration setting or something that I don't know about?

Thanks for any response.

Was it helpful?

Solution

So I figured this out. I had two spring services one with IGNORE and one with RETHROW. I was getting freemarker template object from that one with RETHROW and it seems that template object keeps ExceptionHandler setting based on origin configuration.

So when I processed template from RETHROW one in IGNORE one, it still used RETHROW because that template came from that config.

When I loaded template from IGNORE one it kept ignoring even on RETHROW one. This is definitely unexpected behavior for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top