##### Coverage Report - org.seasar.cubby.plugins.guice.GuicePlugin

Classes in this File | Line Coverage | Branch Coverage | Complexity
GuicePlugin |
73% | 14/19
50% | 2/4
| 1.8;1.8

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 | 1 |
             public GuicePlugin() {
65 | 1 |
                     support(BeanDescProvider.class);
66 | 1 |
                     support(ContainerProvider.class);
67 | 1 |
                     support(RequestParserProvider.class);
68 | 1 |
                     support(PathResolverProvider.class);
69 | 1 |
                     support(ConverterProvider.class);
70 | 1 |
             }
71 |  |
72 |  |
             /**
73 |  |
              * {@inheritDoc}
74 |  |
              */
75 |  |
             @Override
76 |  |
             public void initialize(final ServletContext servletContext)
77 |  |
                             throws Exception {
78 | 1 |
                     super.initialize(servletContext);
79 |  |
80 | 1 |
                     this.injector = (Injector) servletContext.getAttribute(Injector.class
81 |  |
                                     .getName());
82 | 1 |
                     if (this.injector == null) {
83 | 0 |
                             throw new IllegalStateException(Injector.class.getName()
84 |  |
                                             + "is not confugured.");
85 |  |
                     }
86 | 1 |
             }
87 |  |
88 |  |
             /**
89 |  |
              * {@inheritDoc}
90 |  |
              */
91 |  |
             @Override
92 |  |
             public void destroy() {
93 | 0 |
                     this.injector = null;
94 | 0 |
                     super.destroy();
95 | 0 |
             }
96 |  |
97 |  |
             /**
98 |  |
              * {@inheritDoc}
99 |  |
              */
100 |  |
             @Override
101 |  |
             public <S extends Provider> S getProvider(final Class<S> service) {
102 | 3 |
                     if (this.isSupport(service)) {
103 | 3 |
                             return service.cast(this.injector.getInstance(service));
104 |  |
                     } else {
105 | 0 |
                             return null;
106 |  |
                     }
107 |  |
             }
108 |  |
109 |  |
             /**
110 |  |
              * インジェクタを取得します。
111 |  |
              *
112 |  |
              * @return インジェクタ
113 |  |
              */
114 |  |
             public Injector getInjector() {
115 | 1 |
                     return injector;
116 |  |
             }
117 |  |
118 |  |
     }