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.guice;
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  
29  import com.google.inject.Injector;
30  import com.google.inject.servlet.GuiceServletContextListener;
31  
32  /**
33   * Cubby を <a href="http://code.google.com/p/google-guice/">Google Guice</a>
34   * に統合するためのプラグインです。
35   * <p>
36   * {@link GuiceServletContextListener} のサブクラスを web.xml に登録して {@link Injector}
37   * を初期化して下さい。 Cubby では {@link CubbyGuiceServletContextListener}
38   * を提供しているのでこれを使用することができます。
39   * </p>
40   * <p>
41   * このプラグインが提供するプロバイダは以下の通りです。
42   * <ul>
43   * <li>{@link BeanDescProvider}</li>
44   * <li>{@link ContainerProvider}</li>
45   * <li>{@link RequestParserProvider}</li>
46   * <li>{@link PathResolverProvider}</li>
47   * <li>{@link ConverterProvider}</li>
48   * </ul>
49   * </p>
50   * 
51   * @see <a href="http://code.google.com/p/google-guice/">Google&nbsp;Guice</a>
52   * @author baba
53   */
54  public class GuicePlugin extends AbstractPlugin {
55  
56  	/** モジュールの WEB 配備記述子の初期化パラメータ名 */
57  	public static final String MODULE_INIT_PARAM_NAME = "cubby.guice.module";
58  
59  	/** インジェクタ */
60  	private Injector injector;
61  
62  	/**
63  	 * インスタンス化します。
64  	 */
65  	public GuicePlugin() {
66  		support(BeanDescProvider.class);
67  		support(ContainerProvider.class);
68  		support(RequestParserProvider.class);
69  		support(PathResolverProvider.class);
70  		support(ConverterProvider.class);
71  	}
72  
73  	/**
74  	 * {@inheritDoc}
75  	 */
76  	@Override
77  	public void initialize(final ServletContext servletContext)
78  			throws Exception {
79  		super.initialize(servletContext);
80  
81  		this.injector = (Injector) servletContext.getAttribute(Injector.class
82  				.getName());
83  		if (this.injector == null) {
84  			throw new IllegalStateException(Injector.class.getName()
85  					+ "is not confugured.");
86  		}
87  	}
88  
89  	/**
90  	 * {@inheritDoc}
91  	 */
92  	@Override
93  	public void destroy() {
94  		this.injector = null;
95  		super.destroy();
96  	}
97  
98  	/**
99  	 * {@inheritDoc}
100 	 */
101 	@Override
102 	public <S extends Provider> S getProvider(final Class<S> service) {
103 		if (this.isSupport(service)) {
104 			return service.cast(this.injector.getInstance(service));
105 		} else {
106 			return null;
107 		}
108 	}
109 
110 	/**
111 	 * インジェクタを取得します。
112 	 * 
113 	 * @return インジェクタ
114 	 */
115 	public Injector getInjector() {
116 		return injector;
117 	}
118 
119 }