|
1 |
| package org.jconfig; |
|
2 |
| |
|
3 |
| import java.io.BufferedReader; |
|
4 |
| import java.io.IOException; |
|
5 |
| import java.io.InputStreamReader; |
|
6 |
| import java.util.HashMap; |
|
7 |
| import java.util.Map; |
|
8 |
| import java.util.Properties; |
|
9 |
| import java.util.Vector; |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| public class VariableManager { |
|
21 |
| |
|
22 |
| private static VariableManager vm = null; |
|
23 |
| private HashMap varMapping; |
|
24 |
| private Properties envVariables; |
|
25 |
| |
|
26 |
| |
|
27 |
| private HashMap includedProperties; |
|
28 |
| |
|
29 |
1
| private VariableManager() {
|
|
30 |
1
| varMapping = new HashMap();
|
|
31 |
1
| includedProperties = new HashMap();
|
|
32 |
| } |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
58
| public static VariableManager getInstance() {
|
|
40 |
58
| if ( vm == null ) {
|
|
41 |
1
| vm = new VariableManager();
|
|
42 |
| } |
|
43 |
58
| return vm;
|
|
44 |
| } |
|
45 |
| |
|
46 |
23
| public HashMap getVariables(String configName) {
|
|
47 |
23
| HashMap vars = (HashMap)varMapping.get(configName);
|
|
48 |
23
| if ( vars == null ) {
|
|
49 |
0
| vars = new HashMap();
|
|
50 |
| } |
|
51 |
23
| Vector includes = (Vector)includedProperties.get(configName);
|
|
52 |
23
| if ( includes != null ) {
|
|
53 |
14
| for ( int i=0;i < includes.size();i++) {
|
|
54 |
37
| String name = (String)includes.get(i);
|
|
55 |
37
| if ( vars.containsKey(name)) {
|
|
56 |
9
| vars.remove(name);
|
|
57 |
| } |
|
58 |
| } |
|
59 |
| } |
|
60 |
23
| return vars;
|
|
61 |
| } |
|
62 |
| |
|
63 |
18
| public String getVariable(String configName,String name) {
|
|
64 |
18
| HashMap vars = (HashMap)varMapping.get(configName);
|
|
65 |
18
| if ( vars != null ) {
|
|
66 |
18
| return (String)vars.get(name);
|
|
67 |
| } |
|
68 |
0
| return null;
|
|
69 |
| } |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
153
| public void addVariable(String varName,String varValue,String configName) {
|
|
78 |
153
| HashMap vars = (HashMap)varMapping.get(configName);
|
|
79 |
153
| if ( vars == null ) {
|
|
80 |
12
| vars = new HashMap();
|
|
81 |
| } |
|
82 |
153
| vars.put(varName,varValue);
|
|
83 |
153
| varMapping.put(configName,vars);
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
1
| public void removeVariable(String varName,String configName) {
|
|
93 |
1
| HashMap vars = (HashMap)varMapping.get(configName);
|
|
94 |
1
| if ( vars != null ) {
|
|
95 |
1
| vars.remove(varName);
|
|
96 |
1
| varMapping.put(configName,vars);
|
|
97 |
| } |
|
98 |
| } |
|
99 |
| |
|
100 |
61
| public void addIncludedVariable(String varName,String varValue,String configName) {
|
|
101 |
61
| addVariable(varName,varValue,configName);
|
|
102 |
61
| Vector includes = (Vector)includedProperties.get(configName);
|
|
103 |
61
| if ( includes == null ) {
|
|
104 |
8
| includes = new Vector();
|
|
105 |
| } |
|
106 |
61
| includes.add(varName);
|
|
107 |
61
| includedProperties.put(configName,includes);
|
|
108 |
| } |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
201
| public String replaceVariables(String text, String configName) {
|
|
122 |
201
| Map vars = (Map) varMapping.get(configName);
|
|
123 |
201
| if (vars != null && vars.size() > 0) {
|
|
124 |
152
| text = replaceVar(text, vars);
|
|
125 |
| } |
|
126 |
201
| text = replaceEnvVar(text);
|
|
127 |
201
| return replaceSystemVar(text);
|
|
128 |
| } |
|
129 |
174
| private String replaceVar(String text, Map vars) {
|
|
130 |
174
| if (text == null) {
|
|
131 |
2
| return text;
|
|
132 |
| } |
|
133 |
| |
|
134 |
172
| int startPosition = text.indexOf("${");
|
|
135 |
172
| if (startPosition == -1) {
|
|
136 |
149
| return text;
|
|
137 |
| } |
|
138 |
| |
|
139 |
23
| StringBuffer buffer = new StringBuffer();
|
|
140 |
23
| int textLength = text.length();
|
|
141 |
23
| int lastIndex = 0;
|
|
142 |
| |
|
143 |
| |
|
144 |
23
| while (startPosition > -1 && (startPosition+2) < textLength) {
|
|
145 |
25
| int endPosition = text.indexOf("}", startPosition + 2);
|
|
146 |
25
| if (endPosition == -1) {
|
|
147 |
| |
|
148 |
0
| break;
|
|
149 |
| } |
|
150 |
| |
|
151 |
| |
|
152 |
25
| buffer.append(text.substring(lastIndex, startPosition));
|
|
153 |
| |
|
154 |
| |
|
155 |
25
| String currentKey = text.substring(startPosition + 2, endPosition);
|
|
156 |
25
| String value = (String) vars.get(currentKey);
|
|
157 |
25
| if (value != null) {
|
|
158 |
| |
|
159 |
22
| value = replaceVar(value, vars);
|
|
160 |
| |
|
161 |
| |
|
162 |
22
| buffer.append(value);
|
|
163 |
22
| lastIndex = endPosition + 1;
|
|
164 |
| } else { |
|
165 |
| |
|
166 |
| |
|
167 |
3
| buffer.append(text.charAt(startPosition));
|
|
168 |
3
| buffer.append(text.charAt(startPosition+1));
|
|
169 |
3
| lastIndex = startPosition + 2;
|
|
170 |
| } |
|
171 |
| |
|
172 |
25
| startPosition = text.indexOf("${", lastIndex);
|
|
173 |
| } |
|
174 |
| |
|
175 |
23
| buffer.append(text.substring(lastIndex, textLength));
|
|
176 |
23
| return buffer.toString();
|
|
177 |
| } |
|
178 |
| |
|
179 |
204
| protected String replaceEnvVar(String text) {
|
|
180 |
204
| if ( text != null ) {
|
|
181 |
201
| if ( envVariables == null && text.indexOf("${env:") != -1 ) {
|
|
182 |
1
| envVariables = getEnvVars();
|
|
183 |
| } |
|
184 |
201
| int idx = text.indexOf("${env:");
|
|
185 |
| |
|
186 |
201
| while ( idx != -1 ) {
|
|
187 |
6
| String firstPart = text.substring(0, idx);
|
|
188 |
6
| String tmp = text.substring(idx+6);
|
|
189 |
6
| String envVar = tmp.substring(0,tmp.indexOf("}"));
|
|
190 |
6
| String secondPart = text.substring(idx + envVar.length()+7);
|
|
191 |
6
| String value = envVariables.getProperty(envVar);
|
|
192 |
6
| if ( value == null ) {
|
|
193 |
1
| value = "${env:"+envVar+"}";
|
|
194 |
| } |
|
195 |
6
| text = firstPart + value + secondPart;
|
|
196 |
6
| idx = text.indexOf("${env:",idx+1);
|
|
197 |
| } |
|
198 |
| } |
|
199 |
204
| return text;
|
|
200 |
| } |
|
201 |
| |
|
202 |
205
| protected String replaceSystemVar(String text) {
|
|
203 |
205
| if ( text != null ) {
|
|
204 |
202
| int idx = text.indexOf("${system:");
|
|
205 |
| |
|
206 |
202
| while ( idx != -1 ) {
|
|
207 |
6
| String firstPart = text.substring(0, idx);
|
|
208 |
6
| String tmp = text.substring(idx+9);
|
|
209 |
6
| String sysVar = tmp.substring(0,tmp.indexOf("}"));
|
|
210 |
6
| String secondPart = text.substring(idx + sysVar.length()+10);
|
|
211 |
6
| String value = System.getProperty(sysVar);
|
|
212 |
6
| if ( value == null ) {
|
|
213 |
1
| value = "${system:"+sysVar+"}";
|
|
214 |
| } |
|
215 |
6
| text = firstPart + value + secondPart;
|
|
216 |
6
| idx = text.indexOf("${system:",idx+1);
|
|
217 |
| } |
|
218 |
| } |
|
219 |
205
| return text;
|
|
220 |
| } |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
1
| private Properties getEnvVars() {
|
|
225 |
1
| Process p = null;
|
|
226 |
1
| Properties envVars = new Properties();
|
|
227 |
1
| BufferedReader br = null;
|
|
228 |
1
| try {
|
|
229 |
1
| Runtime r = Runtime.getRuntime();
|
|
230 |
1
| String OS = System.getProperty("os.name").toLowerCase();
|
|
231 |
1
| if (OS.indexOf("windows 9") > -1) {
|
|
232 |
0
| p = r.exec( "command.com /c set" );
|
|
233 |
| } |
|
234 |
1
| else if ( (OS.indexOf("nt") > -1)
|
|
235 |
| || (OS.indexOf("windows 2000") > -1 |
|
236 |
| || (OS.indexOf("windows xp") > -1) ) ) { |
|
237 |
| |
|
238 |
1
| p = r.exec( "cmd.exe /c set" );
|
|
239 |
| } |
|
240 |
| else { |
|
241 |
| |
|
242 |
0
| p = r.exec( "env" );
|
|
243 |
| } |
|
244 |
1
| br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
|
|
245 |
1
| String line;
|
|
246 |
?
| while( (line = br.readLine()) != null ) {
|
|
247 |
36
| int idx = line.indexOf( '=' );
|
|
248 |
36
| String key = line.substring( 0, idx );
|
|
249 |
36
| String value = line.substring( idx+1 );
|
|
250 |
36
| envVars.setProperty( key, value );
|
|
251 |
| } |
|
252 |
1
| br.close();
|
|
253 |
| } |
|
254 |
| catch (Exception e) { |
|
255 |
| |
|
256 |
0
| try {
|
|
257 |
0
| if ( br != null ) {
|
|
258 |
0
| br.close();
|
|
259 |
| } |
|
260 |
| } catch (IOException e1) { |
|
261 |
| } |
|
262 |
| } |
|
263 |
1
| return envVars;
|
|
264 |
| } |
|
265 |
| } |