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.s2;
    17
    18  import javax.el.ELResolver;
    19  import javax.servlet.ServletContext;
    20  import javax.servlet.jsp.JspApplicationContext;
    21  import javax.servlet.jsp.JspFactory;
    22
    23  import org.seasar.cubby.plugin.AbstractPlugin;
    24  import org.seasar.cubby.plugins.s2.el.S2BeanELResolver;
    25  import org.seasar.cubby.spi.BeanDescProvider;
    26  import org.seasar.cubby.spi.ContainerProvider;
    27  import org.seasar.cubby.spi.ConverterProvider;
    28  import org.seasar.cubby.spi.PathResolverProvider;
    29  import org.seasar.cubby.spi.Provider;
    30  import org.seasar.cubby.spi.RequestParserProvider;
    31  import org.seasar.framework.container.S2Container;
    32  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
    33  import org.seasar.framework.log.Logger;
    34
    35  /**
    36   * Cubby を <a href="http://s2container.seasar.org/2.4/ja/">S2Container</a>
    37   * に統合するためのプラグインです。
    38   * <p>
    39   * このプラグインが提供するプロバイダは以下の通りです。
    40   * <ul>
    41   * <li>{@link BeanDescProvider}</li>
    42   * <li>{@link ContainerProvider}</li>
    43   * <li>{@link RequestParserProvider}</li>
    44   * <li>{@link PathResolverProvider}</li>
    45   * <li>{@link ConverterProvider}</li>
    46   * </ul>
    47   * </p>
    48   *
    49   * @see <a href="http://s2container.seasar.org/2.4/ja/">S2Container</a>
    50   * @author baba
    51   */
    52  public class S2ContainerPlugin extends AbstractPlugin {
    53
    54  	/** ロガー。 */
    55  	private static final Logger logger = Logger
    56  			.getLogger(S2ContainerPlugin.class);
    57
    58  	static {
    59  		// HACK
    60  		// In "mvn tomcat:run", JspFactory.getDefaultFactory() returns null.
    61  		// because static initializer of "JspRuntimeContext" has not been
    62  		// executed yet.
    63  		// Like "JasperListener" in "server.xml"
    64  		try {
    65  			Class.forName("org.apache.jasper.compiler.JspRuntimeContext");
    66  		} catch (final Exception e) {
    67  			if (logger.isDebugEnabled()) {
    68  				logger.debug(e.getMessage());
    69  			}
    70  		}
    71  	}
    72
    73  	/**
    74  	 * インスタンス化します。
    75  	 */
    76  	public S2ContainerPlugin() {
    77  		support(BeanDescProvider.class);
    78  		support(ContainerProvider.class);
    79  		support(RequestParserProvider.class);
    80  		support(PathResolverProvider.class);
    81  		support(ConverterProvider.class);
    82  	}
    83
    84  	/** サーブレットコンテキスト。 */
    85  	private ServletContext servletContext;
    86
    87  	/**
    88  	 * {@inheritDoc}
    89  	 */
    90  	@Override
    91  	public void initialize(final ServletContext servletContext)
    92  			throws Exception {
    93  		super.initialize(servletContext);
    94  		this.servletContext = servletContext;
    95  	}
    96
    97  	/**
    98  	 * {@inheritDoc}
    99  	 * <p>
    100 	 * {@link S2BeanELResolver} を {@link JspApplicationContext} に登録します。
    101 	 * </p>
    102 	 */
    103 	@Override
    104 	public void ready() {
    105 		final JspFactory jspFactory = JspFactory.getDefaultFactory();
    106 		if (jspFactory != null) {
    107 			final JspApplicationContext jspApplicationContext = jspFactory
    108 					.getJspApplicationContext(servletContext);
    109 			final S2Container container = SingletonS2ContainerFactory
    110 					.getContainer();
    111 			final ELResolver[] elResolvers = (ELResolver[]) container
    112 					.findAllComponents(ELResolver.class);
    113 			for (final ELResolver elResolver : elResolvers) {
    114 				jspApplicationContext.addELResolver(elResolver);
    115 				logger.log("ICUB0001", new Object[] { elResolver });
    116 			}
    117 		}
    118 	}
    119
    120 	/**
    121 	 * {@inheritDoc}
    122 	 */
    123 	@Override
    124 	public <S extends Provider> S getProvider(final Class<S> service) {
    125 		if (this.isSupport(service)) {
    126 			final S2Container container = SingletonS2ContainerFactory
    127 					.getContainer();
    128 			return service.cast(container.getComponent(service));
    129 		} else {
    130 			return null;
    131 		}
    132 	}
    133
    134 }