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