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