1
2
3
4
5
6
7
8
9
10 package org.mule.module.guice;
11
12 import org.mule.api.MuleContext;
13 import org.mule.api.lifecycle.InitialisationException;
14 import org.mule.api.registry.RegistrationException;
15 import org.mule.config.i18n.MessageFactory;
16 import org.mule.registry.AbstractRegistry;
17
18 import com.google.inject.Binding;
19 import com.google.inject.Injector;
20 import com.google.inject.Key;
21 import com.google.inject.TypeLiteral;
22 import com.google.inject.name.Named;
23
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.guiceyfruit.Injectors;
30 import org.guiceyfruit.support.CloseFailedException;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class GuiceRegistry extends AbstractRegistry
48 {
49 private Injector injector = null;
50
51 public GuiceRegistry(MuleContext muleContext)
52 {
53 super("guice", muleContext);
54 }
55
56
57 GuiceRegistry(Injector injector, MuleContext muleContext)
58 {
59 this(muleContext);
60 this.injector = injector;
61 }
62
63 protected void doInitialise() throws InitialisationException
64 {
65
66 }
67
68 protected void doDispose()
69 {
70 try
71 {
72 Injectors.close(injector);
73 }
74 catch (CloseFailedException e)
75 {
76 logger.error("Failed to close the Guice registry cleanly", e);
77 }
78 }
79
80 public <T> T lookupObject(String key)
81 {
82
83 for (Map.Entry<Key<?>, Binding<?>> entry : injector.getBindings().entrySet())
84 {
85 if (entry.getKey().getAnnotation() instanceof Named)
86 {
87 String name = ((Named) entry.getKey().getAnnotation()).value();
88 if (name.equals(key))
89 {
90 Object o = entry.getValue().getProvider().get();
91 return (T) o;
92 }
93 }
94 }
95 return null;
96 }
97
98 @Override
99 public <T> T lookupObject(Class<T> type) throws RegistrationException
100 {
101
102
103 List<Binding<T>> bindings = injector.findBindingsByType(TypeLiteral.get(type));
104 if(bindings.size()==0)
105 {
106 return null;
107
108 }
109 else if (bindings.size()==1)
110 {
111 return bindings.get(0).getProvider().get();
112 }
113 else
114 {
115 throw new RegistrationException(MessageFactory.createStaticMessage("More than one object of type: " + type + ", was found"));
116 }
117 }
118
119 public <T> Map<String, T> lookupByType(Class<T> type)
120 {
121
122
123 return Collections.EMPTY_MAP;
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 }
159
160 public <T> Collection<T> lookupObjects(Class<T> type)
161 {
162 return Collections.emptyList();
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 }
183
184 public void registerObject(String key, Object value) throws RegistrationException
185 {
186 throw new UnsupportedOperationException("registerObject");
187 }
188
189 public void registerObject(String key, Object value, Object metadata) throws RegistrationException
190 {
191 throw new UnsupportedOperationException("registerObject");
192 }
193
194 public void registerObjects(Map objects) throws RegistrationException
195 {
196 throw new UnsupportedOperationException("registerObjects");
197 }
198
199 public void unregisterObject(String key) throws RegistrationException
200 {
201 throw new UnsupportedOperationException("unregisterObject");
202 }
203
204 public void unregisterObject(String key, Object metadata) throws RegistrationException
205 {
206 throw new UnsupportedOperationException("unregisterObject");
207 }
208
209 public boolean isReadOnly()
210 {
211 return true;
212 }
213
214 public boolean isRemote()
215 {
216 return false;
217 }
218 }