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 java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.servlet.ServletContext;
23  import javax.servlet.ServletContextEvent;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import com.google.inject.Guice;
29  import com.google.inject.Injector;
30  import com.google.inject.Module;
31  import com.google.inject.servlet.GuiceServletContextListener;
32  
33  /**
34   * {@link Injector} を初期化するための {@code Servlet} コンテキストのリスナです。
35   * <p>
36   * アプリケーションが使用するモジュールのクラス名を WEB 配備記述子の初期化パラメータ {@value #MODULE_INIT_PARAM_NAME}
37   * に指定してください。
38   * </p>
39   * <p>
40   * 例
41   * 
42   * <pre>
43   * &lt;context-param&gt;
44   *   &lt;param-name&gt;{@value #MODULE_INIT_PARAM_NAME}&lt;/paanm-name&gt;
45   *   &lt;param-value&gt;com.example.ApplicationModule&lt;/param-value&gt;
46   * &lt;/context-param&gt;
47   * </pre>
48   * 
49   * </p>
50   * 
51   * @author baba
52   */
53  public class CubbyGuiceServletContextListener extends
54  		GuiceServletContextListener {
55  
56  	private static final Logger logger = LoggerFactory
57  			.getLogger(CubbyGuiceServletContextListener.class.getName());
58  
59  	/** モジュールの WEB 配備記述子の初期化パラメータ名 */
60  	public static final String MODULE_INIT_PARAM_NAME = "cubby.guice.module";
61  
62  	/** インスタンス化するモジュールのクラス名 */
63  	private String[] moduleClassNames;
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	@Override
69  	public void contextInitialized(ServletContextEvent servletContextEvent) {
70  		final ServletContext servletContext = servletContextEvent
71  				.getServletContext();
72  		final String moduleClassNamesString = servletContext
73  				.getInitParameter(MODULE_INIT_PARAM_NAME);
74  		if (moduleClassNamesString == null
75  				|| moduleClassNamesString.length() == 0) {
76  			throw new IllegalArgumentException("No context parameter \""
77  					+ MODULE_INIT_PARAM_NAME + "\", please set Module FQCN");
78  		}
79  		this.moduleClassNames = moduleClassNamesString.split(",");
80  
81  		super.contextInitialized(servletContextEvent);
82  	}
83  
84  	/**
85  	 * 指定されたクラス名のモジュールを生成します。
86  	 * 
87  	 * @param moduleClassName
88  	 *            モジュールのクラス名
89  	 * @return インジェクタ
90  	 */
91  	protected Module createModule(final String moduleClassName) {
92  		if (logger.isInfoEnabled()) {
93  			logger.info("Instantiates " + moduleClassName);
94  		}
95  
96  		final ClassLoader loader = Thread.currentThread()
97  				.getContextClassLoader();
98  		try {
99  			final Class<?> clazz = Class.forName(moduleClassName, true, loader);
100 			final Module module = Module.class.cast(clazz.newInstance());
101 			return module;
102 		} catch (final ClassNotFoundException e) {
103 			throw new IllegalArgumentException("Illegal module "
104 					+ moduleClassName, e);
105 		} catch (final InstantiationException e) {
106 			throw new IllegalArgumentException("Illegal module "
107 					+ moduleClassName, e);
108 		} catch (final IllegalAccessException e) {
109 			throw new IllegalArgumentException("Illegal module "
110 					+ moduleClassName, e);
111 		}
112 	}
113 
114 	/**
115 	 * {@inheritDoc}
116 	 */
117 	@Override
118 	protected Injector getInjector() {
119 		final List<Module> modules = new ArrayList<Module>();
120 		for (final String moduleClassName : this.moduleClassNames) {
121 			final Module module = createModule(moduleClassName.trim());
122 			modules.add(module);
123 		}
124 		return Guice.createInjector(modules);
125 	}
126 
127 }