1 | |
package org.apache.onami.configuration; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import static com.google.inject.name.Names.named; |
23 | |
import static com.google.inject.spi.Elements.getElements; |
24 | |
import static com.google.inject.util.Modules.override; |
25 | |
import static java.util.Arrays.asList; |
26 | |
|
27 | |
import java.util.List; |
28 | |
import java.util.Map.Entry; |
29 | |
|
30 | |
import org.apache.onami.configuration.variables.AntStyleParser; |
31 | |
import org.apache.onami.configuration.variables.Parser; |
32 | |
import org.apache.onami.configuration.variables.VariablesMap; |
33 | |
|
34 | |
import com.google.inject.AbstractModule; |
35 | |
import com.google.inject.Binding; |
36 | |
import com.google.inject.Key; |
37 | |
import com.google.inject.Module; |
38 | |
import com.google.inject.TypeLiteral; |
39 | |
import com.google.inject.name.Named; |
40 | |
import com.google.inject.spi.DefaultElementVisitor; |
41 | |
import com.google.inject.spi.Element; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 240 | public final class OnamiVariablesExpander |
47 | |
extends AbstractModule |
48 | |
{ |
49 | |
|
50 | |
public static Module expandVariables( Module...baseModules ) |
51 | |
{ |
52 | 2 | return expandVariables( asList( baseModules ) ); |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public static Module expandVariables( Parser parser, Module...baseModules ) |
63 | |
{ |
64 | 0 | return expandVariables( parser, asList( baseModules ) ); |
65 | |
} |
66 | |
|
67 | |
public static Module expandVariables( Iterable<? extends Module> baseModules ) |
68 | |
{ |
69 | 2 | return expandVariables( new AntStyleParser(), baseModules ); |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public static Module expandVariables( Parser parser, Iterable<? extends Module> baseModules ) |
80 | |
{ |
81 | 2 | return override( baseModules ).with( new OnamiVariablesExpander( parser, getElements( baseModules ) ) ); |
82 | |
} |
83 | |
|
84 | 2 | private final TypeLiteral<String> stringLiteral = new TypeLiteral<String>(){}; |
85 | |
|
86 | |
private final Parser parser; |
87 | |
|
88 | |
private final List<Element> elements; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
private OnamiVariablesExpander( Parser parser, List<Element> elements ) |
94 | 2 | { |
95 | 2 | this.parser = parser; |
96 | 2 | this.elements = elements; |
97 | 2 | } |
98 | |
|
99 | |
@Override |
100 | |
protected void configure() |
101 | |
{ |
102 | 2 | final VariablesMap variablesMap = new VariablesMap( parser ); |
103 | |
|
104 | 2 | for ( final Element element : elements ) |
105 | |
{ |
106 | 240 | element.acceptVisitor( new DefaultElementVisitor<Void>() |
107 | 480 | { |
108 | |
|
109 | |
@Override |
110 | |
public <T> Void visit( Binding<T> binding ) |
111 | |
{ |
112 | 240 | Key<?> bindingKey = binding.getKey(); |
113 | |
|
114 | 240 | if ( stringLiteral.equals( bindingKey.getTypeLiteral() ) |
115 | |
&& bindingKey.getAnnotation() != null |
116 | |
&& ( Named.class.isAssignableFrom( bindingKey.getAnnotationType() ) |
117 | |
|| javax.inject.Named.class.isAssignableFrom( bindingKey.getAnnotationType() ) ) ) |
118 | |
{ |
119 | |
String propertyKey; |
120 | |
|
121 | 240 | if ( Named.class.isAssignableFrom( bindingKey.getAnnotationType() ) ) |
122 | |
{ |
123 | 240 | propertyKey = ( (Named) bindingKey.getAnnotation() ).value(); |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 0 | propertyKey = ( (javax.inject.Named) bindingKey.getAnnotation() ).value(); |
128 | |
} |
129 | |
|
130 | 240 | String propertyValue = (String) binding.getProvider().get(); |
131 | |
|
132 | 240 | variablesMap.put( propertyKey, propertyValue ); |
133 | |
} |
134 | |
|
135 | 240 | return super.visit( binding ); |
136 | |
} |
137 | |
|
138 | |
}); |
139 | |
} |
140 | |
|
141 | 2 | for ( Entry<String, String> variable : variablesMap.entrySet() ) |
142 | |
{ |
143 | 240 | bindConstant().annotatedWith( named( variable.getKey() ) ).to( variable.getValue() ); |
144 | |
} |
145 | 2 | } |
146 | |
|
147 | |
} |