All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.thymeleaf.spring4.messageresolver.SpringNonCacheableMessageResolver Maven / Gradle / Ivy

There is a newer version: 3.0.15.RELEASE
Show newest version
/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2014, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package org.thymeleaf.spring4.messageresolver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.NoSuchMessageException;
import org.thymeleaf.Arguments;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.exceptions.ConfigurationException;
import org.thymeleaf.messageresolver.AbstractMessageResolver;
import org.thymeleaf.messageresolver.MessageResolution;
import org.thymeleaf.util.Validate;



/**
 * 

* Implementation of {@link org.thymeleaf.messageresolver.IMessageResolver} that * integrates the standard Spring way of resolving messages into Thymeleaf. *

*

* This resolution is done by means of using the available Spring-configured * {@link MessageSource} objects. *

*

* This message resolver will consider you are using a * reloadable MessageSource in your Spring * configuration and thus will be considered non-cacheable. *

* * @author Daniel Fernández * * @since 1.0 * */ public final class SpringNonCacheableMessageResolver extends AbstractMessageResolver implements MessageSourceAware { private static final Logger logger = LoggerFactory.getLogger(SpringNonCacheableMessageResolver.class); private MessageSource messageSource; public SpringNonCacheableMessageResolver() { super(); } @Override protected void initializeSpecific() { /* * Check the application context has been set. */ if (this.messageSource == null) { throw new ConfigurationException( "Cannot initialize " + SpringNonCacheableMessageResolver.class.getSimpleName() + ": MessageSource has not been set. Either define this object as " + "a Spring bean (which will automatically set the MessageSource) or, " + "if you instance it directly, set the MessageSource manually using its "+ "corresponding setter method."); } } /** *

* Returns the message source ({@link MessageSource}) to be * used for message resolution. *

* * @return the message source */ public MessageSource getMessageSource() { checkInitialized(); return this.messageSource; } /** *

* Uninitialized method meant for use by subclasses. *

* * @return the message source */ protected MessageSource unsafeGetMessageSource() { return this.messageSource; } /** *

* Sets the message source to be used for message resolution *

* * @param messageSource the message source */ public void setMessageSource(final MessageSource messageSource) { checkNotInitialized(); this.messageSource = messageSource; } public MessageResolution resolveMessage( final Arguments arguments, final String key, final Object[] messageParameters) { Validate.notNull(arguments, "Arguments cannot be null"); Validate.notNull(arguments.getContext().getLocale(), "Locale in context cannot be null"); Validate.notNull(key, "Message key cannot be null"); if (logger.isTraceEnabled()) { logger.trace("[THYMELEAF][{}] Resolving message with key \"{}\" for template \"{}\" and locale \"{}\". Messages will be retrieved from Spring's MessageSource infrastructure.", new Object[] {TemplateEngine.threadIndex(), key, arguments.getTemplateName(), arguments.getContext().getLocale()}); } try { final String resolvedMessage = this.messageSource.getMessage(key, messageParameters, arguments.getContext().getLocale()); return new MessageResolution(resolvedMessage); } catch (NoSuchMessageException e) { return null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy