Java Code Examples for freemarker.template.Configuration#setCacheStorage()

The following examples show how to use freemarker.template.Configuration#setCacheStorage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FreemarkerResultHandler.java    From oxygen with Apache License 2.0 5 votes vote down vote up
FreemarkerResolver() {
  cfg = new Configuration(Configuration.getVersion());
  cfg.setDefaultEncoding(StandardCharsets.UTF_8.name());
  cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
  cfg.setClassForTemplateLoading(Bootstrap.class,
      WebConfigKeys.VIEW_PREFIX_FREEMARKER.getValue());
  cfg.setNumberFormat(Strings.OCTOTHORP);
  if (WebConfigKeys.VIEW_CACHE.castValue(boolean.class)) {
    cfg.setCacheStorage(new MruCacheStorage(20, 250));
  } else {
    cfg.unsetCacheStorage();
  }
}
 
Example 2
Source File: FreeMarkerViewResolver.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a default config for FreeMarker which reads templates from the classpath under the path specified in the prefix
 * a prefix is specified.
 *
 * @param configuration free marker config
 * @param resourceClass class to load resources relative to
 * @return supplied configuration if not nul, otherwise a default one
 */
protected Configuration buildDefaultConfig(Configuration configuration, Class<?> resourceClass) {
    if (configuration != null) {
        return configuration;
    }
    Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
    cfg.setClassForTemplateLoading(resourceClass == null ? getClass() : resourceClass, "/");
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    cfg.setLogTemplateExceptions(true);
    cfg.setCacheStorage(NullCacheStorage.INSTANCE);
    return cfg;
}
 
Example 3
Source File: FreeMarkerProcessor.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * FreeMarker configuration for loading the specified template directly from a String
 * 
 * @param path      Pseudo Path to the template
 * @param template  Template content
 * 
 * @return FreeMarker configuration
 */
protected Configuration getStringConfig(String path, String template)
{
    Configuration config = new Configuration();
    
    // setup template cache
    config.setCacheStorage(new MruCacheStorage(2, 0));
    
    // use our custom loader to load a template directly from a String
    StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
    stringTemplateLoader.putTemplate(path, template);
    config.setTemplateLoader(stringTemplateLoader);
    
    // use our custom object wrapper that can deal with QNameMap objects directly
    config.setObjectWrapper(qnameObjectWrapper);
    
    // rethrow any exception so we can deal with them
    config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    
    // set default template encoding
    if (defaultEncoding != null)
    {
        config.setDefaultEncoding(defaultEncoding);
    }
    config.setIncompatibleImprovements(new Version(2, 3, 20));
    config.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
    
    return config;
}
 
Example 4
Source File: FreeMarkerTemplateHander.java    From VX-API-Gateway with MIT License 5 votes vote down vote up
public FreeMarkerTemplateHander(Vertx vertx, String templateDirectory, String contentType) {
	super(DEFAULT_TEMPLATE_EXTENSION, DEFAULT_MAX_CACHE_SIZE);
	this.contentType = contentType;
	config = new Configuration(Configuration.VERSION_2_3_28);
	try {
		config.setDirectoryForTemplateLoading(new File(templateDirectory));
	} catch (IOException e) {
		throw new FileSystemException("not found template directory:" + templateDirectory);
	}
	config.setObjectWrapper(new VertxWebObjectWrapper(config.getIncompatibleImprovements()));
	config.setCacheStorage(new NullCacheStorage());
}
 
Example 5
Source File: FreeMarkerTemplateEngineImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
public FreeMarkerTemplateEngineImpl(Vertx vertx, String extension) {
  super(vertx, extension);

  config = new Configuration(Configuration.VERSION_2_3_29);
  config.setObjectWrapper(new VertxWebObjectWrapper(config.getIncompatibleImprovements()));
  config.setTemplateLoader(new FreeMarkerTemplateLoader(vertx));
  config.setCacheStorage(new NullCacheStorage());
}
 
Example 6
Source File: CrafterFreeMarkerConfigurer.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void postProcessConfiguration(Configuration config) throws IOException, TemplateException {
    if (templateExceptionHandler != null) {
        config.setTemplateExceptionHandler(templateExceptionHandler);
    }
    if (!cacheTemplates) {
        config.setCacheStorage(NullCacheStorage.INSTANCE);
    }
}
 
Example 7
Source File: FreemarkerTemplateEngine.java    From pippo with Apache License 2.0 4 votes vote down vote up
@Override
public void init(Application application) {
    super.init(application);

    Router router = getRouter();
    PippoSettings pippoSettings = getPippoSettings();

    configuration = new Configuration(Configuration.VERSION_2_3_21);
    configuration.setDefaultEncoding(PippoConstants.UTF8);
    configuration.setOutputEncoding(PippoConstants.UTF8);
    configuration.setLocalizedLookup(true);
    configuration.setClassForTemplateLoading(FreemarkerTemplateEngine.class, getTemplatePathPrefix());

    // We also do not want Freemarker to chose a platform dependent
    // number formatting. Eg "1000" could be printed out by FTL as "1,000"
    // on some platforms.
    // See also:
    // http://freemarker.sourceforge.net/docs/app_faq.html#faq_number_grouping
    configuration.setNumberFormat("0.######"); // now it will print 1000000

    if (pippoSettings.isDev()) {
        configuration.setTemplateUpdateDelayMilliseconds(0); // disable cache
    } else {
        // never update the templates in production or while testing...
        configuration.setTemplateUpdateDelayMilliseconds(Integer.MAX_VALUE);

        // Hold 20 templates as strong references as recommended by:
        // http://freemarker.sourceforge.net/docs/pgui_config_templateloading.html
        configuration.setCacheStorage(new freemarker.cache.MruCacheStorage(20, Integer.MAX_VALUE));
    }

    // set global template variables
    configuration.setSharedVariable("contextPath", new SimpleScalar(router.getContextPath()));
    configuration.setSharedVariable("appPath", new SimpleScalar(router.getApplicationPath()));

    webjarResourcesMethod = new WebjarsAtMethod(router);
    publicResourcesMethod = new PublicAtMethod(router);

    // allow custom initialization
    init(application, configuration);
}