KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > jsf > DelegatingPhaseListenerMulticaster


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.jsf;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import javax.faces.context.FacesContext;
23 import javax.faces.event.PhaseEvent;
24 import javax.faces.event.PhaseId;
25 import javax.faces.event.PhaseListener;
26
27 import org.springframework.beans.factory.BeanFactoryUtils;
28 import org.springframework.beans.factory.ListableBeanFactory;
29 import org.springframework.web.context.WebApplicationContext;
30
31 /**
32  * JSF PhaseListener implementation that delegates to one or more Spring-managed
33  * PhaseListener beans coming from the Spring root WebApplicationContext.
34  *
35  * <p>Configure this listener multicaster in your <code>faces-config.xml</code> file
36  * as follows:
37  *
38  * <pre>
39  * &lt;application&gt;
40  * ...
41  * &lt;phase-listener&gt;
42  * org.springframework.web.jsf.DelegatingPhaseListenerMulticaster
43  * &lt;/phase-listener&gt;
44  * ...
45  * &lt;/application&gt;</pre>
46  *
47  * The multicaster will delegate all <code>beforePhase</code> and <code>afterPhase</code>
48  * events to all target PhaseListener beans. By default, those will simply be obtained
49  * by type: All beans in the Spring root WebApplicationContext that implement the
50  * PhaseListener interface will be fetched and invoked.
51  *
52  * <p>Note this multicaster's <code>getPhaseId()</code> method will always return
53  * <code>ANY_PHASE</code>. The phase id exposed by the target listener beans
54  * will be ignored; all events will be propagated to all listeners.
55  *
56  * <p>This multicaster may be subclassed to change the strategy used to obtain
57  * the listener beans, or to change the strategy used to access the ApplicationContext
58  * (normally obtained via {@link FacesContextUtils#getWebApplicationContext(FacesContext)}).
59  *
60  * @author Juergen Hoeller
61  * @author Colin Sampaleanu
62  * @since 1.2.7
63  */

64 public class DelegatingPhaseListenerMulticaster implements PhaseListener {
65
66     public PhaseId getPhaseId() {
67         return PhaseId.ANY_PHASE;
68     }
69
70     public void beforePhase(PhaseEvent event) {
71         Collection JavaDoc listeners = getDelegates(event.getFacesContext());
72         Iterator JavaDoc it = listeners.iterator();
73         while (it.hasNext()) {
74             PhaseListener listener = (PhaseListener) it.next();
75             listener.beforePhase(event);
76         }
77     }
78
79     public void afterPhase(PhaseEvent event) {
80         Collection JavaDoc listeners = getDelegates(event.getFacesContext());
81         Iterator JavaDoc it = listeners.iterator();
82         while (it.hasNext()) {
83             PhaseListener listener = (PhaseListener) it.next();
84             listener.afterPhase(event);
85         }
86     }
87
88
89     /**
90      * Obtain the delegate PhaseListener beans from the Spring root
91      * WebApplicationContext.
92      * @param facesContext the current JSF context
93      * @return a Collection of PhaseListener objects
94      * @see #getBeanFactory
95      * @see org.springframework.beans.factory.ListableBeanFactory#getBeansOfType(Class)
96      */

97     protected Collection JavaDoc getDelegates(FacesContext facesContext) {
98         ListableBeanFactory bf = getBeanFactory(facesContext);
99         return BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, PhaseListener.class, true, false).values();
100     }
101
102     /**
103      * Retrieve the Spring BeanFactory to delegate bean name resolution to.
104      * <p>Default implementation delegates to <code>getWebApplicationContext</code>.
105      * Can be overridden to provide an arbitrary ListableBeanFactory reference to
106      * resolve against; usually, this will be a full Spring ApplicationContext.
107      * @param facesContext the current JSF context
108      * @return the Spring ListableBeanFactory (never <code>null</code>)
109      * @see #getWebApplicationContext
110      */

111     protected ListableBeanFactory getBeanFactory(FacesContext facesContext) {
112         return getWebApplicationContext(facesContext);
113     }
114
115     /**
116      * Retrieve the web application context to delegate bean name resolution to.
117      * <p>Default implementation delegates to FacesContextUtils.
118      * @param facesContext the current JSF context
119      * @return the Spring web application context (never <code>null</code>)
120      * @see FacesContextUtils#getRequiredWebApplicationContext
121      */

122     protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
123         return FacesContextUtils.getRequiredWebApplicationContext(facesContext);
124     }
125
126 }
127
Popular Tags