Improved integration with Groovy 2.3.1
Groovy 2.3.1 has a new template loader abstraction that handles compiler caching (better performance by factor of 10).
This commit is contained in:
		
							parent
							
								
									85ac8f6b05
								
							
						
					
					
						commit
						e964b9eb34
					
				| 
						 | 
				
			
			@ -24,6 +24,7 @@ import org.springframework.context.ApplicationEvent;
 | 
			
		|||
 * 
 | 
			
		||||
 * @author Dave Syer
 | 
			
		||||
 */
 | 
			
		||||
@SuppressWarnings("serial")
 | 
			
		||||
public class JobExecutionEvent extends ApplicationEvent {
 | 
			
		||||
 | 
			
		||||
	private final JobExecution execution;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,7 +17,6 @@
 | 
			
		|||
package org.springframework.boot.autoconfigure.groovy.template;
 | 
			
		||||
 | 
			
		||||
import groovy.text.TemplateEngine;
 | 
			
		||||
import groovy.text.markup.BaseTemplate;
 | 
			
		||||
import groovy.text.markup.MarkupTemplateEngine;
 | 
			
		||||
import groovy.text.markup.TemplateConfiguration;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -28,6 +27,7 @@ import java.util.List;
 | 
			
		|||
 | 
			
		||||
import javax.servlet.Servlet;
 | 
			
		||||
 | 
			
		||||
import groovy.text.markup.TemplateResolver;
 | 
			
		||||
import org.springframework.beans.factory.BeanClassLoaderAware;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 | 
			
		||||
| 
						 | 
				
			
			@ -35,7 +35,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 | 
			
		|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
 | 
			
		||||
import org.springframework.boot.autoconfigure.groovy.template.web.GroovyTemplateViewResolver;
 | 
			
		||||
import org.springframework.boot.autoconfigure.groovy.template.web.LocaleAwareTemplate;
 | 
			
		||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
 | 
			
		||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
 | 
			
		||||
import org.springframework.context.ApplicationContext;
 | 
			
		||||
| 
						 | 
				
			
			@ -89,12 +88,9 @@ public class GroovyTemplateAutoConfiguration {
 | 
			
		|||
		@ConditionalOnMissingBean(TemplateEngine.class)
 | 
			
		||||
		public TemplateEngine groovyTemplateEngine() throws Exception {
 | 
			
		||||
			TemplateConfiguration configuration = this.properties.getConfiguration();
 | 
			
		||||
			if (configuration.getBaseTemplateClass() == BaseTemplate.class) {
 | 
			
		||||
				// Enable locale-dependent includes
 | 
			
		||||
				configuration.setBaseTemplateClass(LocaleAwareTemplate.class);
 | 
			
		||||
			}
 | 
			
		||||
			return new MarkupTemplateEngine(createParentLoaderForTemplates(),
 | 
			
		||||
					configuration);
 | 
			
		||||
 | 
			
		||||
            return new MarkupTemplateEngine(createParentLoaderForTemplates(),
 | 
			
		||||
                    configuration, new GroovyTemplateResolver());
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		private ClassLoader createParentLoaderForTemplates() throws Exception {
 | 
			
		||||
| 
						 | 
				
			
			@ -132,6 +128,7 @@ public class GroovyTemplateAutoConfiguration {
 | 
			
		|||
 | 
			
		||||
			return resolver;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,55 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2003-2012 the original author or authors.
 | 
			
		||||
 *
 | 
			
		||||
 * 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.springframework.boot.autoconfigure.groovy.template;
 | 
			
		||||
 | 
			
		||||
import groovy.text.markup.MarkupTemplateEngine;
 | 
			
		||||
import groovy.text.markup.TemplateConfiguration;
 | 
			
		||||
import groovy.text.markup.TemplateResolver;
 | 
			
		||||
import org.springframework.context.i18n.LocaleContextHolder;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A custom {@link groovy.text.markup.TemplateResolver template resolver} which resolves templates using the locale
 | 
			
		||||
 * found in the thread locale. This resolver ignores the template engine configuration locale.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Cédric Champeau
 | 
			
		||||
 * @since 1.1.0
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
public class GroovyTemplateResolver implements TemplateResolver {
 | 
			
		||||
    private ClassLoader templateClassLoader;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void configure(final ClassLoader templateClassLoader, final TemplateConfiguration configuration) {
 | 
			
		||||
        this.templateClassLoader = templateClassLoader;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public URL resolveTemplate(final String templatePath) throws IOException {
 | 
			
		||||
        MarkupTemplateEngine.TemplateResource templateResource = MarkupTemplateEngine.TemplateResource.parse(templatePath);
 | 
			
		||||
        URL resource = templateClassLoader.getResource(templateResource.withLocale(LocaleContextHolder.getLocale().toString().replace("-", "_")).toString());
 | 
			
		||||
        if (resource == null) {
 | 
			
		||||
            // no resource found with the default locale, try without any locale
 | 
			
		||||
            resource = templateClassLoader.getResource(templateResource.withLocale(null).toString());
 | 
			
		||||
        }
 | 
			
		||||
        if (resource == null) {
 | 
			
		||||
            throw new IOException("Unable to load template:" + templatePath);
 | 
			
		||||
        }
 | 
			
		||||
        return resource;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,50 +0,0 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2012-2013 the original author or authors.
 | 
			
		||||
 *
 | 
			
		||||
 * 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.springframework.boot.autoconfigure.groovy.template.web;
 | 
			
		||||
 | 
			
		||||
import groovy.text.markup.BaseTemplate;
 | 
			
		||||
import groovy.text.markup.MarkupTemplateEngine;
 | 
			
		||||
import groovy.text.markup.TemplateConfiguration;
 | 
			
		||||
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import org.springframework.context.i18n.LocaleContextHolder;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author Dave Syer
 | 
			
		||||
 */
 | 
			
		||||
public abstract class LocaleAwareTemplate extends BaseTemplate {
 | 
			
		||||
 | 
			
		||||
	public LocaleAwareTemplate(MarkupTemplateEngine templateEngine, Map<?, ?> model,
 | 
			
		||||
			Map<String, String> modelTypes, TemplateConfiguration configuration) {
 | 
			
		||||
		super(localize(templateEngine), model, modelTypes, localize(configuration));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static MarkupTemplateEngine localize(MarkupTemplateEngine templateEngine) {
 | 
			
		||||
		TemplateConfiguration templateConfiguration = templateEngine
 | 
			
		||||
				.getTemplateConfiguration();
 | 
			
		||||
		ClassLoader parent = templateEngine.getTemplateLoader().getParent();
 | 
			
		||||
		return new MarkupTemplateEngine(parent, localize(templateConfiguration));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static TemplateConfiguration localize(TemplateConfiguration configuration) {
 | 
			
		||||
		TemplateConfiguration result = new TemplateConfiguration(configuration);
 | 
			
		||||
		result.setLocale(LocaleContextHolder.getLocale());
 | 
			
		||||
		return result;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -57,7 +57,7 @@
 | 
			
		|||
		<freemarker.version>2.3.20</freemarker.version>
 | 
			
		||||
		<gemfire.version>7.0.2</gemfire.version>
 | 
			
		||||
		<gradle.version>1.6</gradle.version>
 | 
			
		||||
		<groovy.version>2.3.0</groovy.version>
 | 
			
		||||
		<groovy.version>2.3.1</groovy.version>
 | 
			
		||||
		<h2.version>1.3.175</h2.version>
 | 
			
		||||
		<hamcrest.version>1.3</hamcrest.version>
 | 
			
		||||
		<hibernate.version>4.3.1.Final</hibernate.version>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue