001/* 002 * Copyright 2002-2012 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 java.util.Collection; 020import javax.faces.context.FacesContext; 021import javax.faces.event.PhaseEvent; 022import javax.faces.event.PhaseId; 023import javax.faces.event.PhaseListener; 024 025import org.springframework.beans.factory.BeanFactoryUtils; 026import org.springframework.beans.factory.ListableBeanFactory; 027import org.springframework.web.context.WebApplicationContext; 028 029/** 030 * JSF PhaseListener implementation that delegates to one or more Spring-managed 031 * PhaseListener beans coming from the Spring root WebApplicationContext. 032 * 033 * <p>Configure this listener multicaster in your {@code faces-config.xml} file 034 * as follows: 035 * 036 * <pre class="code"> 037 * <application> 038 * ... 039 * <phase-listener> 040 * org.springframework.web.jsf.DelegatingPhaseListenerMulticaster 041 * </phase-listener> 042 * ... 043 * </application></pre> 044 * 045 * The multicaster will delegate all {@code beforePhase} and {@code afterPhase} 046 * events to all target PhaseListener beans. By default, those will simply be obtained 047 * by type: All beans in the Spring root WebApplicationContext that implement the 048 * PhaseListener interface will be fetched and invoked. 049 * 050 * <p>Note: This multicaster's {@code getPhaseId()} method will always return 051 * {@code ANY_PHASE}. <b>The phase id exposed by the target listener beans 052 * will be ignored; all events will be propagated to all listeners.</b> 053 * 054 * <p>This multicaster may be subclassed to change the strategy used to obtain 055 * the listener beans, or to change the strategy used to access the ApplicationContext 056 * (normally obtained via {@link FacesContextUtils#getWebApplicationContext(FacesContext)}). 057 * 058 * @author Juergen Hoeller 059 * @author Colin Sampaleanu 060 * @since 1.2.7 061 */ 062@SuppressWarnings("serial") 063public class DelegatingPhaseListenerMulticaster implements PhaseListener { 064 065 @Override 066 public PhaseId getPhaseId() { 067 return PhaseId.ANY_PHASE; 068 } 069 070 @Override 071 public void beforePhase(PhaseEvent event) { 072 for (PhaseListener listener : getDelegates(event.getFacesContext())) { 073 listener.beforePhase(event); 074 } 075 } 076 077 @Override 078 public void afterPhase(PhaseEvent event) { 079 for (PhaseListener listener : getDelegates(event.getFacesContext())) { 080 listener.afterPhase(event); 081 } 082 } 083 084 085 /** 086 * Obtain the delegate PhaseListener beans from the Spring root WebApplicationContext. 087 * @param facesContext the current JSF context 088 * @return a Collection of PhaseListener objects 089 * @see #getBeanFactory 090 * @see org.springframework.beans.factory.ListableBeanFactory#getBeansOfType(Class) 091 */ 092 protected Collection<PhaseListener> getDelegates(FacesContext facesContext) { 093 ListableBeanFactory bf = getBeanFactory(facesContext); 094 return BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, PhaseListener.class, true, false).values(); 095 } 096 097 /** 098 * Retrieve the Spring BeanFactory to delegate bean name resolution to. 099 * <p>The default implementation delegates to {@code getWebApplicationContext}. 100 * Can be overridden to provide an arbitrary ListableBeanFactory reference to 101 * resolve against; usually, this will be a full Spring ApplicationContext. 102 * @param facesContext the current JSF context 103 * @return the Spring ListableBeanFactory (never {@code null}) 104 * @see #getWebApplicationContext 105 */ 106 protected ListableBeanFactory getBeanFactory(FacesContext facesContext) { 107 return getWebApplicationContext(facesContext); 108 } 109 110 /** 111 * Retrieve the web application context to delegate bean name resolution to. 112 * <p>The default implementation delegates to FacesContextUtils. 113 * @param facesContext the current JSF context 114 * @return the Spring web application context (never {@code null}) 115 * @see FacesContextUtils#getRequiredWebApplicationContext 116 */ 117 protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) { 118 return FacesContextUtils.getRequiredWebApplicationContext(facesContext); 119 } 120 121}