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  		}
68  	}
69  
70  	/**
71  	 * インスタンス化します。
72  	 */
73  	public S2ContainerPlugin() {
74  		support(BeanDescProvider.class);
75  		support(ContainerProvider.class);
76  		support(RequestParserProvider.class);
77  		support(PathResolverProvider.class);
78  		support(ConverterProvider.class);
79  	}
80  
81  	/** サーブレットコンテキスト。 */
82  	private ServletContext servletContext;
83  
84  	/**
85  	 * {@inheritDoc}
86  	 */
87  	@Override
88  	public void initialize(final ServletContext servletContext) {
89  		this.servletContext = servletContext;
90  	}
91  
92  	/**
93  	 * {@inheritDoc}
94  	 * <p>
95  	 * {@link S2BeanELResolver} を {@link JspApplicationContext} に登録します。
96  	 * </p>
97  	 */
98  	@Override
99  	public void ready() {
100 		final JspFactory jspFactory = JspFactory.getDefaultFactory();
101 		if (jspFactory != null) {
102 			final JspApplicationContext jspApplicationContext = jspFactory
103 					.getJspApplicationContext(servletContext);
104 			final S2Container container = SingletonS2ContainerFactory
105 					.getContainer();
106 			final ELResolver[] elResolvers = (ELResolver[]) container
107 					.findAllComponents(ELResolver.class);
108 			for (final ELResolver elResolver : elResolvers) {
109 				jspApplicationContext.addELResolver(elResolver);
110 				logger.log("ICUB0001", new Object[] { elResolver });
111 			}
112 		}
113 	}
114 
115 	/**
116 	 * {@inheritDoc}
117 	 */
118 	public <S extends Provider> S getProvider(final Class<S> service) {
119 		if (this.isSupport(service)) {
120 			final S2Container container = SingletonS2ContainerFactory
121 					.getContainer();
122 			return service.cast(container.getComponent(service));
123 		} else {
124 			return null;
125 		}
126 	}
127 
128 }