Search in sources :

Example 1 with StrongCacheStorage

use of freemarker.cache.StrongCacheStorage in project freemarker by apache.

the class TemplateLevelSettings method renderWith.

private String renderWith(Version version, String mainBoolFmt, String incBoolFmt, String impBoolFtm) throws IOException, TemplateException {
    Configuration cfg = new Configuration(version);
    cfg.setTemplateLoader(TEMPLATES);
    cfg.setCacheStorage(new StrongCacheStorage());
    cfg.setBooleanFormat("C,c");
    if (incBoolFmt != null) {
        cfg.getTemplate(INCLUDED_FTL).setBooleanFormat(incBoolFmt);
    }
    if (impBoolFtm != null) {
        cfg.getTemplate(IMPORTED_FTL).setBooleanFormat(impBoolFtm);
    }
    Template t = cfg.getTemplate(MAIN_FTL);
    if (mainBoolFmt != null) {
        t.setBooleanFormat(mainBoolFmt);
    }
    StringWriter sw = new StringWriter();
    t.process(null, sw);
    return sw.toString();
}
Also used : Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) StrongCacheStorage(freemarker.cache.StrongCacheStorage) Template(freemarker.template.Template)

Example 2 with StrongCacheStorage

use of freemarker.cache.StrongCacheStorage in project freemarker by apache.

the class ConfigurationTest method testChangingTemplateNameFormatClearsCache.

public void testChangingTemplateNameFormatClearsCache() throws Exception {
    Configuration cfg = new Configuration();
    cfg.setCacheStorage(new StrongCacheStorage());
    CacheStorageWithGetSize cache = (CacheStorageWithGetSize) cfg.getCacheStorage();
    cache = (CacheStorageWithGetSize) cfg.getCacheStorage();
    assertEquals(0, cache.getSize());
    cfg.setClassForTemplateLoading(ConfigurationTest.class, "");
    assertEquals(0, cache.getSize());
    cfg.getTemplate("toCache1.ftl");
    assertEquals(1, cache.getSize());
    cfg.setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_3_0);
    assertEquals(1, cache.getSize());
    cfg.setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_4_0);
    assertEquals(0, cache.getSize());
    cfg.getTemplate("toCache1.ftl");
    assertEquals(1, cache.getSize());
    cfg.setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_4_0);
    assertEquals(1, cache.getSize());
    cfg.setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_3_0);
    assertEquals(0, cache.getSize());
}
Also used : CacheStorageWithGetSize(freemarker.cache.CacheStorageWithGetSize) StrongCacheStorage(freemarker.cache.StrongCacheStorage)

Example 3 with StrongCacheStorage

use of freemarker.cache.StrongCacheStorage in project freemarker by apache.

the class ConfigurationTest method testSetTemplateLoaderAndCache.

public void testSetTemplateLoaderAndCache() throws Exception {
    Configuration cfg = new Configuration();
    CacheStorageWithGetSize cacheStorage = (CacheStorageWithGetSize) cfg.getCacheStorage();
    assertEquals(0, cacheStorage.getSize());
    cfg.setCacheStorage(new StrongCacheStorage());
    cacheStorage = (CacheStorageWithGetSize) cfg.getCacheStorage();
    assertEquals(0, cacheStorage.getSize());
    cfg.setClassForTemplateLoading(ConfigurationTest.class, "");
    assertEquals(0, cacheStorage.getSize());
    cfg.getTemplate("toCache1.ftl");
    assertEquals(1, cacheStorage.getSize());
    cfg.getTemplate("toCache2.ftl");
    assertEquals(2, cacheStorage.getSize());
    cfg.setClassForTemplateLoading(ConfigurationTest.class, "");
    assertEquals(0, cacheStorage.getSize());
    cfg.getTemplate("toCache1.ftl");
    assertEquals(1, cacheStorage.getSize());
    cfg.setTemplateLoader(cfg.getTemplateLoader());
    assertEquals(1, cacheStorage.getSize());
}
Also used : CacheStorageWithGetSize(freemarker.cache.CacheStorageWithGetSize) StrongCacheStorage(freemarker.cache.StrongCacheStorage)

Example 4 with StrongCacheStorage

use of freemarker.cache.StrongCacheStorage in project alfresco-remote-api by Alfresco.

the class RepositoryTemplateProcessor method initConfig.

/**
 * Initialise FreeMarker Configuration
 */
protected void initConfig() {
    Configuration config = new Configuration();
    // setup template cache
    config.setCacheStorage(new StrongCacheStorage());
    config.setTemplateUpdateDelay(updateDelay);
    // setup template loaders
    List<TemplateLoader> loaders = new ArrayList<TemplateLoader>();
    for (Store apiStore : searchPath.getStores()) {
        TemplateLoader loader = apiStore.getTemplateLoader();
        if (loader == null) {
            throw new WebScriptException("Unable to retrieve template loader for Web Script store " + apiStore.getBasePath());
        }
        loaders.add(loader);
    }
    MultiTemplateLoader loader = new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()]));
    config.setTemplateLoader(loader);
    // use our custom object wrapper that can deal with QNameMap objects directly
    config.setObjectWrapper(new QNameAwareObjectWrapper());
    // rethrow any exception so we can deal with them
    config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    // turn off locale sensitive lookup - to save numerous wasted calls to nodeservice.exists()
    config.setLocalizedLookup(false);
    // set template encoding
    if (defaultEncoding != null) {
        config.setDefaultEncoding(defaultEncoding);
    }
    // set output encoding
    config.setOutputEncoding("UTF-8");
    config.setIncompatibleImprovements(new Version(2, 3, 20));
    config.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
    templateConfig = config;
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) Configuration(freemarker.template.Configuration) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) Version(freemarker.template.Version) TemplateLoader(freemarker.cache.TemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) ArrayList(java.util.ArrayList) Store(org.springframework.extensions.webscripts.Store) StrongCacheStorage(freemarker.cache.StrongCacheStorage) QNameAwareObjectWrapper(org.alfresco.repo.template.QNameAwareObjectWrapper)

Example 5 with StrongCacheStorage

use of freemarker.cache.StrongCacheStorage in project freemarker by apache.

the class ConfigurationTest method testChangingLocalizedLookupClearsCache.

public void testChangingLocalizedLookupClearsCache() throws Exception {
    Configuration cfg = new Configuration();
    cfg.setCacheStorage(new StrongCacheStorage());
    CacheStorageWithGetSize cache = (CacheStorageWithGetSize) cfg.getCacheStorage();
    cache = (CacheStorageWithGetSize) cfg.getCacheStorage();
    assertEquals(0, cache.getSize());
    cfg.setClassForTemplateLoading(ConfigurationTest.class, "");
    assertEquals(0, cache.getSize());
    cfg.getTemplate("toCache1.ftl");
    assertEquals(1, cache.getSize());
    cfg.setLocalizedLookup(true);
    assertEquals(1, cache.getSize());
    cfg.setLocalizedLookup(false);
    assertEquals(0, cache.getSize());
    cfg.getTemplate("toCache1.ftl");
    assertEquals(1, cache.getSize());
    cfg.setLocalizedLookup(false);
    assertEquals(1, cache.getSize());
    cfg.setLocalizedLookup(true);
    assertEquals(0, cache.getSize());
}
Also used : CacheStorageWithGetSize(freemarker.cache.CacheStorageWithGetSize) StrongCacheStorage(freemarker.cache.StrongCacheStorage)

Aggregations

StrongCacheStorage (freemarker.cache.StrongCacheStorage)5 CacheStorageWithGetSize (freemarker.cache.CacheStorageWithGetSize)3 Configuration (freemarker.template.Configuration)2 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)1 TemplateLoader (freemarker.cache.TemplateLoader)1 Template (freemarker.template.Template)1 Version (freemarker.template.Version)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 QNameAwareObjectWrapper (org.alfresco.repo.template.QNameAwareObjectWrapper)1 Store (org.springframework.extensions.webscripts.Store)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1