View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.controller.impl;
17  
18  import java.util.Arrays;
19  import java.util.Collections;
20  import java.util.Comparator;
21  import java.util.List;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.seasar.cubby.controller.RequestParser;
26  import org.seasar.cubby.controller.RequestParserSelector;
27  import org.seasar.framework.container.S2Container;
28  
29  /**
30   * リクエスト解析器セレクタの実装です。
31   * 
32   * @author baba
33   * @since 1.1.0
34   */
35  public class RequestParserSelectorImpl implements RequestParserSelector {
36  
37  	/** コンテナ。 */
38  	private S2Container container;
39  
40  	/** リクエスト解析器の {@link Comparator}。 */
41  	private Comparator<RequestParser> requestParserComparator = new Comparator<RequestParser>() {
42  
43  		/**
44  		 * {@inheritDoc}
45  		 * <p>
46  		 * 優先順位の昇順にソートします。
47  		 * </p>
48  		 */
49  		public int compare(RequestParser reuqestParser1,
50  				RequestParser reuqestParser2) {
51  			return reuqestParser1.getPriority() - reuqestParser2.getPriority();
52  		}
53  
54  	};
55  
56  	/**
57  	 * コンテナを設定します。
58  	 * 
59  	 * @param container
60  	 *            コンテナ
61  	 */
62  	public void setContainer(final S2Container container) {
63  		this.container = container;
64  	}
65  
66  	/**
67  	 * {@inheritDoc}
68  	 */
69  	public RequestParser select(final HttpServletRequest request) {
70  		final S2Container root = container.getRoot();
71  		final RequestParser[] requestParsers = (RequestParser[]) root
72  				.findAllComponents(RequestParser.class);
73  		final List<RequestParser> requestParserList = Arrays
74  				.asList(requestParsers);
75  		Collections.sort(requestParserList, requestParserComparator);
76  		for (final RequestParser requestParser : requestParserList) {
77  			if (requestParser.isParsable(request)) {
78  				return requestParser;
79  			}
80  		}
81  		return null;
82  	}
83  
84  }