KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.faces.application.NavigationHandler;
20 import javax.faces.context.FacesContext;
21
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.web.context.WebApplicationContext;
24
25 /**
26  * JSF NavigationHandler implementation that delegates to a NavigationHandler
27  * bean obtained from the Spring root WebApplicationContext.
28  *
29  * <p>Configure this handler proxy in your <code>faces-config.xml</code> file
30  * as follows:
31  *
32  * <pre>
33  * &lt;application&gt;
34  * ...
35  * &lt;navigation-handler&gt;
36  * org.springframework.web.jsf.DelegatingNavigationHandlerProxy
37  * &lt;/navigation-handler&gt;
38  * ...
39  * &lt;/application&gt;</pre>
40  *
41  * By default, the Spring ApplicationContext will be searched for the NavigationHandler
42  * under the bean name "jsfNavigationHandler". In the simplest case, this is a plain
43  * Spring bean definition like the following. However, all of Spring's bean configuration
44  * power can be applied to such a bean, in particular all flavors of dependency injection.
45  *
46  * <pre>
47  * &lt;bean name="jsfNavigationHandler" class="mypackage.MyNavigationHandler"&gt;
48  * &lt;property name="myProperty" ref="myOtherBean"/&gt;
49  * &lt;/bean&gt;</pre>
50  *
51  * The target NavigationHandler bean will typically extend the standard JSF
52  * NavigationHandler class. However, note that decorating the original
53  * NavigationHandler (the JSF provider's default handler) is <i>not</i> supported
54  * in such a scenario, since we can't inject the original handler in standard
55  * JSF style (that is, as constructor argument).
56  *
57  * <p>For <b>decorating the original NavigationHandler</b>, make sure that your
58  * target bean extends Spring's <b>DecoratingNavigationHandler</b> class. This
59  * allows to pass in the original handler as method argument, which this proxy
60  * automatically detects. Note that a DecoratingNavigationHandler subclass
61  * will still work as standard JSF NavigationHandler as well!
62  *
63  * <p>This proxy may be subclassed to change the bean name used to search for the
64  * navigation handler, change the strategy used to obtain the target handler,
65  * or change the strategy used to access the ApplicationContext (normally obtained
66  * via {@link FacesContextUtils#getWebApplicationContext(FacesContext)}).
67  *
68  * @author Juergen Hoeller
69  * @author Colin Sampaleanu
70  * @since 1.2.7
71  * @see DecoratingNavigationHandler
72  */

73 public class DelegatingNavigationHandlerProxy extends NavigationHandler {
74
75     /**
76      * Default name of the target bean in the Spring application context:
77      * "jsfNavigationHandler"
78      */

79     public final static String JavaDoc DEFAULT_TARGET_BEAN_NAME = "jsfNavigationHandler";
80
81     private NavigationHandler originalNavigationHandler;
82
83
84     /**
85      * Create a new DelegatingNavigationHandlerProxy.
86      */

87     public DelegatingNavigationHandlerProxy() {
88     }
89
90     /**
91      * Create a new DelegatingNavigationHandlerProxy.
92      * @param originalNavigationHandler the original NavigationHandler
93      */

94     public DelegatingNavigationHandlerProxy(NavigationHandler originalNavigationHandler) {
95         this.originalNavigationHandler = originalNavigationHandler;
96     }
97
98
99     /**
100      * Handle the navigation request implied by the specified parameters,
101      * through delegating to the target bean in the Spring application context.
102      * <p>The target bean needs to extend the JSF NavigationHandler class.
103      * If it extends Spring's DecoratingNavigationHandler, the overloaded
104      * <code>handleNavigation</code> method with the original NavigationHandler
105      * as argument will be used. Else, the standard <code>handleNavigation</code>
106      * method will be called.
107      */

108     public void handleNavigation(FacesContext facesContext, String JavaDoc fromAction, String JavaDoc outcome) {
109         NavigationHandler handler = getDelegate(facesContext);
110         if (handler instanceof DecoratingNavigationHandler) {
111             ((DecoratingNavigationHandler) handler).handleNavigation(
112                     facesContext, fromAction, outcome, this.originalNavigationHandler);
113         }
114         else {
115             handler.handleNavigation(facesContext, fromAction, outcome);
116         }
117     }
118
119     /**
120      * Return the target NavigationHandler to delegate to.
121      * <p>By default, a bean with the name "jsfNavigationHandler" is obtained
122      * from the Spring root WebApplicationContext, for every invocation.
123      * @param facesContext the current JSF context
124      * @return the target NavigationHandler to delegate to
125      * @see #getTargetBeanName
126      * @see #getBeanFactory
127      */

128     protected NavigationHandler getDelegate(FacesContext facesContext) {
129         String JavaDoc targetBeanName = getTargetBeanName(facesContext);
130         return (NavigationHandler) getBeanFactory(facesContext).getBean(targetBeanName, NavigationHandler.class);
131     }
132
133     /**
134      * Return the name of the target NavigationHandler bean in the BeanFactory.
135      * Default is "jsfNavigationHandler".
136      * @param facesContext the current JSF context
137      * @return the name of the target bean
138      */

139     protected String JavaDoc getTargetBeanName(FacesContext facesContext) {
140         return DEFAULT_TARGET_BEAN_NAME;
141     }
142
143     /**
144      * Retrieve the Spring BeanFactory to delegate bean name resolution to.
145      * <p>Default implementation delegates to <code>getWebApplicationContext</code>.
146      * Can be overridden to provide an arbitrary BeanFactory reference to resolve
147      * against; usually, this will be a full Spring ApplicationContext.
148      * @param facesContext the current JSF context
149      * @return the Spring BeanFactory (never <code>null</code>)
150      * @see #getWebApplicationContext
151      */

152     protected BeanFactory getBeanFactory(FacesContext facesContext) {
153         return getWebApplicationContext(facesContext);
154     }
155
156     /**
157      * Retrieve the web application context to delegate bean name resolution to.
158      * <p>Default implementation delegates to FacesContextUtils.
159      * @param facesContext the current JSF context
160      * @return the Spring web application context (never <code>null</code>)
161      * @see FacesContextUtils#getRequiredWebApplicationContext
162      */

163     protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
164         return FacesContextUtils.getRequiredWebApplicationContext(facesContext);
165     }
166
167 }
168
Popular Tags