Search in sources :

Example 1 with ICacheManager

use of org.thymeleaf.cache.ICacheManager in project thymeleaf by thymeleaf.

the class OGNLShortcutExpression method evaluate.

Object evaluate(final IEngineConfiguration configuration, final Map<String, Object> context, final Object root) throws Exception {
    final ICacheManager cacheManager = configuration.getCacheManager();
    final ICache<ExpressionCacheKey, Object> expressionCache = (cacheManager == null ? null : cacheManager.getExpressionCache());
    Object target = root;
    for (final String propertyName : this.expressionLevels) {
        // If target is null, we will mimic what OGNL does in these cases...
        if (target == null) {
            throw new OgnlException("source is null for getProperty(null, \"" + propertyName + "\")");
        }
        // For the best integration possible, we will ask OGNL which property accessor it would use for
        // this target object, and then depending on the result apply our equivalent or just default to
        // OGNL evaluation if it is a custom property accessor we do not implement.
        final Class<?> targetClass = OgnlRuntime.getTargetClass(target);
        final PropertyAccessor ognlPropertyAccessor = OgnlRuntime.getPropertyAccessor(targetClass);
        // Depending on the returned OGNL property accessor, we will try to apply ours
        if (target instanceof Class<?>) {
            // Because of the way OGNL works, the "OgnlRuntime.getTargetClass(...)" of a Class object is the class
            // object itself, so we might be trying to apply a PropertyAccessor to a Class instead of a real object,
            // something we avoid by means of this shortcut
            target = getObjectProperty(expressionCache, propertyName, target);
        } else if (OGNLContextPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getContextProperty(propertyName, context, target);
        } else if (ObjectPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getObjectProperty(expressionCache, propertyName, target);
        } else if (MapPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getMapProperty(propertyName, (Map<?, ?>) target);
        } else if (ListPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getListProperty(expressionCache, propertyName, (List<?>) target);
        } else if (SetPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getSetProperty(expressionCache, propertyName, (Set<?>) target);
        } else if (IteratorPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getIteratorProperty(expressionCache, propertyName, (Iterator<?>) target);
        } else if (EnumerationPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getEnumerationProperty(expressionCache, propertyName, (Enumeration<?>) target);
        } else if (ArrayPropertyAccessor.class.equals(ognlPropertyAccessor.getClass())) {
            target = getArrayProperty(expressionCache, propertyName, (Object[]) target);
        } else {
            // default to normal OGNL evaluation.
            throw new OGNLShortcutExpressionNotApplicableException();
        }
    }
    return target;
}
Also used : EnumerationPropertyAccessor(ognl.EnumerationPropertyAccessor) PropertyAccessor(ognl.PropertyAccessor) ListPropertyAccessor(ognl.ListPropertyAccessor) ObjectPropertyAccessor(ognl.ObjectPropertyAccessor) MapPropertyAccessor(ognl.MapPropertyAccessor) ArrayPropertyAccessor(ognl.ArrayPropertyAccessor) EnumerationPropertyAccessor(ognl.EnumerationPropertyAccessor) IteratorPropertyAccessor(ognl.IteratorPropertyAccessor) SetPropertyAccessor(ognl.SetPropertyAccessor) ExpressionCacheKey(org.thymeleaf.cache.ExpressionCacheKey) MapPropertyAccessor(ognl.MapPropertyAccessor) OgnlException(ognl.OgnlException) Iterator(java.util.Iterator) List(java.util.List) SetPropertyAccessor(ognl.SetPropertyAccessor) ICacheManager(org.thymeleaf.cache.ICacheManager)

Example 2 with ICacheManager

use of org.thymeleaf.cache.ICacheManager in project thymeleaf by thymeleaf.

the class ConfigurationPrinterHelper method printConfiguration.

static void printConfiguration(final IEngineConfiguration configuration) {
    final ConfigLogBuilder logBuilder = new ConfigLogBuilder();
    final ICacheManager cacheManager = configuration.getCacheManager();
    final Set<ITemplateResolver> templateResolvers = configuration.getTemplateResolvers();
    final Set<IMessageResolver> messageResolvers = configuration.getMessageResolvers();
    final Set<ILinkBuilder> linkBuilders = configuration.getLinkBuilders();
    logBuilder.line("Initializing Thymeleaf Template engine configuration...");
    logBuilder.line("[THYMELEAF] TEMPLATE ENGINE CONFIGURATION:");
    if (!StringUtils.isEmptyOrWhitespace(Thymeleaf.VERSION)) {
        if (!StringUtils.isEmptyOrWhitespace(Thymeleaf.BUILD_TIMESTAMP)) {
            logBuilder.line("[THYMELEAF] * Thymeleaf version: {} (built {})", Thymeleaf.VERSION, Thymeleaf.BUILD_TIMESTAMP);
        } else {
            logBuilder.line("[THYMELEAF] * Thymeleaf version: {}", Thymeleaf.VERSION);
        }
    }
    logBuilder.line("[THYMELEAF] * Cache Manager implementation: {}", (cacheManager == null ? "[no caches]" : cacheManager.getClass().getName()));
    logBuilder.line("[THYMELEAF] * Template resolvers:");
    for (final ITemplateResolver templateResolver : templateResolvers) {
        if (templateResolver.getOrder() != null) {
            logBuilder.line("[THYMELEAF]     * [{}] {}", templateResolver.getOrder(), templateResolver.getName());
        } else {
            logBuilder.line("[THYMELEAF]     * {}", templateResolver.getName());
        }
    }
    logBuilder.line("[THYMELEAF] * Message resolvers:");
    for (final IMessageResolver messageResolver : messageResolvers) {
        if (messageResolver.getOrder() != null) {
            logBuilder.line("[THYMELEAF]     * [{}] {}", messageResolver.getOrder(), messageResolver.getName());
        } else {
            logBuilder.line("[THYMELEAF]     * {}", messageResolver.getName());
        }
    }
    logBuilder.line("[THYMELEAF] * Link builders:");
    for (final ILinkBuilder linkBuilder : linkBuilders) {
        if (linkBuilder.getOrder() != null) {
            logBuilder.line("[THYMELEAF]     * [{}] {}", linkBuilder.getOrder(), linkBuilder.getName());
        } else {
            logBuilder.line("[THYMELEAF]     * {}", linkBuilder.getName());
        }
    }
    final Set<DialectConfiguration> dialectConfigurations = configuration.getDialectConfigurations();
    int dialectIndex = 1;
    final Integer totalDialects = Integer.valueOf(dialectConfigurations.size());
    for (final DialectConfiguration dialectConfiguration : dialectConfigurations) {
        final IDialect dialect = dialectConfiguration.getDialect();
        if (totalDialects.intValue() > 1) {
            logBuilder.line("[THYMELEAF] * Dialect [{} of {}]: {} ({})", new Object[] { Integer.valueOf(dialectIndex), totalDialects, dialect.getName(), dialect.getClass().getName() });
        } else {
            logBuilder.line("[THYMELEAF] * Dialect: {} ({})", dialect.getName(), dialect.getClass().getName());
        }
        String dialectPrefix = null;
        if (dialect instanceof IProcessorDialect) {
            dialectPrefix = (dialectConfiguration.isPrefixSpecified() ? dialectConfiguration.getPrefix() : ((IProcessorDialect) dialect).getPrefix());
            logBuilder.line("[THYMELEAF]     * Prefix: \"{}\"", (dialectPrefix != null ? dialectPrefix : "(none)"));
        }
        if (configLogger.isDebugEnabled()) {
            printDebugConfiguration(logBuilder, dialect, dialectPrefix);
        }
        dialectIndex++;
    }
    logBuilder.end("[THYMELEAF] TEMPLATE ENGINE CONFIGURED OK");
    /*
         * The following condition makes sense because contents in each case will differ a lot.
         */
    if (configLogger.isTraceEnabled()) {
        configLogger.trace(logBuilder.toString());
    } else if (configLogger.isDebugEnabled()) {
        configLogger.debug(logBuilder.toString());
    }
}
Also used : ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver) IProcessorDialect(org.thymeleaf.dialect.IProcessorDialect) IDialect(org.thymeleaf.dialect.IDialect) IMessageResolver(org.thymeleaf.messageresolver.IMessageResolver) ILinkBuilder(org.thymeleaf.linkbuilder.ILinkBuilder) ICacheManager(org.thymeleaf.cache.ICacheManager)

Aggregations

ICacheManager (org.thymeleaf.cache.ICacheManager)2 Iterator (java.util.Iterator)1 List (java.util.List)1 ArrayPropertyAccessor (ognl.ArrayPropertyAccessor)1 EnumerationPropertyAccessor (ognl.EnumerationPropertyAccessor)1 IteratorPropertyAccessor (ognl.IteratorPropertyAccessor)1 ListPropertyAccessor (ognl.ListPropertyAccessor)1 MapPropertyAccessor (ognl.MapPropertyAccessor)1 ObjectPropertyAccessor (ognl.ObjectPropertyAccessor)1 OgnlException (ognl.OgnlException)1 PropertyAccessor (ognl.PropertyAccessor)1 SetPropertyAccessor (ognl.SetPropertyAccessor)1 ExpressionCacheKey (org.thymeleaf.cache.ExpressionCacheKey)1 IDialect (org.thymeleaf.dialect.IDialect)1 IProcessorDialect (org.thymeleaf.dialect.IProcessorDialect)1 ILinkBuilder (org.thymeleaf.linkbuilder.ILinkBuilder)1 IMessageResolver (org.thymeleaf.messageresolver.IMessageResolver)1 ITemplateResolver (org.thymeleaf.templateresolver.ITemplateResolver)1