KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
23  * Base class for JSF NavigationHandler implementations that want
24  * to be capable of decorating an original NavigationHandler.
25  *
26  * <p>Supports the standard JSF style of decoration (through a constructor argument)
27  * as well as an overloaded <code>handleNavigation</code> method with explicit
28  * NavigationHandler argument (passing in the original NavigationHandler). Subclasses
29  * are forced to implement this overloaded <code>handleNavigation</code> method.
30  * Standard JSF invocations will automatically delegate to the overloaded method,
31  * with the constructor-injected NavigationHandler as argument.
32  *
33  * @author Juergen Hoeller
34  * @since 1.2.7
35  * @see DelegatingNavigationHandlerProxy
36  */

37 public abstract class DecoratingNavigationHandler extends NavigationHandler {
38
39     private NavigationHandler decoratedNavigationHandler;
40
41
42     /**
43      * Create a DecoratingNavigationHandler without fixed original NavigationHandler.
44      */

45     protected DecoratingNavigationHandler() {
46     }
47
48     /**
49      * Create a DecoratingNavigationHandler with fixed original NavigationHandler.
50      * @param originalNavigationHandler the original NavigationHandler to decorate
51      */

52     protected DecoratingNavigationHandler(NavigationHandler originalNavigationHandler) {
53         this.decoratedNavigationHandler = originalNavigationHandler;
54     }
55
56     /**
57      * Return the fixed original NavigationHandler decorated by this handler, if any
58      * (that is, if passed in through the constructor).
59      */

60     public final NavigationHandler getDecoratedNavigationHandler() {
61         return decoratedNavigationHandler;
62     }
63
64
65     /**
66      * This implementation of the standard JSF <code>handleNavigation</code> method
67      * delegates to the overloaded variant, passing in constructor-injected
68      * NavigationHandler as argument.
69      * @see #handleNavigation(javax.faces.context.FacesContext, String, String, javax.faces.application.NavigationHandler)
70      */

71     public final void handleNavigation(FacesContext facesContext, String JavaDoc fromAction, String JavaDoc outcome) {
72         handleNavigation(facesContext, fromAction, outcome, this.decoratedNavigationHandler);
73     }
74
75     /**
76      * Special <code>handleNavigation</code> variant with explicit NavigationHandler
77      * argument. Either called directly, by code with an explicit original handler,
78      * or called from the standard <code>handleNavigation</code> method, as
79      * plain JSF-defined NavigationHandler.
80      * <p>Implementations should invoke <code>callNextHandlerInChain</code> to
81      * delegate to the next handler in the chain. This will always call the most
82      * appropriate next handler (see <code>callNextHandlerInChain</code> javadoc).
83      * Alternatively, the decorated NavigationHandler or the passed-in original
84      * NavigationHandler can also be called directly; however, this is not as
85      * flexible in terms of reacting to potential positions in the chain.
86      * @param facesContext the current JSF context
87      * @param fromAction the action binding expression that was evaluated to retrieve the
88      * specified outcome, or <code>null</code> if the outcome was acquired by some other means
89      * @param outcome the logical outcome returned by a previous invoked application action
90      * (which may be <code>null</code>)
91      * @param originalNavigationHandler the original NavigationHandler,
92      * or <code>null</code> if none
93      * @see #callNextHandlerInChain
94      */

95     public abstract void handleNavigation(
96             FacesContext facesContext, String JavaDoc fromAction, String JavaDoc outcome, NavigationHandler originalNavigationHandler);
97
98
99     /**
100      * Method to be called by subclasses when intending to delegate to the next
101      * handler in the NavigationHandler chain. Will always call the most
102      * appropriate next handler, either the decorated NavigationHandler passed
103      * in as constructor argument or the original NavigationHandler as passed
104      * into this method - according to the position of this instance in the chain.
105      * <p>Will call the decorated NavigationHandler specified as constructor
106      * argument, if any. In case of a DecoratingNavigationHandler as target, the
107      * original NavigationHandler as passed into this method will be passed on to
108      * the next element in the chain: This ensures propagation of the original
109      * handler that the last element in the handler chain might delegate back to.
110      * In case of a standard NavigationHandler as target, the original handler
111      * will simply not get passed on; no delegating back to the original is
112      * possible further down the chain in that scenario.
113      * <p>If no decorated NavigationHandler specified as constructor argument,
114      * this instance is the last element in the chain. Hence, this method will
115      * call the original NavigationHandler as passed into this method. If no
116      * original NavigantionHandler has been passed in (for example if this
117      * instance is the last element in a chain with standard NavigationHandlers
118      * as earlier elements), this method corresponds to a no-op.
119      * @param facesContext the current JSF context
120      * @param fromAction the action binding expression that was evaluated to retrieve the
121      * specified outcome, or <code>null</code> if the outcome was acquired by some other means
122      * @param outcome the logical outcome returned by a previous invoked application action
123      * (which may be <code>null</code>)
124      * @param originalNavigationHandler the original NavigationHandler,
125      * or <code>null</code> if none
126      */

127     protected final void callNextHandlerInChain(
128             FacesContext facesContext, String JavaDoc fromAction, String JavaDoc outcome, NavigationHandler originalNavigationHandler) {
129
130         NavigationHandler decoratedNavigationHandler = getDecoratedNavigationHandler();
131
132         if (decoratedNavigationHandler instanceof DecoratingNavigationHandler) {
133             // DecoratingNavigationHandler specified through constructor argument:
134
// Call it with original NavigationHandler passed in.
135
DecoratingNavigationHandler decHandler = (DecoratingNavigationHandler) decoratedNavigationHandler;
136             decHandler.handleNavigation(facesContext, fromAction, outcome, originalNavigationHandler);
137         }
138         else if (decoratedNavigationHandler != null) {
139             // Standard NavigationHandler specified through constructor argument:
140
// Call it through standard API, without original NavigationHandler passed in.
141
// The called handler will not be able to redirect to the original handler.
142
decoratedNavigationHandler.handleNavigation(facesContext, fromAction, outcome);
143         }
144         else if (originalNavigationHandler != null) {
145             // No NavigationHandler specified through constructor argument:
146
// Call original handler, marking the end of this chain.
147
originalNavigationHandler.handleNavigation(facesContext, fromAction, outcome);
148         }
149     }
150
151 }
152
Popular Tags