1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35 public class GuiceContainerProvider implements ContainerProvider {
36
37
38 private static final Set<String> LOOKUP_FAILURE_EXCEPTION_CLASS_NAMES;
39 static {
40 final Set<String> set = new HashSet<String>();
41 set.add("com.google.inject.ConfigurationException");
42 LOOKUP_FAILURE_EXCEPTION_CLASS_NAMES = Collections.unmodifiableSet(set);
43 }
44
45
46 private final Container container;
47
48
49
50
51 @Inject
52 public GuiceContainerProvider(final Injector injector) {
53 this.container = new GuiceContainerImpl(injector);
54 }
55
56
57
58
59 public Container getContainer() {
60 return container;
61 }
62
63
64
65
66
67
68
69 private class GuiceContainerImpl implements Container {
70
71
72 private final Injector injector;
73
74
75
76
77
78
79
80 public GuiceContainerImpl(final Injector injector) {
81 this.injector = injector;
82 }
83
84
85
86
87 public <T> T lookup(final Class<T> type) throws LookupException {
88 try {
89 return injector.getInstance(type);
90 } catch (final RuntimeException e) {
91 if (LOOKUP_FAILURE_EXCEPTION_CLASS_NAMES.contains(e.getClass()
92 .getName())) {
93 throw new LookupException(e);
94 } else {
95 throw e;
96 }
97 }
98 }
99
100 }
101
102 }