1 | |
package org.apache.onami.autobind.scanner.features; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
import static java.lang.String.format; |
21 | |
import static java.util.logging.Level.FINE; |
22 | |
import static java.util.logging.Level.INFO; |
23 | |
import static java.util.logging.Logger.getLogger; |
24 | |
|
25 | |
import java.lang.annotation.Annotation; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.HashSet; |
28 | |
import java.util.Map; |
29 | |
import java.util.Set; |
30 | |
import java.util.logging.Logger; |
31 | |
|
32 | |
import javax.inject.Inject; |
33 | |
import javax.inject.Provider; |
34 | |
|
35 | |
import org.apache.onami.autobind.install.BindingStage; |
36 | |
import org.apache.onami.autobind.install.BindingTracer; |
37 | |
import org.apache.onami.autobind.install.InstallationContext; |
38 | |
import org.apache.onami.autobind.install.InstallationContext.StageableRequest; |
39 | |
import org.apache.onami.autobind.install.bindjob.BindingJob; |
40 | |
import org.apache.onami.autobind.install.bindjob.ConstantBindingJob; |
41 | |
import org.apache.onami.autobind.install.bindjob.ImplementationBindingJob; |
42 | |
import org.apache.onami.autobind.install.bindjob.InstanceBindingJob; |
43 | |
import org.apache.onami.autobind.install.bindjob.InterfaceBindingJob; |
44 | |
import org.apache.onami.autobind.install.bindjob.ProviderBindingJob; |
45 | |
import org.apache.onami.autobind.scanner.ScannerModule; |
46 | |
import org.apache.onami.autobind.utils.VariableResolver; |
47 | |
|
48 | |
import com.google.inject.Binder; |
49 | |
import com.google.inject.Injector; |
50 | |
import com.google.inject.binder.AnnotatedBindingBuilder; |
51 | |
import com.google.inject.binder.LinkedBindingBuilder; |
52 | |
import com.google.inject.binder.ScopedBindingBuilder; |
53 | |
import com.google.inject.util.Providers; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 0 | public abstract class BindingScannerFeature |
61 | |
implements ScannerFeature |
62 | |
{ |
63 | |
|
64 | 0 | private final Logger _logger = getLogger( getClass().getName() ); |
65 | |
|
66 | 0 | protected Set<String> others = new HashSet<String>(); |
67 | |
|
68 | 0 | protected Set<String> qualifiers = new HashSet<String>(); |
69 | |
|
70 | |
protected Binder _binder; |
71 | |
|
72 | |
@Inject |
73 | |
protected Injector injector; |
74 | |
|
75 | |
@Inject |
76 | |
protected BindingTracer tracer; |
77 | |
|
78 | |
@Inject |
79 | |
protected VariableResolver resolver; |
80 | |
|
81 | |
protected InstallationContext context; |
82 | |
|
83 | |
public void setBinder( Binder binder ) |
84 | |
{ |
85 | 0 | _binder = binder; |
86 | 0 | } |
87 | |
|
88 | |
@Inject |
89 | |
public void configure( InstallationContext context ) |
90 | |
{ |
91 | 0 | this.context = context; |
92 | 0 | } |
93 | |
|
94 | |
@Override |
95 | |
public void found( final Class<Object> annotatedClass, final Map<String, Annotation> annotations ) |
96 | |
{ |
97 | 0 | final BindingStage stage = accept( annotatedClass, annotations ); |
98 | 0 | if ( stage != BindingStage.IGNORE ) |
99 | |
{ |
100 | 0 | context.add( new StageableRequest() |
101 | 0 | { |
102 | 0 | private Class<Object> _annotatedClass = annotatedClass; |
103 | |
|
104 | 0 | private Map<String, Annotation> _annotations = new HashMap<String, Annotation>( annotations ); |
105 | |
|
106 | |
@Override |
107 | |
public Void call() |
108 | |
throws Exception |
109 | |
{ |
110 | 0 | process( _annotatedClass, _annotations ); |
111 | 0 | return null; |
112 | |
} |
113 | |
|
114 | |
@Override |
115 | |
public BindingStage getExecutionStage() |
116 | |
{ |
117 | 0 | return stage; |
118 | |
} |
119 | |
} ); |
120 | |
} |
121 | 0 | } |
122 | |
|
123 | |
public abstract BindingStage accept( Class<Object> annotatedClass, Map<String, Annotation> annotations ); |
124 | |
|
125 | |
public abstract void process( Class<Object> annotatedClass, Map<String, Annotation> annotations ); |
126 | |
|
127 | |
protected <T, V extends T> void bindProvider( final Provider<V> provider, Class<T> interf, Annotation annotation, |
128 | |
Class<? extends Annotation> scope ) |
129 | |
{ |
130 | 0 | BindingJob job = new ProviderBindingJob( scope, provider.getClass(), annotation, interf.getName() ); |
131 | 0 | if ( !tracer.contains( job ) ) |
132 | |
{ |
133 | |
LinkedBindingBuilder<T> builder; |
134 | 0 | synchronized ( _binder ) |
135 | |
{ |
136 | 0 | builder = _binder.bind( interf ); |
137 | 0 | if ( annotation != null ) |
138 | |
{ |
139 | 0 | builder = ( (AnnotatedBindingBuilder<T>) builder ).annotatedWith( annotation ); |
140 | |
} |
141 | 0 | ScopedBindingBuilder scopedBuilder = builder.toProvider( Providers.guicify( provider ) ); |
142 | 0 | if ( scope != null ) |
143 | |
{ |
144 | 0 | scopedBuilder.in( scope ); |
145 | |
} |
146 | 0 | } |
147 | 0 | tracer.add( job ); |
148 | 0 | } |
149 | |
else |
150 | |
{ |
151 | 0 | String message = format( "Ignoring BindingJob \"%s\", because it was already bound.", job ); |
152 | |
|
153 | 0 | if ( _logger.isLoggable( FINE ) ) |
154 | |
{ |
155 | 0 | _logger.log( FINE, message, new Exception( message ) ); |
156 | |
} |
157 | 0 | else if ( _logger.isLoggable( INFO ) ) |
158 | |
{ |
159 | 0 | _logger.log( INFO, message ); |
160 | |
} |
161 | |
} |
162 | 0 | } |
163 | |
|
164 | |
protected <T, V extends T> void bindProvider( final Class<? extends Provider<V>> provider, Class<T> interf, |
165 | |
Annotation annotation, Class<? extends Annotation> scope ) |
166 | |
{ |
167 | 0 | BindingJob job = new ProviderBindingJob( scope, provider, annotation, interf.getName() ); |
168 | 0 | if ( !tracer.contains( job ) ) |
169 | |
{ |
170 | |
LinkedBindingBuilder<T> builder; |
171 | 0 | synchronized ( _binder ) |
172 | |
{ |
173 | 0 | builder = _binder.bind( interf ); |
174 | 0 | if ( annotation != null ) |
175 | |
{ |
176 | 0 | builder = ( (AnnotatedBindingBuilder<T>) builder ).annotatedWith( annotation ); |
177 | |
} |
178 | 0 | ScopedBindingBuilder scopedBuilder = builder.toProvider( provider ); |
179 | 0 | if ( scope != null ) |
180 | |
{ |
181 | 0 | scopedBuilder.in( scope ); |
182 | |
} |
183 | 0 | } |
184 | 0 | tracer.add( job ); |
185 | 0 | } |
186 | |
else |
187 | |
{ |
188 | 0 | String message = format( "Ignoring BindingJob \"%s\", because it was already bound.", job ); |
189 | |
|
190 | 0 | if ( _logger.isLoggable( FINE ) ) |
191 | |
{ |
192 | 0 | _logger.log( FINE, message, new Exception( message ) ); |
193 | |
} |
194 | 0 | else if ( _logger.isLoggable( INFO ) ) |
195 | |
{ |
196 | 0 | _logger.log( INFO, message ); |
197 | |
} |
198 | |
} |
199 | 0 | } |
200 | |
|
201 | |
protected <T, V extends T> void bindInstance( V implementation, Class<T> interf, Annotation annotation, |
202 | |
Class<? extends Annotation> scope ) |
203 | |
{ |
204 | 0 | BindingJob job = |
205 | |
new InstanceBindingJob( scope, annotation, implementation.getClass().getName(), interf.getName() ); |
206 | |
|
207 | 0 | if ( !tracer.contains( job ) ) |
208 | |
{ |
209 | |
LinkedBindingBuilder<T> builder; |
210 | 0 | synchronized ( _binder ) |
211 | |
{ |
212 | 0 | builder = _binder.bind( interf ); |
213 | 0 | if ( annotation != null ) |
214 | |
{ |
215 | 0 | builder = ( (AnnotatedBindingBuilder<T>) builder ).annotatedWith( annotation ); |
216 | |
} |
217 | 0 | builder.toInstance( implementation ); |
218 | 0 | } |
219 | 0 | tracer.add( job ); |
220 | 0 | } |
221 | |
else |
222 | |
{ |
223 | 0 | String message = format( "Ignoring BindingJob \"%s\", because it was already bound.", job ); |
224 | |
|
225 | 0 | if ( _logger.isLoggable( FINE ) ) |
226 | |
{ |
227 | 0 | _logger.log( FINE, message, new Exception( message ) ); |
228 | |
} |
229 | 0 | else if ( _logger.isLoggable( INFO ) ) |
230 | |
{ |
231 | 0 | _logger.log( INFO, message ); |
232 | |
} |
233 | |
} |
234 | 0 | } |
235 | |
|
236 | |
protected void bindConstant( String value, Annotation annotation ) |
237 | |
{ |
238 | 0 | BindingJob job = new ConstantBindingJob( annotation, value.getClass().getName() ); |
239 | 0 | if ( !tracer.contains( job ) ) |
240 | |
{ |
241 | 0 | synchronized ( _binder ) |
242 | |
{ |
243 | 0 | _binder.bindConstant().annotatedWith( annotation ).to( value ); |
244 | 0 | } |
245 | 0 | tracer.add( job ); |
246 | |
} |
247 | |
else |
248 | |
{ |
249 | 0 | String message = format( "Ignoring BindingJob \"%s\", because it was already bound.", job ); |
250 | |
|
251 | 0 | if ( _logger.isLoggable( FINE ) ) |
252 | |
{ |
253 | 0 | _logger.log( FINE, message, new Exception( message ) ); |
254 | |
} |
255 | 0 | else if ( _logger.isLoggable( INFO ) ) |
256 | |
{ |
257 | 0 | _logger.log( INFO, message ); |
258 | |
} |
259 | |
} |
260 | 0 | } |
261 | |
|
262 | |
protected <T, V extends T> void bind( Class<V> implementationClass, Class<T> interf, Annotation annotation, |
263 | |
Class<? extends Annotation> scope ) |
264 | |
{ |
265 | 0 | BindingJob job = new InterfaceBindingJob( scope, annotation, implementationClass.getName(), interf.getName() ); |
266 | |
|
267 | 0 | if ( !tracer.contains( job ) ) |
268 | |
{ |
269 | |
LinkedBindingBuilder<T> builder; |
270 | 0 | synchronized ( _binder ) |
271 | |
{ |
272 | 0 | builder = _binder.bind( interf ); |
273 | 0 | if ( annotation != null ) |
274 | |
{ |
275 | 0 | builder = ( (AnnotatedBindingBuilder<T>) builder ).annotatedWith( annotation ); |
276 | |
} |
277 | 0 | ScopedBindingBuilder scopedBindingBuilder = builder.to( implementationClass ); |
278 | 0 | if ( scope != null ) |
279 | |
{ |
280 | 0 | scopedBindingBuilder.in( scope ); |
281 | |
} |
282 | 0 | } |
283 | 0 | tracer.add( job ); |
284 | 0 | } |
285 | |
else |
286 | |
{ |
287 | 0 | String message = format( "Ignoring BindingJob \"%s\", because it was already bound.", job ); |
288 | |
|
289 | 0 | if ( _logger.isLoggable( FINE ) ) |
290 | |
{ |
291 | 0 | _logger.log( FINE, message, new Exception( message ) ); |
292 | |
} |
293 | 0 | else if ( _logger.isLoggable( INFO ) ) |
294 | |
{ |
295 | 0 | _logger.log( INFO, message ); |
296 | |
} |
297 | |
} |
298 | 0 | } |
299 | |
|
300 | |
protected <T> void bind( Class<T> implementationClass, Annotation annotation, Class<? extends Annotation> scope ) |
301 | |
{ |
302 | 0 | BindingJob job = new ImplementationBindingJob( scope, annotation, implementationClass.getName() ); |
303 | |
|
304 | 0 | if ( !tracer.contains( job ) ) |
305 | |
{ |
306 | |
LinkedBindingBuilder<T> builder; |
307 | 0 | synchronized ( _binder ) |
308 | |
{ |
309 | 0 | builder = _binder.bind( implementationClass ); |
310 | 0 | if ( annotation != null ) |
311 | |
{ |
312 | 0 | builder = ( (AnnotatedBindingBuilder<T>) builder ).annotatedWith( annotation ); |
313 | |
} |
314 | 0 | if ( scope != null ) |
315 | |
{ |
316 | 0 | builder.in( scope ); |
317 | |
} |
318 | 0 | } |
319 | 0 | tracer.add( job ); |
320 | 0 | } |
321 | |
else |
322 | |
{ |
323 | 0 | String message = format( "Ignoring BindingJob \"%s\", because it was already bound.", job ); |
324 | |
|
325 | 0 | if ( _logger.isLoggable( FINE ) ) |
326 | |
{ |
327 | 0 | _logger.log( FINE, message, new Exception( message ) ); |
328 | |
} |
329 | 0 | else if ( _logger.isLoggable( INFO ) ) |
330 | |
{ |
331 | 0 | _logger.log( INFO, message ); |
332 | |
} |
333 | |
} |
334 | 0 | } |
335 | |
|
336 | |
} |