1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.apache.onami.autobind.utils; |
21 | |
|
22 | |
import java.util.StringTokenizer; |
23 | |
|
24 | |
import javax.inject.Inject; |
25 | |
|
26 | |
import org.apache.onami.autobind.jsr330.Names; |
27 | |
|
28 | |
import com.google.inject.AbstractModule; |
29 | |
import com.google.inject.Guice; |
30 | |
import com.google.inject.Injector; |
31 | |
import com.google.inject.Key; |
32 | |
|
33 | |
|
34 | 0 | public class VariableResolver { |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
private static final String VAR_BEGIN = "$"; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private static final String PIPE_SEPARATOR = "|"; |
45 | |
|
46 | |
private static final String KEY_PREFIX = "${"; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
@Inject |
52 | |
private Injector injector; |
53 | |
|
54 | |
public String resolve(final String pattern) { |
55 | 0 | StringBuilder buffer = new StringBuilder(); |
56 | |
|
57 | 0 | int prev = 0; |
58 | |
int pos; |
59 | 0 | while ((pos = pattern.indexOf(VAR_BEGIN, prev)) >= 0) { |
60 | 0 | if (pos > 0) { |
61 | 0 | buffer.append(pattern.substring(prev, pos)); |
62 | |
} |
63 | 0 | if (pos == pattern.length() - 1) { |
64 | 0 | buffer.append(VAR_BEGIN); |
65 | 0 | prev = pos + 1; |
66 | 0 | } else if (pattern.charAt(pos + 1) != '{') { |
67 | 0 | if (pattern.charAt(pos + 1) == '$') { |
68 | 0 | buffer.append(VAR_BEGIN); |
69 | 0 | prev = pos + 2; |
70 | |
} else { |
71 | 0 | buffer.append(pattern.substring(pos, pos + 2)); |
72 | 0 | prev = pos + 2; |
73 | |
} |
74 | |
} else { |
75 | 0 | int endName = pattern.indexOf('}', pos); |
76 | 0 | if (endName < 0) { |
77 | 0 | throw new IllegalArgumentException("Syntax error in property: " + pattern); |
78 | |
} |
79 | 0 | StringTokenizer keyTokenizer = new StringTokenizer(pattern.substring(pos + 2, endName), PIPE_SEPARATOR); |
80 | 0 | String key = keyTokenizer.nextToken().trim(); |
81 | 0 | String defaultValue = null; |
82 | 0 | if (keyTokenizer.hasMoreTokens()) { |
83 | 0 | defaultValue = keyTokenizer.nextToken().trim(); |
84 | |
} |
85 | |
|
86 | |
try { |
87 | 0 | buffer.append(injector.getInstance(Key.get(String.class, Names.named(key)))); |
88 | 0 | } catch (Throwable e) { |
89 | 0 | if (defaultValue != null) { |
90 | 0 | buffer.append(defaultValue); |
91 | |
} else { |
92 | 0 | buffer.append(KEY_PREFIX).append(key).append('}'); |
93 | |
} |
94 | 0 | } |
95 | |
|
96 | 0 | prev = endName + 1; |
97 | 0 | } |
98 | |
} |
99 | 0 | if (prev < pattern.length()) { |
100 | 0 | buffer.append(pattern.substring(prev)); |
101 | |
} |
102 | |
|
103 | 0 | return buffer.toString(); |
104 | |
} |
105 | |
|
106 | |
public static void main(String[] args) { |
107 | 0 | Injector injector = Guice.createInjector(new AbstractModule() { |
108 | |
@Override |
109 | |
protected void configure() { |
110 | 0 | bindConstant().annotatedWith(Names.named("variable.1")).to("feuer"); |
111 | 0 | bindConstant().annotatedWith(Names.named("variable.2")).to("frei"); |
112 | 0 | bindConstant().annotatedWith(Names.named("config.soap.protocol")).to("ftp"); |
113 | 0 | bindConstant().annotatedWith(Names.named("config.soap.ip")).to("1.1.1.1"); |
114 | 0 | bindConstant().annotatedWith(Names.named("config.soap.port")).to("9999"); |
115 | 0 | bindConstant().annotatedWith(Names.named("config.soap.app")).to("dynmaic"); |
116 | 0 | bindConstant().annotatedWith(Names.named("config.soap.client")).to("/henkel"); |
117 | 0 | bindConstant().annotatedWith(Names.named("config.soap.stage")).to("test"); |
118 | 0 | } |
119 | |
}); |
120 | |
|
121 | 0 | VariableResolver resolver = injector.getInstance(VariableResolver.class); |
122 | 0 | System.out.println(resolver.resolve("${variable.1} ${variable.2}")); |
123 | 0 | System.out.println(resolver.resolve("\"${variable.3| }\"")); |
124 | 0 | System.out.println(resolver.resolve("${config.soap.protocol|http}://${config.soap.ip|127.0.0.1}:${config.soap.port|12400}/${config.soap.app|configuration}${config.soap.client| }/soap/optional.xml?stage=${config.soap.stage|default}")); |
125 | 0 | } |
126 | |
|
127 | |
} |