1 | |
package org.apache.onami.autobind.annotations.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 com.google.inject.multibindings.Multibinder.newSetBinder; |
22 | |
import static java.util.logging.Level.FINE; |
23 | |
import static java.util.logging.Level.INFO; |
24 | |
import static org.apache.onami.autobind.install.BindingStage.BINDING; |
25 | |
import static org.apache.onami.autobind.install.BindingStage.IGNORE; |
26 | |
|
27 | |
import java.lang.annotation.Annotation; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
import javax.inject.Singleton; |
31 | |
|
32 | |
import org.apache.onami.autobind.annotations.Bind; |
33 | |
import org.apache.onami.autobind.install.BindingStage; |
34 | |
import org.apache.onami.autobind.install.bindjob.BindingJob; |
35 | |
import org.apache.onami.autobind.install.bindjob.MultiBindingJob; |
36 | |
|
37 | |
import com.google.inject.binder.ScopedBindingBuilder; |
38 | |
import com.google.inject.multibindings.Multibinder; |
39 | |
|
40 | |
@Singleton |
41 | 0 | public class MultiBindingFeature |
42 | |
extends AutoBindingFeature |
43 | |
{ |
44 | |
|
45 | |
@Override |
46 | |
public BindingStage accept( Class<Object> annotatedClass, Map<String, Annotation> annotations ) |
47 | |
{ |
48 | 0 | if ( annotations.containsKey( Bind.class.getName() ) ) |
49 | |
{ |
50 | 0 | Bind annotation = (Bind) annotations.get( Bind.class.getName() ); |
51 | 0 | if ( annotation.multiple() ) |
52 | |
{ |
53 | 0 | return BINDING; |
54 | |
} |
55 | |
} |
56 | 0 | return IGNORE; |
57 | |
} |
58 | |
|
59 | |
@Override |
60 | |
protected <T, V extends T> void bind( Class<V> implementationClass, Class<T> interf, Annotation annotation, |
61 | |
Class<? extends Annotation> scope ) |
62 | |
{ |
63 | 0 | BindingJob job = new MultiBindingJob( scope, annotation, implementationClass.getName(), interf.getName() ); |
64 | |
|
65 | 0 | if ( !tracer.contains( job ) ) |
66 | |
{ |
67 | |
Multibinder<T> builder; |
68 | 0 | synchronized ( _binder ) |
69 | |
{ |
70 | 0 | if ( annotation != null ) |
71 | |
{ |
72 | 0 | builder = newSetBinder( _binder, interf, annotation ); |
73 | |
} |
74 | |
else |
75 | |
{ |
76 | 0 | builder = newSetBinder( _binder, interf ); |
77 | |
} |
78 | |
|
79 | 0 | ScopedBindingBuilder scopedBindingBuilder = builder.addBinding().to( implementationClass ); |
80 | 0 | if ( scope != null ) |
81 | |
{ |
82 | 0 | scopedBindingBuilder.in( scope ); |
83 | |
} |
84 | 0 | } |
85 | 0 | tracer.add( job ); |
86 | 0 | } |
87 | |
else |
88 | |
{ |
89 | 0 | String message = format( "Ignoring Multi-BindingJob \"%s\", because it was already bound.", job ); |
90 | |
|
91 | 0 | if ( _logger.isLoggable( FINE ) ) |
92 | |
{ |
93 | 0 | _logger.log( FINE, message, new Exception( message ) ); |
94 | |
} |
95 | 0 | else if ( _logger.isLoggable( INFO ) ) |
96 | |
{ |
97 | 0 | _logger.log( INFO, message ); |
98 | |
} |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
} |