Skip to content

Commit 7eb9e4c

Browse files
harawatah3adache
authored andcommitted
Replace variable expression ${} in environment properties file
1 parent fd19186 commit 7eb9e4c

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/main/java/org/apache/ibatis/migration/Environment.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.Map;
2828
import java.util.Properties;
2929

30+
import org.apache.ibatis.parsing.GenericTokenParser;
31+
3032
public class Environment {
3133

3234
public static final String CHANGELOG = "changelog";
@@ -102,6 +104,17 @@ private enum SETTING_KEY {
102104
private final Properties sysProps = System.getProperties();
103105
private final Properties variables = new Properties();
104106

107+
private final GenericTokenParser parser = new GenericTokenParser("${", "}", key -> {
108+
String value = sysProps.getProperty(key);
109+
if (value == null) {
110+
value = envVars.get(key);
111+
}
112+
if (value == null) {
113+
value = "${" + key + "}";
114+
}
115+
return value;
116+
});
117+
105118
public Environment(File file) {
106119
try (FileInputStream inputStream = new FileInputStream(file)) {
107120
Properties prop = new Properties();
@@ -136,7 +149,7 @@ public Environment(File file) {
136149

137150
// User defined variables.
138151
prop.entrySet().stream().filter(e -> !SETTING_KEYS.contains(e.getKey())).forEach(e -> {
139-
variables.put(e.getKey(), e.getValue());
152+
variables.put(e.getKey(), parser.parse((String) e.getValue()));
140153
});
141154
} catch (FileNotFoundException e) {
142155
throw new MigrationException("Environment file missing: " + file.getAbsolutePath());
@@ -162,7 +175,7 @@ protected String readProperty(Properties properties, String propertyKey, String
162175
}
163176
// 3. Read .properties file with variable replacement.
164177
property = properties.getProperty(propertyKey, defaultValue);
165-
return property == null ? null : property;
178+
return property == null ? null : parser.parse(property);
166179
}
167180

168181
public String getTimeZone() {

src/test/java/org/apache/ibatis/migration/system_property/SystemPropertyTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,16 @@ public void checkAssertion() {
5656

5757
// Set system properties
5858
System.setProperty("MIGRATIONS_DRIVER", "org.hsqldb.jdbcDriver");
59+
System.setProperty("username", "Pocahontas");
60+
System.setProperty("var1", "Variable 1");
5961

6062
Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "up", "1", "--trace"));
6163

6264
String output = out.getLog();
6365
assertTrue(output.contains("SUCCESS"));
66+
assertTrue(output.contains("username: Pocahontas"));
67+
assertTrue(output.contains("var1: Variable 1"));
68+
assertTrue(output.contains("var2: ${var2}"));
6469

6570
out.clearLog();
6671
System.exit(0);

src/test/java/org/apache/ibatis/migration/system_property/testdir/environments/development.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
driver=should_be_overwritten_by_system_property
1818
url=jdbc:hsqldb:mem:sysprop
19-
username=Pocahontas
19+
username=${username}
2020
password=
2121

2222
changelog=CHANGELOG
23+
24+
var1=${var1}
25+
var2=${var2}

src/test/java/org/apache/ibatis/migration/system_property/testdir/scripts/001_create_changelog.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ ALTER TABLE ${changelog}
3535
ADD CONSTRAINT PK_${changelog}
3636
PRIMARY KEY (id);
3737

38+
SELECT 'username: ' || USER_NAME FROM INFORMATION_SCHEMA.SYSTEM_USERS;
39+
SELECT 'var1: ' || '${var1}' FROM (VALUES(0));
40+
SELECT 'var2: ' || '${var2}' FROM (VALUES(0));
41+
3842

3943
-- //@UNDO
4044

0 commit comments

Comments
 (0)