/*
 * Copyright 2002-2006 the original author or authors.
 *
 * 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.springframework.web.jsf;

import java.util.Collection;
import java.util.Iterator;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.web.context.WebApplicationContext;

/**
 * JSF PhaseListener implementation that delegates to one or more Spring-managed
 * PhaseListener beans coming from the Spring root WebApplicationContext.
 *
 * <p>Configure this listener adapter in your <code>faces-config.xml</code> file
 * as follows:
 *
 * <pre>
 * &lt;application&gt;
 *   ...
 *   &lt;phase-listener&gt;
 *     org.springframework.web.jsf.phase.DelegatingPhaseListenerAdapter
 *   &lt;/phase-listener&gt;
 *   ...
 * &lt;/application&gt;</pre>
 *
 * The adapter will delegate all <code>beforePhase</code> and <code>afterPhase</code>
 * events to all target PhaseListener beans. By default, those will simply be obtained
 * by type: All beans in the Spring root WebApplicationContext that implement the
 * PhaseListener interface will be fetched and invoked.
 *
 * <p>Note this adapter's <code>getPhaseId()</code> method will always return
 * <code>ANY_PHASE</code>. The phase id exposed by the target listener beans
 * will be ignored; all events will be propagated to all listeners.
 *
 * <p>This adapter may be subclassed to change the strategy used to obtain
 * the listener beans, or to change the strategy used to access the ApplicationContext
 * (normally obtained via {@link FacesContextUtils#getWebApplicationContext(FacesContext)}).
 *
 * @author Juergen Hoeller
 * @author Colin Sampaleanu
 * @since 1.2.7
 */
public class DelegatingPhaseListenerAdapter implements PhaseListener {

	public PhaseId getPhaseId() {
		return PhaseId.ANY_PHASE;
	}

	public void beforePhase(PhaseEvent event) {
		Collection listeners = getDelegates(event.getFacesContext());
		Iterator it = listeners.iterator();
		while (it.hasNext()) {
			PhaseListener listener = (PhaseListener) it.next();
			listener.beforePhase(event);
		}
	}

	public void afterPhase(PhaseEvent event) {
		Collection listeners = getDelegates(event.getFacesContext());
		Iterator it = listeners.iterator();
		while (it.hasNext()) {
			PhaseListener listener = (PhaseListener) it.next();
			listener.afterPhase(event);
		}
	}


	/**
	 * Obtain the delegate PhaseListener beans from the Spring root
	 * WebApplicationContext.
	 * @param facesContext the current JSF context
	 * @return a Collection of PhaseListener objects
	 * @see #getBeanFactory
	 * @see org.springframework.beans.factory.ListableBeanFactory#getBeansOfType(Class)
	 */
	protected Collection getDelegates(FacesContext facesContext) {
		ListableBeanFactory bf = getBeanFactory(facesContext);
		return bf.getBeansOfType(PhaseListener.class).values();
	}

	/**
	 * Retrieve the Spring BeanFactory to delegate bean name resolution to.
	 * <p>Default implementation delegates to <code>getWebApplicationContext</code>.
	 * Can be overridden to provide an arbitrary ListableBeanFactory reference to
	 * resolve against; usually, this will be a full Spring ApplicationContext.
	 * @param facesContext the current JSF context
	 * @return the Spring ListableBeanFactory (never <code>null</code>)
	 * @see #getWebApplicationContext
	 */
	protected ListableBeanFactory getBeanFactory(FacesContext facesContext) {
		return getWebApplicationContext(facesContext);
	}

	/**
	 * Retrieve the web application context to delegate bean name resolution to.
	 * <p>Default implementation delegates to FacesContextUtils.
	 * @param facesContext the current JSF context
	 * @return the Spring web application context (never <code>null</code>)
	 * @see FacesContextUtils#getRequiredWebApplicationContext
	 */
	protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
		return FacesContextUtils.getRequiredWebApplicationContext(facesContext);
	}

}

@@ -859,24 +859,83 @@
 eListener;%0A%0A
+import org.springframework.beans.factory.BeanFactoryUtils;%0A
 import org.s
@@ -1416,14 +1416,8 @@
 jsf.
-phase.
 Dele
@@ -3499,26 +3499,59 @@
 urn 
-bf.getBeansOfType(
+BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, 
 Phas

use beansOfTypeIncludingAncestors for delegate retrieval