Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GuiceContainerProvider |
|
| 0.0;0 | ||||
GuiceContainerProvider$GuiceContainerImpl |
|
| 0.0;0 |
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.spi; | |
17 | ||
18 | import java.util.Collections; | |
19 | import java.util.HashSet; | |
20 | import java.util.Set; | |
21 | ||
22 | import org.seasar.cubby.spi.ContainerProvider; | |
23 | import org.seasar.cubby.spi.container.Container; | |
24 | import org.seasar.cubby.spi.container.LookupException; | |
25 | ||
26 | import com.google.inject.Inject; | |
27 | import com.google.inject.Injector; | |
28 | ||
29 | /** | |
30 | * Guice の {@link Injector} による {@link Container} の実装を提供します。 | |
31 | * | |
32 | * @author baba | |
33 | * @since 2.0.0 | |
34 | */ | |
35 | 0 | public class GuiceContainerProvider implements ContainerProvider { |
36 | ||
37 | /** ルックアップに失敗したことを表す例外クラス名。 */ | |
38 | private static final Set<String> LOOKUP_FAILURE_EXCEPTION_CLASS_NAMES; | |
39 | static { | |
40 | 0 | final Set<String> set = new HashSet<String>(); |
41 | 0 | set.add("com.google.inject.ConfigurationException"); |
42 | 0 | LOOKUP_FAILURE_EXCEPTION_CLASS_NAMES = Collections.unmodifiableSet(set); |
43 | 0 | } |
44 | ||
45 | /** {@link Container} */ | |
46 | private final Container container; | |
47 | ||
48 | /** | |
49 | * インスタンス化します。 | |
50 | */ | |
51 | @Inject | |
52 | 0 | public GuiceContainerProvider(final Injector injector) { |
53 | 0 | this.container = new GuiceContainerImpl(injector); |
54 | 0 | } |
55 | ||
56 | /** | |
57 | * {@inheritDoc} | |
58 | */ | |
59 | public Container getContainer() { | |
60 | 0 | return container; |
61 | } | |
62 | ||
63 | /** | |
64 | * {@link Injector} による {@link Container} の実装です。 | |
65 | * | |
66 | * @author baba | |
67 | * @since 2.0.0 | |
68 | */ | |
69 | private class GuiceContainerImpl implements Container { | |
70 | ||
71 | /** インスタンスをルックアップするための {@link Injector} */ | |
72 | private final Injector injector; | |
73 | ||
74 | /** | |
75 | * インスタンス化します。 | |
76 | * | |
77 | * @param injector | |
78 | * インスタンスをルックアップするための {@link Injector} | |
79 | */ | |
80 | 0 | public GuiceContainerImpl(final Injector injector) { |
81 | 0 | this.injector = injector; |
82 | 0 | } |
83 | ||
84 | /** | |
85 | * {@inheritDoc} | |
86 | */ | |
87 | public <T> T lookup(final Class<T> type) throws LookupException { | |
88 | try { | |
89 | 0 | return injector.getInstance(type); |
90 | 0 | } catch (final RuntimeException e) { |
91 | 0 | if (LOOKUP_FAILURE_EXCEPTION_CLASS_NAMES.contains(e.getClass() |
92 | .getName())) { | |
93 | 0 | throw new LookupException(e); |
94 | } else { | |
95 | 0 | throw e; |
96 | } | |
97 | } | |
98 | } | |
99 | ||
100 | } | |
101 | ||
102 | } |