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.util.Assert; 023import org.springframework.web.context.WebApplicationContext; 024import org.springframework.web.util.WebUtils; 025 026/** 027 * Convenience methods to retrieve Spring's root {@link WebApplicationContext} 028 * for a given JSF {@link FacesContext}. This is useful for accessing a 029 * Spring application context from custom JSF-based code. 030 * 031 * <p>Analogous to Spring's WebApplicationContextUtils for the ServletContext. 032 * 033 * @author Juergen Hoeller 034 * @since 1.1 035 * @see org.springframework.web.context.ContextLoader 036 * @see org.springframework.web.context.support.WebApplicationContextUtils 037 */ 038public abstract class FacesContextUtils { 039 040 /** 041 * Find the root {@link WebApplicationContext} for this web app, typically 042 * loaded via {@link org.springframework.web.context.ContextLoaderListener}. 043 * <p>Will rethrow an exception that happened on root context startup, 044 * to differentiate between a failed context startup and no context at all. 045 * @param fc the FacesContext to find the web application context for 046 * @return the root WebApplicationContext for this web app, or {@code null} if none 047 * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 048 */ 049 public static WebApplicationContext getWebApplicationContext(FacesContext fc) { 050 Assert.notNull(fc, "FacesContext must not be null"); 051 Object attr = fc.getExternalContext().getApplicationMap().get( 052 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 053 if (attr == null) { 054 return null; 055 } 056 if (attr instanceof RuntimeException) { 057 throw (RuntimeException) attr; 058 } 059 if (attr instanceof Error) { 060 throw (Error) attr; 061 } 062 if (!(attr instanceof WebApplicationContext)) { 063 throw new IllegalStateException("Root context attribute is not of type WebApplicationContext: " + attr); 064 } 065 return (WebApplicationContext) attr; 066 } 067 068 /** 069 * Find the root {@link WebApplicationContext} for this web app, typically 070 * loaded via {@link org.springframework.web.context.ContextLoaderListener}. 071 * <p>Will rethrow an exception that happened on root context startup, 072 * to differentiate between a failed context startup and no context at all. 073 * @param fc the FacesContext to find the web application context for 074 * @return the root WebApplicationContext for this web app 075 * @throws IllegalStateException if the root WebApplicationContext could not be found 076 * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 077 */ 078 public static WebApplicationContext getRequiredWebApplicationContext(FacesContext fc) throws IllegalStateException { 079 WebApplicationContext wac = getWebApplicationContext(fc); 080 if (wac == null) { 081 throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?"); 082 } 083 return wac; 084 } 085 086 /** 087 * Return the best available mutex for the given session: 088 * that is, an object to synchronize on for the given session. 089 * <p>Returns the session mutex attribute if available; usually, 090 * this means that the HttpSessionMutexListener needs to be defined 091 * in {@code web.xml}. Falls back to the Session reference itself 092 * if no mutex attribute found. 093 * <p>The session mutex is guaranteed to be the same object during 094 * the entire lifetime of the session, available under the key defined 095 * by the {@code SESSION_MUTEX_ATTRIBUTE} constant. It serves as a 096 * safe reference to synchronize on for locking on the current session. 097 * <p>In many cases, the Session reference itself is a safe mutex 098 * as well, since it will always be the same object reference for the 099 * same active logical session. However, this is not guaranteed across 100 * different servlet containers; the only 100% safe way is a session mutex. 101 * @param fc the FacesContext to find the session mutex for 102 * @return the mutex object (never {@code null}) 103 * @see org.springframework.web.util.WebUtils#SESSION_MUTEX_ATTRIBUTE 104 * @see org.springframework.web.util.HttpSessionMutexListener 105 */ 106 public static Object getSessionMutex(FacesContext fc) { 107 Assert.notNull(fc, "FacesContext must not be null"); 108 ExternalContext ec = fc.getExternalContext(); 109 Object mutex = ec.getSessionMap().get(WebUtils.SESSION_MUTEX_ATTRIBUTE); 110 if (mutex == null) { 111 mutex = ec.getSession(true); 112 } 113 return mutex; 114 } 115 116}