From 540aa090d380eef38b8a45b54b90a3ee4ac9ecf7 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 23 Oct 2012 19:00:48 -0400 Subject: [PATCH] Avoid releasing FacesContext in PropertyAccessor Issue: SPR-1558 --- .../JsfManagedBeanPropertyAccessor.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/JsfManagedBeanPropertyAccessor.java b/spring-faces/src/main/java/org/springframework/faces/webflow/JsfManagedBeanPropertyAccessor.java index 4843324f..8268a7f5 100644 --- a/spring-faces/src/main/java/org/springframework/faces/webflow/JsfManagedBeanPropertyAccessor.java +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/JsfManagedBeanPropertyAccessor.java @@ -44,10 +44,10 @@ import org.springframework.webflow.execution.RequestContextHolder; *

* Source code adapted from {@link JsfManagedBeanResolver}. *

- * + * * @author Jeremy Grelle * @author Rossen Stoyanchev - * + * * @since 2.1 */ public class JsfManagedBeanPropertyAccessor implements PropertyAccessor { @@ -77,8 +77,8 @@ public class JsfManagedBeanPropertyAccessor implements PropertyAccessor { /** * Locates a JSF managed bean through a temporary FacesContext. This method is only meant to be called from the Flow - * Execution. It assumes the FacesContext will not be available and creates a temporary one on the fly. - * + * Execution. If a FacesContext is not available, a temporary will be created on the fly. + * * @param name The name of the bean to resolve. * @return The JSF Managed Bean instance if found. */ @@ -86,14 +86,21 @@ public class JsfManagedBeanPropertyAccessor implements PropertyAccessor { RequestContext requestContext = RequestContextHolder.getRequestContext(); Assert.notNull(requestContext, "RequestContext cannot be null. " + "This PropertyAccessor is only intended to be invoked from an active Flow Execution."); - FacesContext facesContext = FlowFacesContext.newInstance(requestContext, FlowLifecycle.newInstance()); + boolean releaseFacesContext = false; + FacesContext facesContext = FacesContext.getCurrentInstance(); + if (facesContext == null) { + releaseFacesContext = true; + facesContext = FlowFacesContext.newInstance(requestContext, FlowLifecycle.newInstance()); + } try { ExpressionFactory factory = facesContext.getApplication().getExpressionFactory(); ELContext elContext = facesContext.getELContext(); ValueExpression expression = factory.createValueExpression(elContext, "#{" + name + "}", Object.class); return expression.getValue(facesContext.getELContext()); } finally { - facesContext.release(); + if (releaseFacesContext) { + facesContext.release(); + } } }