-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.java
More file actions
executable file
·475 lines (397 loc) · 12.1 KB
/
Configuration.java
File metadata and controls
executable file
·475 lines (397 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package com.voxeo.tropo;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletConfig;
import org.apache.log4j.Logger;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import com.micromethod.common.util.CodeUtils;
import com.micromethod.common.util.NetworkUtils;
import com.micromethod.common.util.annotation.StringPart;
import com.voxeo.tropo.app.ApplicationManager;
import com.voxeo.tropo.app.MockAppMgr;
import com.voxeo.tropo.util.ConfigurationItem;
public class Configuration {
public static final Logger LOG = Logger.getLogger(Configuration.class);
private static Configuration INSTANCE = null;
private static String LOCAL_ADDRESS = NetworkUtils.getLocalAddress().getHostAddress();
@StringPart
private String _mediaServer = "127.0.0.1";
@StringPart
private int _mediaServerPort = 9974;
@StringPart
@ConfigurationItem
private int maxTimeSpeak = 300000;
@StringPart
@ConfigurationItem
private int maxTimeListen = 60000;
@StringPart
@ConfigurationItem
private String termTimeout = "0";
@StringPart
@ConfigurationItem
private String termChar = "#";
@StringPart
@ConfigurationItem
private String noInputTimeout = "10000";
@StringPart
@ConfigurationItem
private String confidenceLevel = "0.3";
@StringPart
@ConfigurationItem
private String sensitivity = "0.5";
@StringPart
@ConfigurationItem
private String speedVsAccuracy = "0.5";
@StringPart
@ConfigurationItem
private String asrSpeechLanguage = "en-us-prophecy";
@StringPart
@ConfigurationItem
private String ttsSpeechLanguage = "en-US";
@StringPart
@ConfigurationItem
private boolean killOnBargeIn = true;
@StringPart
@ConfigurationItem
private String beepURL = "http://127.0.0.1:8080/beep.wav";
@StringPart
@ConfigurationItem
private String phoneSBC = "sbc-staging-internal.orl.voxeo.net";
@StringPart
@ConfigurationItem
private boolean parseSpeechText = true;
@StringPart
@ConfigurationItem
private boolean hackSDP = true;
@StringPart
@ConfigurationItem
private String mediaAddress = null;
@StringPart
private Map<String, String> _scriptHeaders = new HashMap<String, String>();
@StringPart
private Map<String, Integer> _enginePoolSizes = new HashMap<String, Integer>();
@StringPart
private Class<? extends ApplicationManager> _appManager = MockAppMgr.class;
@StringPart
private Map<String, String> _appManagerParas = new HashMap<String, String>();
@StringPart
@ConfigurationItem
private boolean enableSecurityManager = true;
@StringPart
@ConfigurationItem
private long appMonitorTime = 60000;
@StringPart
@ConfigurationItem
private int threadSize = 400;
@StringPart
private Map<String, Set<String>> sandboxAllow = new HashMap<String, Set<String>>();
@StringPart
private Map<String, Set<String>> sandboxForbid = new HashMap<String, Set<String>>();
public static Configuration get() {
if (INSTANCE == null) {
throw new IllegalArgumentException("Tropo configuratrion has not been initialized.");
}
return INSTANCE;
}
public static void init(final ServletConfig config) throws JDOMException, IOException, ClassNotFoundException {
String xml = config.getInitParameter("configuration");
if (xml == null) {
xml = "/tropo.xml";
}
final InputStream i = Configuration.class.getResourceAsStream(xml);
if (i == null) {
throw new IOException("Unable to find configuration XML: " + xml);
}
try {
INSTANCE = new Configuration(i);
}
finally {
i.close();
}
}
@Override
public String toString() {
return CodeUtils.toStringByAnnotation(this);
}
@SuppressWarnings("unchecked")
private Configuration(final InputStream i) throws JDOMException, IOException, ClassNotFoundException {
_scriptHeaders.put("js", "com/voxeo/tropo/javascript/tropo.js");
_scriptHeaders.put("groovy", "com/voxeo/tropo/groovy/tropo.groovy");
_scriptHeaders.put("jython", "com/voxeo/tropo/jython/tropo.jy");
_scriptHeaders.put("php", "com/voxeo/tropo/php/tropo.php");
_scriptHeaders.put("jruby", "com/voxeo/tropo/jruby/tropo.rb");
final SAXBuilder b = new SAXBuilder();
b.setValidation(false);
final Element f = b.build(i).getRootElement();
enableSecurityManager = Boolean.parseBoolean(f.getAttributeValue("enableSecurityManager"));
final String threads = f.getAttributeValue("threadSize");
if (threads != null && threads.length() > 0) {
threadSize = Integer.parseInt(threads);
}
// security manager
if (enableSecurityManager) {
Element e = f.getChild("sandbox");
if (e != null) {
parseSandbox(e);
}
}
// media server configurations
final Element m = f.getChild("mediaServer");
if (m != null) {
if (m.getAttributeValue("host") != null) {
_mediaServer = m.getAttributeValue("host");
}
if (m.getAttributeValue("port") != null) {
_mediaServerPort = Integer.parseInt(m.getAttributeValue("port"));
}
quickSetValue(this, m);
}
if (mediaAddress == null || mediaAddress.length() == 0) {
if (_mediaServer.equals("127.0.0.1")) {
mediaAddress = LOCAL_ADDRESS;
}
else {
mediaAddress = _mediaServer;
}
}
// app manager configurations
final Element a = f.getChild("appManager");
if (a != null) {
_appManager = (Class<? extends ApplicationManager>) this.getClass().getClassLoader().loadClass(
a.getAttributeValue("class"));
final List<Element> ls = a.getChildren("para");
for (final Element p : ls) {
_appManagerParas.put(p.getAttributeValue("name"), p.getAttributeValue("value"));
}
}
// scripts configurations
final Element s = f.getChild("scripts");
if (s != null) {
final List<Element> ls = s.getChildren("script");
for (final Element p : ls) {
String type = p.getAttributeValue("type");
_scriptHeaders.put(type, p.getAttributeValue("file"));
String size = p.getAttributeValue("enginePoolSize");
if (size != null && size.length() > 0) {
_enginePoolSizes.put(type, Integer.valueOf(size));
}
else {
_enginePoolSizes.put(type, Integer.valueOf(20));
}
}
}
// debug loggging
if (LOG.isDebugEnabled()) {
LOG.debug("Loaded config " + toString());
}
}
/**
* @param _mediaServer
* the _mediaServer to set
*/
public void setMediaServer(final String mediaServer) {
this._mediaServer = mediaServer;
}
/**
* @return the _mediaServer
*/
public String getMediaServer() {
return _mediaServer;
}
/**
* @param _mediaServerPort
* the _mediaServerPort to set
*/
public void setMediaServerPort(final int mediaServerPort) {
this._mediaServerPort = mediaServerPort;
}
/**
* @return the _mediaServerPort
*/
public int getMediaServerPort() {
return _mediaServerPort;
}
/**
* @param _appManager
* the _appManager to set
*/
public void setAppManager(final Class<? extends ApplicationManager> appManager) {
this._appManager = appManager;
}
/**
* @return the _appManager
*/
public Class<? extends ApplicationManager> getAppManager() {
return _appManager;
}
/**
* @param _appManagerParas
* the _appManagerParas to set
*/
public void setAppManagerParas(final Map<String, String> appManagerParas) {
this._appManagerParas = appManagerParas;
}
/**
* @return the _appManagerParas
*/
public Map<String, String> getAppManagerParas() {
return _appManagerParas;
}
public String getScriptHeader(final String type) {
return _scriptHeaders.get(type);
}
public Map<String, Integer> getEnginePoolSizes() {
return _enginePoolSizes;
}
public int getMaxTimeSpeak() {
return maxTimeSpeak;
}
public int getMaxTimeListen() {
return maxTimeListen;
}
public String getTermTimeout() {
return termTimeout;
}
public String getTermChar() {
return termChar;
}
public String getNoInputTimeout() {
return noInputTimeout;
}
public String getConfidenceLevel() {
return confidenceLevel;
}
public String getSensitivity() {
return sensitivity;
}
public String getSpeedVsAccuracy() {
return speedVsAccuracy;
}
public String getAsrSpeechLanguage() {
return asrSpeechLanguage;
}
public String getTtsSpeechLanguage() {
return ttsSpeechLanguage;
}
public boolean isKillOnBargeIn() {
return killOnBargeIn;
}
public String getBeepURL() {
return beepURL;
}
public String getPhoneSBC() {
return phoneSBC;
}
public boolean isParseSpeechText() {
return parseSpeechText;
}
public boolean getHackSDP() {
return hackSDP;
}
public String getMediaAddress() {
return mediaAddress;
}
public String getLocalAddress() {
return LOCAL_ADDRESS;
}
public long getApplicationMonitorTime() {
return appMonitorTime;
}
public void setApplicationMonitorTime(final long time) {
appMonitorTime = time;
}
public int getThreadSize() {
return threadSize;
}
private static void quickSetValue(final Object o, final Element m) {
for (final Field field : o.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(ConfigurationItem.class)) {
final String name = field.getName();
String value = m.getChildText(name);
if (value != null) {
value = value.trim();
final boolean accessibility = field.isAccessible();
try {
field.setAccessible(true);
final Class<?> type = field.getType();
if (type == int.class) {
field.set(o, Integer.parseInt(value));
}
else if (type == long.class) {
field.set(o, Long.parseLong(value));
}
else if (type == boolean.class) {
field.set(o, Boolean.parseBoolean(value));
}
else {
field.set(o, value);
}
}
catch (final Throwable t) {
LOG.error("", t);
}
finally {
field.setAccessible(accessibility);
}
}
}
}
}
/**
* @return the enableSecurityManager
*/
public boolean isEnableSecurityManager() {
return enableSecurityManager;
}
@SuppressWarnings("unchecked")
public void parseSandbox(Element s){
List<Element> as = s.getChildren("allow");
for(Element a:as){
List<Element> is = a.getChildren("item");
for(Element i:is){
String action = i.getAttributeValue("action");
String target = i.getTextNormalize().toLowerCase();
Set t = sandboxAllow.get(action);
if(t==null){
t=new HashSet();
sandboxAllow.put(action, t);
}
t.add(target);
}
}
as = s.getChildren("forbid");
for(Element a:as){
List<Element> is = a.getChildren("item");
for(Element i:is){
String action = i.getAttributeValue("action");
String target = i.getTextNormalize().toLowerCase();
Set t = sandboxForbid.get(action);
if(t==null){
t=new HashSet();
sandboxForbid.put(action, t);
}
t.add(target);
}
}
}
/**
* @return the sandboxForbid
*/
Map<String, Set<String>> getSandboxForbid() {
return sandboxForbid;
}
/**
* @return the sandboxAllow
*/
Map<String, Set<String>> getSandboxAllow() {
return sandboxAllow;
}
}