KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > handler > AbstractHandlerMapping


1 /*
2  * Copyright 2002-2007 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.portlet.handler;
18
19 import javax.portlet.PortletRequest;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.context.support.ApplicationObjectSupport;
23 import org.springframework.core.Ordered;
24 import org.springframework.web.context.request.WebRequestInterceptor;
25 import org.springframework.web.portlet.HandlerExecutionChain;
26 import org.springframework.web.portlet.HandlerInterceptor;
27 import org.springframework.web.portlet.HandlerMapping;
28
29 /**
30  * Abstract base class for {@link org.springframework.web.portlet.HandlerMapping}
31  * implementations. Supports ordering, a default handler, and handler interceptors.
32  *
33  * @author Juergen Hoeller
34  * @author John A. Lewis
35  * @since 2.0
36  * @see #getHandlerInternal
37  * @see org.springframework.web.portlet.HandlerInterceptor
38  */

39 public abstract class AbstractHandlerMapping extends ApplicationObjectSupport
40     implements HandlerMapping, Ordered {
41
42     private int order = Integer.MAX_VALUE; // default: same as non-Ordered
43

44     private Object JavaDoc defaultHandler;
45
46     private Object JavaDoc[] interceptors;
47
48     private boolean applyWebRequestInterceptorsToRenderPhaseOnly = true;
49
50     private HandlerInterceptor[] adaptedInterceptors;
51
52
53     public final void setOrder(int order) {
54       this.order = order;
55     }
56
57     public final int getOrder() {
58       return order;
59     }
60
61     /**
62      * Set the default handler for this handler mapping.
63      * This handler will be returned if no specific mapping was found.
64      * <p>Default is <code>null</code>, indicating no default handler.
65      */

66     public void setDefaultHandler(Object JavaDoc defaultHandler) {
67         this.defaultHandler = defaultHandler;
68     }
69
70     /**
71      * Return the default handler for this handler mapping,
72      * or <code>null</code> if none.
73      */

74     public Object JavaDoc getDefaultHandler() {
75         return this.defaultHandler;
76     }
77
78     /**
79      * Set the interceptors to apply for all handlers mapped by this handler mapping.
80      * <p>Supported interceptor types are HandlerInterceptor and WebRequestInterceptor.
81      * Each given WebRequestInterceptor will be wrapped in a WebRequestHandlerInterceptorAdapter.
82      * @param interceptors array of handler interceptors, or <code>null</code> if none
83      * @see #adaptInterceptor
84      * @see org.springframework.web.portlet.HandlerInterceptor
85      * @see org.springframework.web.context.request.WebRequestInterceptor
86      */

87     public final void setInterceptors(Object JavaDoc[] interceptors) {
88         this.interceptors = interceptors;
89     }
90
91     /**
92      * Specify whether to apply WebRequestInterceptors to the Portlet render phase
93      * only ("true", or whether to apply them to the Portlet action phase as well
94      * ("false").
95      * <p>Default is "true", since WebRequestInterceptors are usually built for
96      * MVC-style handler execution plus rendering process (which is, for example,
97      * the primary target scenario for "Open Session in View" interceptors,
98      * offering lazy loading of persistent objects during view rendering).
99      * Set this to "false" to have WebRequestInterceptors apply to the action
100      * phase as well (for example, in case of an "Open Session in View" interceptor,
101      * to allow for lazy loading outside of a transaction during the action phase).
102      * @see #setInterceptors
103      * @see org.springframework.web.context.request.WebRequestInterceptor
104      * @see WebRequestHandlerInterceptorAdapter#WebRequestHandlerInterceptorAdapter(WebRequestInterceptor, boolean)
105      * @see org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
106      */

107     public void setApplyWebRequestInterceptorsToRenderPhaseOnly(boolean applyWebRequestInterceptorsToRenderPhaseOnly) {
108         this.applyWebRequestInterceptorsToRenderPhaseOnly = applyWebRequestInterceptorsToRenderPhaseOnly;
109     }
110
111
112     /**
113      * Calls the <code>initInterceptors()</code> method.
114      * @see #initInterceptors()
115      */

116     protected void initApplicationContext() throws BeansException {
117         initInterceptors();
118     }
119
120     /**
121      * Initialize the specified interceptors, adapting them where necessary.
122      * @see #setInterceptors
123      * @see #adaptInterceptor
124      */

125     protected void initInterceptors() {
126         if (this.interceptors != null) {
127             this.adaptedInterceptors = new HandlerInterceptor[this.interceptors.length];
128             for (int i = 0; i < this.interceptors.length; i++) {
129                 if (this.interceptors[i] == null) {
130                     throw new IllegalArgumentException JavaDoc("Entry number " + i + " in interceptors array is null");
131                 }
132                 this.adaptedInterceptors[i] = adaptInterceptor(this.interceptors[i]);
133             }
134         }
135     }
136
137     /**
138      * Adapt the given interceptor object to the HandlerInterceptor interface.
139      * <p>Supported interceptor types are HandlerInterceptor and WebRequestInterceptor.
140      * Each given WebRequestInterceptor will be wrapped in a WebRequestHandlerInterceptorAdapter.
141      * Can be overridden in subclasses.
142      * @param interceptor the specified interceptor object
143      * @return the interceptor wrapped as HandlerInterceptor
144      * @see #setApplyWebRequestInterceptorsToRenderPhaseOnly
145      * @see org.springframework.web.portlet.HandlerInterceptor
146      * @see org.springframework.web.context.request.WebRequestInterceptor
147      * @see WebRequestHandlerInterceptorAdapter
148      */

149     protected HandlerInterceptor adaptInterceptor(Object JavaDoc interceptor) {
150         if (interceptor instanceof HandlerInterceptor) {
151             return (HandlerInterceptor) interceptor;
152         }
153         else if (interceptor instanceof WebRequestInterceptor) {
154             return new WebRequestHandlerInterceptorAdapter(
155                     (WebRequestInterceptor) interceptor, this.applyWebRequestInterceptorsToRenderPhaseOnly);
156         }
157         else {
158             throw new IllegalArgumentException JavaDoc("Interceptor type not supported: " + interceptor.getClass().getName());
159         }
160     }
161
162     /**
163      * Return the adapted interceptors as HandlerInterceptor array.
164      * @return the array of HandlerInterceptors, or <code>null</code> if none
165      */

166     protected final HandlerInterceptor[] getAdaptedInterceptors() {
167         return this.adaptedInterceptors;
168     }
169
170
171     /**
172      * Look up a handler for the given request, falling back to the default
173      * handler if no specific one is found.
174      * @param request current portlet request
175      * @return the corresponding handler instance, or the default handler
176      * @see #getHandlerInternal
177      */

178     public final HandlerExecutionChain getHandler(PortletRequest request) throws Exception JavaDoc {
179         Object JavaDoc handler = getHandlerInternal(request);
180         if (handler == null) {
181             handler = getDefaultHandler();
182         }
183         if (handler == null) {
184             return null;
185         }
186         // Bean name or resolved handler?
187
if (handler instanceof String JavaDoc) {
188             String JavaDoc handlerName = (String JavaDoc) handler;
189             handler = getApplicationContext().getBean(handlerName);
190         }
191         return getHandlerExecutionChain(handler, request);
192     }
193
194     /**
195      * Look up a handler for the given request, returning <code>null</code> if no
196      * specific one is found. This method is called by <code>getHandler<code>;
197      * a <code>null</code> return value will lead to the default handler, if one is set.
198      * @param request current portlet request
199      * @return the corresponding handler instance, or <code>null</code> if none found
200      * @throws Exception if there is an internal error
201      * @see #getHandler
202      */

203     protected abstract Object JavaDoc getHandlerInternal(PortletRequest request) throws Exception JavaDoc;
204
205     /**
206      * Build a HandlerExecutionChain for the given handler, including applicable interceptors.
207      * <p>The default implementation simply builds a standard HandlerExecutionChain with
208      * the given handler and this handler mapping's common interceptors. Subclasses may
209      * override this in order to extend/rearrange the list of interceptors.
210      * @param handler the resolved handler instance (never <code>null</code>)
211      * @param request current portlet request
212      * @return the HandlerExecutionChain (never <code>null</code>)
213      * @see #getAdaptedInterceptors()
214      */

215     protected HandlerExecutionChain getHandlerExecutionChain(Object JavaDoc handler, PortletRequest request) {
216         return new HandlerExecutionChain(handler, getAdaptedInterceptors());
217     }
218
219 }
220
Popular Tags