View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.plugins.spring;
18  
19  import javax.servlet.ServletContext;
20  
21  import org.seasar.cubby.plugin.AbstractPlugin;
22  import org.seasar.cubby.spi.BeanDescProvider;
23  import org.seasar.cubby.spi.ContainerProvider;
24  import org.seasar.cubby.spi.ConverterProvider;
25  import org.seasar.cubby.spi.PathResolverProvider;
26  import org.seasar.cubby.spi.Provider;
27  import org.seasar.cubby.spi.RequestParserProvider;
28  import org.springframework.beans.factory.BeanFactoryUtils;
29  import org.springframework.context.ApplicationContext;
30  import org.springframework.web.context.ContextLoader;
31  import org.springframework.web.context.ContextLoaderListener;
32  import org.springframework.web.context.request.RequestContextListener;
33  import org.springframework.web.context.support.WebApplicationContextUtils;
34  
35  /**
36   * Cubby を <a href="http://www.springsource.org/">Spring Framework</a>
37   * に統合するためのプラグインです。
38   * 
39   * <p>
40   * アプリケーションが使用する XML ファイルを WEB 配備記述子の初期化パラメータ
41   * {@value ContextLoader#CONFIG_LOCATION_PARAM} に指定してください。 また、
42   * {@link ContextLoaderListener} と {@link RequestContextListener} をリスナに設定してください。
43   * </p>
44   * 
45   * <p>
46   * 例
47   * 
48   * <pre>
49   * &lt;context-param&gt;
50   *   &lt;param-name&gt;{@value ContextLoader#CONFIG_LOCATION_PARAM}&lt;/paanm-name&gt;
51   *   &lt;param-value&gt;/WEB-INF/classes/applicationContext.xml&lt;/param-value&gt;
52   * &lt;/context-param&gt;
53   * 
54   * &lt;listener&gt;
55   *   &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;listener-class&gt;
56   * &lt;/listener&gt;
57   * 
58   * &lt;listener&gt;
59   *   &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;listener-class&gt;
60   * &lt;/listener&gt;
61   * </pre>
62   * 
63   * </p>
64   * 
65   * <p>
66   * このプラグインが提供するプロバイダは以下の通りです。
67   * <ul>
68   * <li>{@link BeanDescProvider}</li>
69   * <li>{@link ContainerProvider}</li>
70   * <li>{@link RequestParserProvider}</li>
71   * <li>{@link PathResolverProvider}</li>
72   * <li>{@link ConverterProvider}</li>
73   * </ul>
74   * </p>
75   * 
76   * @author suzuki-kei
77   * @author someda
78   * 
79   * @see <a
80   *      href="http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#context-create">Convenient
81   *      ApplicationContext instantiation for web applications</a>
82   * 
83   * @since 2.0.0
84   */
85  public class SpringPlugin extends AbstractPlugin {
86  
87  	private ApplicationContext applicationContext;
88  
89  	/**
90  	 * インスタンス化します。
91  	 */
92  	public SpringPlugin() {
93  		support(BeanDescProvider.class);
94  		support(ContainerProvider.class);
95  		support(RequestParserProvider.class);
96  		support(PathResolverProvider.class);
97  		support(ConverterProvider.class);
98  	}
99  
100 	/**
101 	 * {@inheritDoc}
102 	 */
103 	@Override
104 	public void initialize(final ServletContext servletContext)
105 			throws Exception {
106 		super.initialize(servletContext);
107 		this.applicationContext = WebApplicationContextUtils
108 				.getRequiredWebApplicationContext(servletContext);
109 	}
110 
111 	/**
112 	 * {@inheritDoc}
113 	 */
114 	@Override
115 	public <S extends Provider> S getProvider(final Class<S> service) {
116 
117 		if (this.isSupport(service)) {
118 			final String[] names = BeanFactoryUtils
119 					.beanNamesForTypeIncludingAncestors(applicationContext,
120 							service);
121 			return service.cast(applicationContext.getBean(names[0]));
122 		} else {
123 			return null;
124 		}
125 	}
126 
127 }