001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.jsf; 018 019import javax.faces.context.ExternalContext; 020import javax.faces.context.FacesContext; 021 022import org.springframework.lang.Nullable; 023import org.springframework.util.Assert; 024import org.springframework.web.context.WebApplicationContext; 025import org.springframework.web.util.WebUtils; 026 027/** 028 * Convenience methods to retrieve Spring's root {@link WebApplicationContext} 029 * for a given JSF {@link FacesContext}. This is useful for accessing a 030 * Spring application context from custom JSF-based code. 031 * 032 * <p>Analogous to Spring's WebApplicationContextUtils for the ServletContext. 033 * 034 * @author Juergen Hoeller 035 * @since 1.1 036 * @see org.springframework.web.context.ContextLoader 037 * @see org.springframework.web.context.support.WebApplicationContextUtils 038 */ 039public abstract class FacesContextUtils { 040 041 /** 042 * Find the root {@link WebApplicationContext} for this web app, typically 043 * loaded via {@link org.springframework.web.context.ContextLoaderListener}. 044 * <p>Will rethrow an exception that happened on root context startup, 045 * to differentiate between a failed context startup and no context at all. 046 * @param fc the FacesContext to find the web application context for 047 * @return the root WebApplicationContext for this web app, or {@code null} if none 048 * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 049 */ 050 @Nullable 051 public static WebApplicationContext getWebApplicationContext(FacesContext fc) { 052 Assert.notNull(fc, "FacesContext must not be null"); 053 Object attr = fc.getExternalContext().getApplicationMap().get( 054 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 055 if (attr == null) { 056 return null; 057 } 058 if (attr instanceof RuntimeException) { 059 throw (RuntimeException) attr; 060 } 061 if (attr instanceof Error) { 062 throw (Error) attr; 063 } 064 if (!(attr instanceof WebApplicationContext)) { 065 throw new IllegalStateException("Root context attribute is not of type WebApplicationContext: " + attr); 066 } 067 return (WebApplicationContext) attr; 068 } 069 070 /** 071 * Find the root {@link WebApplicationContext} for this web app, typically 072 * loaded via {@link org.springframework.web.context.ContextLoaderListener}. 073 * <p>Will rethrow an exception that happened on root context startup, 074 * to differentiate between a failed context startup and no context at all. 075 * @param fc the FacesContext to find the web application context for 076 * @return the root WebApplicationContext for this web app 077 * @throws IllegalStateException if the root WebApplicationContext could not be found 078 * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 079 */ 080 public static WebApplicationContext getRequiredWebApplicationContext(FacesContext fc) throws IllegalStateException { 081 WebApplicationContext wac = getWebApplicationContext(fc); 082 if (wac == null) { 083 throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?"); 084 } 085 return wac; 086 } 087 088 /** 089 * Return the best available mutex for the given session: 090 * that is, an object to synchronize on for the given session. 091 * <p>Returns the session mutex attribute if available; usually, 092 * this means that the HttpSessionMutexListener needs to be defined 093 * in {@code web.xml}. Falls back to the Session reference itself 094 * if no mutex attribute found. 095 * <p>The session mutex is guaranteed to be the same object during 096 * the entire lifetime of the session, available under the key defined 097 * by the {@code SESSION_MUTEX_ATTRIBUTE} constant. It serves as a 098 * safe reference to synchronize on for locking on the current session. 099 * <p>In many cases, the Session reference itself is a safe mutex 100 * as well, since it will always be the same object reference for the 101 * same active logical session. However, this is not guaranteed across 102 * different servlet containers; the only 100% safe way is a session mutex. 103 * @param fc the FacesContext to find the session mutex for 104 * @return the mutex object (never {@code null}) 105 * @see org.springframework.web.util.WebUtils#SESSION_MUTEX_ATTRIBUTE 106 * @see org.springframework.web.util.HttpSessionMutexListener 107 */ 108 @Nullable 109 public static Object getSessionMutex(FacesContext fc) { 110 Assert.notNull(fc, "FacesContext must not be null"); 111 ExternalContext ec = fc.getExternalContext(); 112 Object mutex = ec.getSessionMap().get(WebUtils.SESSION_MUTEX_ATTRIBUTE); 113 if (mutex == null) { 114 mutex = ec.getSession(true); 115 } 116 return mutex; 117 } 118 119}