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