View Javadoc

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