10000 Refined Configurator.java. · python012/Solvent@5958ed8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5958ed8

Browse files
committed
Refined Configurator.java.
1 parent c972740 commit 5958ed8

File tree

2 files changed

+177
-67
lines changed

2 files changed

+177
-67
lines changed

src/com/solvent/Configurator.java

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package com.solvent.util;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.util.HashMap;
7+
import java.util.Map.Entry;
8+
import java.util.Properties;
9+
10+
import org.apache.log4j.Logger;
11+
12+
import com.solvent.SolventLogger;
13+
14+
public class Configurator {
15+
public static final String DEFAULT_PROPERTIES_RESOURCE = "/com/solvent/conf/solvent-defaults.properties";
16+
public static final String SOLVENT_PROPERTIES = "solvent.conf";
17+
private static Properties defaults = new Properties();
18+
19+
private HashMap<String, String> confMap = new HashMap<String, String>();
20+
private static Configurator INSTANCE = null;
21+
22+
private static Logger log = SolventLogger.getLogger(Configurator.class);
23+
24+
private static String propFileName = System.getProperty(SOLVENT_PROPERTIES);
25+
26+
static {
27+
System.out.println("Loading defaults......");
28+
try {
29+
defaults.load(Configurator.class.getResourceAsStream(DEFAULT_PROPERTIES_RESOURCE));
30+
} catch (IOException e) {
31+
throw new ConfigurationError(e.getMessage(), e);
32+
}
33+
if (null != propFileName) {
34+
File propFile = new File(propFileName);
35+
log.info("\nusing propFile: " + propFile.getName());
36+
try {
37+
Properties props = new Properties();
38+
props.load(new FileInputStream(propFile));
39+
initialize(props);
40+
} catch (IOException e) {
41+
throw new ConfigurationError("Error while loading configuration file: " + propFile.getAbsolutePath(),
42+
e);
43+
}
44+
}
45+
}
46+
47+
// *************************************************************************
48+
49+
public static final String SOLVENT_OUTPUT_VERBOSITY = "solvent.output.verbosity";
50+
public static final String SOLVENT_OUTPUT_PATTERN = "solvent.output.pattern";
51+
public static final String SOLVENT_OUTPUT_FILENAME = "solvent.output.file";
52+
53+
// *************************************************************************
54+
55+
public static String getSolventOutputVerbosity() {
56+
return getString(SOLVENT_OUTPUT_VERBOSITY);
57+
}
58+
59+
public static String getSolventOutputPattern() {
60+
return getString(SOLVENT_OUTPUT_PATTERN);
61+
}
62+
63+
public static File getSolventOutputFile() {
64+
return getFile(SOLVENT_OUTPUT_FILENAME, null);
65+
}
66+
67+
private Configurator() {
68+
setDefaults();
69+
}
70+
71+
private Configurator(Properties prop) {
72+
this();
73+
String pattern = prop.getProperty(SOLVENT_OUTPUT_PATTERN);
74+
String verbosity = prop.getProperty(SOLVENT_OUTPUT_VERBOSITY);
75+
File logFile = getLogFile(prop.getProperty(SOLVENT_OUTPUT_FILENAME));
76+
if (logFile != null) {
77+
prop.setProperty(SOLVENT_OUTPUT_FILENAME, logFile.getAbsolutePath());
78+
}
79+
SolventLogger.initializeLogging(pattern, verbosity, logFile);
80+
81+
for (Entry<Object, Object> entry : prop.entrySet()) {
82+
String key = (String) entry.getKey();
83+
String value = (String) entry.getValue();
84+
85+
if (value != null && !value.isEmpty()) {
86+
log.info("Loading configuration property: " + key + " = " + value);
87+
confMap.put(key, value);
88+
}
89+
}
90+
}
91+
92+
// *************************************************************************
93+
public static void initialize(Properties prop) {
94+
INSTANCE = new Configurator(prop);
95+
}
96+
97+
private static Configurator getInstance() {
98+
return INSTANCE;
99+
}
100+
101+
private void setDefaults() {
102+
// L10N
103+
setDefault(L10N_LANGUAGE);
104+
// aut.properties
105+
setDefault(APP_URL);
106+
// solvent.log4j.properties (output)
107+
setDefault(SOLVENT_OUTPUT_VERBOSITY);
108+
setDefault(SOLVENT_OUTPUT_PATTERN);
109+
setDefault(SOLVENT_OUTPUT_FILENAME);
110+
}
111+
112+
private void setDefault(String option) {
113+
String value = defaults.getProperty(option);
114+
if (value != null && value.trim().isEmpty()) {
115+
value = null;
116+
}
117+
confMap.put(option, value);
118+
}
119+
120+
private static File getLogFile(String logFileName) {
121+
File logFile = null;
122+
if (logFileName != null && !logFileName.trim().isEmpty()) {
123+
logFile = new File(logFileName);
124+
if (!logFile.isAbsolute()) {
125+
logFile = new File(System.getProperty("user.home", logFileName));
126+
}
127+
}
128+
// System.out.println("\nlogfile path:" + logFile.getAbsolutePath());
129+
log.info("\nlogfile path:" + logFile.getAbsolutePath());
130+
return logFile;
131+
}
132+
133+
private static String getString(String optionName) {
134+
return getInstance().confMap.get(optionName);
135+
}
136+
137+
private static int getIntOption(String optionName) {
138+
try {
139+
String value = getInstance().confMap.get(optionName);
140+
return Integer.parseInt(value);
141+
} catch (NumberFormatException e) {
142+
log.warn("optionName: " + optionName + " cannot be parsed to Integer! 0 is returned.");
143+
return 0;
144+
}
145+
}
146+
147+
private static File getFile(String optionName, String defaultValue) {
148+
String value = getInstance().confMap.get(optionName);
149+
if (null == value) {
150+
return (null == defaultValue) ? null
151+
: new File(new File(System.getProperty("user.home"), ".semet"), defaultValue);
152+
}
153+
log.info("fileName: " + value);
154+
return new File(value);
155+
}
156+
157+
// *************************************************************************
158+
159+
public static final String APP_URL = "app.url";
160+
public static final String SELENIUM_PORT = "selenium.port";
161+
public static final String BROWSER_COMMAND = "browser.command";
162+
public static final String SELENIUM_HOST = "selenium.host";
163+
public static final String WEBDRIVER_NAME = "webdriver.name";
164+
public static final String TESTCASE_TIMEOUT = "solvent.test.timeout";
165+
public static final String L10N_LANGUAGE = "l10n.language";
166+
167+
// *************************************************************************
168+
169+
public static String getAppURL() {
170+
return getString(APP_URL);
171+
}
172+
173+
public static int getTestCaseTimeout() {
174+
return getIntOption(TESTCASE_TIMEOUT) * 60 * 1000;
175+
}
176+
177+
}

0 commit comments

Comments
 (0)
0