org.thymeleaf.spring4.context.SpringWebContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thymeleaf-spring4 Show documentation
Show all versions of thymeleaf-spring4 Show documentation
XML/XHTML/HTML5 template engine for Java
/*
* =============================================================================
*
* 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.context;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.thymeleaf.context.WebContext;
/**
*
* Implementation of {@link org.thymeleaf.context.IContext} meant for Spring MVC applications,
* extending {@link WebContext} and adding:
*
*
* - A special beans variable of class ({@link Beans}) that allows users to access beans
* in the application context. This variable can be accessed like any other variable
* in the context: ${beans.myBean.doSomething()}.
* - A reference to the Application Context itself, that can be obtained and used from
* element/attribute processors ({@link #getApplicationContext()}).
*
*
* @author Daniel Fernández
* @author Josh Long
*
* @since 1.0
*
*/
public class SpringWebContext
extends WebContext {
public static final String BEANS_VARIABLE_NAME = "beans";
private static final ConcurrentHashMap> variableMapPrototypes =
new ConcurrentHashMap>();
private final ApplicationContext applicationContext;
/**
*
* Creates a new instance of a SpringWebContext.
*
*
* @param request the request object
* @param response the response object
* @param servletContext the servlet context
* @param locale the locale
* @param variables the variables to be included into the context
* @param appctx the Spring application context
*/
public SpringWebContext(final HttpServletRequest request,
final HttpServletResponse response,
final ServletContext servletContext ,
final Locale locale,
final Map variables,
final ApplicationContext appctx) {
super(request, response, servletContext, locale, addSpringSpecificVariables(variables, appctx));
this.applicationContext = appctx;
}
@SuppressWarnings("unchecked")
private static Map addSpringSpecificVariables(final Map variables, final ApplicationContext appctx) {
HashMap variableMapPrototype = variableMapPrototypes.get(appctx);
if (variableMapPrototype == null) {
variableMapPrototype = new HashMap(20, 1.0f);
// We will use a singleton-per-appctx Beans instance, and that's alright
final Beans beans = new Beans(appctx);
variableMapPrototype.put(BEANS_VARIABLE_NAME, beans);
variableMapPrototypes.put(appctx, variableMapPrototype);
}
final Map newVariables;
synchronized (variableMapPrototype) {
newVariables = (Map) variableMapPrototype.clone();
}
if (variables != null) {
newVariables.putAll(variables);
}
return newVariables;
}
public ApplicationContext getApplicationContext() {
return this.applicationContext;
}
}