| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SpringPlugin |
|
| 1.6666666666666667;1.667 |
| 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 | * <context-param> | |
| 50 | * <param-name>{@value ContextLoader#CONFIG_LOCATION_PARAM}</paanm-name> | |
| 51 | * <param-value>/WEB-INF/classes/applicationContext.xml</param-value> | |
| 52 | * </context-param> | |
| 53 | * | |
| 54 | * <listener> | |
| 55 | * <listener-class>org.springframework.web.context.ContextLoaderListener<listener-class> | |
| 56 | * </listener> | |
| 57 | * | |
| 58 | * <listener> | |
| 59 | * <listener-class>org.springframework.web.context.request.RequestContextListener<listener-class> | |
| 60 | * </listener> | |
| 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 | 2 | public SpringPlugin() { |
| 93 | 2 | support(BeanDescProvider.class); |
| 94 | 2 | support(ContainerProvider.class); |
| 95 | 2 | support(RequestParserProvider.class); |
| 96 | 2 | support(PathResolverProvider.class); |
| 97 | 2 | support(ConverterProvider.class); |
| 98 | 2 | } |
| 99 | ||
| 100 | /** | |
| 101 | * {@inheritDoc} | |
| 102 | */ | |
| 103 | @Override | |
| 104 | public void initialize(final ServletContext servletContext) | |
| 105 | throws Exception { | |
| 106 | 2 | super.initialize(servletContext); |
| 107 | 2 | this.applicationContext = WebApplicationContextUtils |
| 108 | .getRequiredWebApplicationContext(servletContext); | |
| 109 | 2 | } |
| 110 | ||
| 111 | /** | |
| 112 | * {@inheritDoc} | |
| 113 | */ | |
| 114 | @Override | |
| 115 | public <S extends Provider> S getProvider(final Class<S> service) { | |
| 116 | ||
| 117 | 2 | if (this.isSupport(service)) { |
| 118 | 1 | final String[] names = BeanFactoryUtils |
| 119 | .beanNamesForTypeIncludingAncestors(applicationContext, | |
| 120 | service); | |
| 121 | 1 | return service.cast(applicationContext.getBean(names[0])); |
| 122 | } else { | |
| 123 | 1 | return null; |
| 124 | } | |
| 125 | } | |
| 126 | ||
| 127 | } |