View Javadoc
1   /*
2    * Copyright 2002-2014 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;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import org.springframework.util.CollectionUtils;
24  import org.springframework.util.ObjectUtils;
25  
26  /**
27   * Handler execution chain, consisting of handler object and any handler interceptors.
28   * Returned by HandlerMapping's {@link HandlerMapping#getHandler} method.
29   *
30   * @author Juergen Hoeller
31   * @author John A. Lewis
32   * @since 2.0
33   * @see HandlerInterceptor
34   */
35  public class HandlerExecutionChain {
36  
37  	private final Object handler;
38  
39  	private HandlerInterceptor[] interceptors;
40  
41  	private List<HandlerInterceptor> interceptorList;
42  
43  
44  	/**
45  	 * Create a new HandlerExecutionChain.
46  	 * @param handler the handler object to execute
47  	 */
48  	public HandlerExecutionChain(Object handler) {
49  		this(handler, (HandlerInterceptor[]) null);
50  	}
51  
52  	/**
53  	 * Create a new HandlerExecutionChain.
54  	 * @param handler the handler object to execute
55  	 * @param interceptors the array of interceptors to apply
56  	 * (in the given order) before the handler itself executes
57  	 */
58  	public HandlerExecutionChain(Object handler, HandlerInterceptor... interceptors) {
59  		if (handler instanceof HandlerExecutionChain) {
60  			HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;
61  			this.handler = originalChain.getHandler();
62  			this.interceptorList = new ArrayList<HandlerInterceptor>();
63  			CollectionUtils.mergeArrayIntoCollection(originalChain.getInterceptors(), this.interceptorList);
64  			CollectionUtils.mergeArrayIntoCollection(interceptors, this.interceptorList);
65  		}
66  		else {
67  			this.handler = handler;
68  			this.interceptors = interceptors;
69  		}
70  	}
71  
72  
73  	/**
74  	 * Return the handler object to execute.
75  	 * @return the handler object
76  	 */
77  	public Object getHandler() {
78  		return this.handler;
79  	}
80  
81  	public void addInterceptor(HandlerInterceptor interceptor) {
82  		initInterceptorList().add(interceptor);
83  	}
84  
85  	public void addInterceptors(HandlerInterceptor... interceptors) {
86  		if (!ObjectUtils.isEmpty(interceptors)) {
87  			initInterceptorList().addAll(Arrays.asList(interceptors));
88  		}
89  	}
90  
91  	private List<HandlerInterceptor> initInterceptorList() {
92  		if (this.interceptorList == null) {
93  			this.interceptorList = new ArrayList<HandlerInterceptor>();
94  			if (this.interceptors != null) {
95  				// An interceptor array specified through the constructor
96  				this.interceptorList.addAll(Arrays.asList(this.interceptors));
97  			}
98  		}
99  		this.interceptors = null;
100 		return this.interceptorList;
101 	}
102 
103 	/**
104 	 * Return the array of interceptors to apply (in the given order).
105 	 * @return the array of HandlerInterceptors instances (may be {@code null})
106 	 */
107 	public HandlerInterceptor[] getInterceptors() {
108 		if (this.interceptors == null && this.interceptorList != null) {
109 			this.interceptors = this.interceptorList.toArray(new HandlerInterceptor[this.interceptorList.size()]);
110 		}
111 		return this.interceptors;
112 	}
113 
114 
115 	/**
116 	 * Delegates to the handler's {@code toString()}.
117 	 */
118 	@Override
119 	public String toString() {
120 		return String.valueOf(this.handler);
121 	}
122 
123 }