| 1 | |
package liquibase.changelog; |
| 2 | |
|
| 3 | |
import liquibase.util.StringUtils; |
| 4 | |
import liquibase.database.Database; |
| 5 | |
|
| 6 | |
import java.util.List; |
| 7 | |
import java.util.ArrayList; |
| 8 | |
import java.util.Map; |
| 9 | |
import java.util.Collection; |
| 10 | |
import java.util.regex.Pattern; |
| 11 | |
import java.util.regex.Matcher; |
| 12 | |
|
| 13 | 9 | public class ChangeLogParameters { |
| 14 | |
|
| 15 | 25 | private List<ChangeLogParameter> changeLogParameters = new ArrayList<ChangeLogParameter>(); |
| 16 | |
private ExpressionExpander expressionExpander; |
| 17 | |
private Database currentDatabase; |
| 18 | |
private List<String> currentContexts; |
| 19 | |
|
| 20 | |
public ChangeLogParameters() { |
| 21 | 20 | this(null); |
| 22 | 20 | } |
| 23 | |
|
| 24 | 25 | public ChangeLogParameters(Database currentDatabase) { |
| 25 | 25 | for (Map.Entry entry : System.getProperties().entrySet()) { |
| 26 | 1350 | changeLogParameters.add(new ChangeLogParameter(entry.getKey().toString(), entry.getValue())); |
| 27 | |
} |
| 28 | 25 | this.expressionExpander = new ExpressionExpander(this); |
| 29 | 25 | this.currentDatabase = currentDatabase; |
| 30 | 25 | this.currentContexts = new ArrayList<String>(); |
| 31 | 25 | } |
| 32 | |
|
| 33 | |
public void addContext(String context) { |
| 34 | 0 | this.currentContexts.add(context); |
| 35 | 0 | } |
| 36 | |
|
| 37 | |
public void setContexts(Collection<String> contexts) { |
| 38 | 2 | this.currentContexts = new ArrayList<String>(); |
| 39 | 2 | if (contexts != null) { |
| 40 | 2 | this.currentContexts.addAll(contexts); |
| 41 | |
} |
| 42 | 2 | } |
| 43 | |
|
| 44 | |
public void set(String paramter, Object value) { |
| 45 | 6 | changeLogParameters.add(new ChangeLogParameter(paramter, value)); |
| 46 | 6 | } |
| 47 | |
|
| 48 | |
public void set(String key, String value, String contexts, String databases) { |
| 49 | 4 | changeLogParameters.add(new ChangeLogParameter(key, value, contexts, databases)); |
| 50 | 4 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public Object getValue(String key) { |
| 61 | 11 | ChangeLogParameter parameter = findParameter(key); |
| 62 | 11 | return parameter != null ? parameter.getValue() : null; |
| 63 | |
} |
| 64 | |
|
| 65 | |
private ChangeLogParameter findParameter(String key) { |
| 66 | 11 | for (ChangeLogParameter param : changeLogParameters) { |
| 67 | 587 | if (param.getKey().equalsIgnoreCase(key) && param.isValid()) { |
| 68 | 8 | return param; |
| 69 | |
} |
| 70 | |
} |
| 71 | 3 | return null; |
| 72 | |
} |
| 73 | |
|
| 74 | |
public boolean hasValue(String key) { |
| 75 | 0 | return findParameter(key) != null; |
| 76 | |
} |
| 77 | |
|
| 78 | |
public String expandExpressions(String string) { |
| 79 | 558 | return expressionExpander.expandExpressions(string); |
| 80 | |
} |
| 81 | |
|
| 82 | |
private class ChangeLogParameter { |
| 83 | |
private String key; |
| 84 | |
private Object value; |
| 85 | |
private List<String> validContexts; |
| 86 | |
private List<String> validDatabases; |
| 87 | |
|
| 88 | 1356 | public ChangeLogParameter(String key, Object value) { |
| 89 | 1356 | this.key = key; |
| 90 | 1356 | this.value = value; |
| 91 | 1356 | } |
| 92 | |
|
| 93 | |
public ChangeLogParameter(String key, Object value, String validContexts, String validDatabases) { |
| 94 | 4 | this(key, value, StringUtils.splitAndTrim(validContexts, ","), StringUtils |
| 95 | |
.splitAndTrim(validDatabases, ",")); |
| 96 | 4 | } |
| 97 | |
|
| 98 | 4 | public ChangeLogParameter(String key, Object value, List<String> validContexts, List<String> validDatabases) { |
| 99 | 4 | this.key = key; |
| 100 | 4 | this.value = value; |
| 101 | 4 | this.validContexts = validContexts; |
| 102 | 4 | this.validDatabases = validDatabases; |
| 103 | 4 | } |
| 104 | |
|
| 105 | |
public String getKey() { |
| 106 | 587 | return key; |
| 107 | |
} |
| 108 | |
|
| 109 | |
public Object getValue() { |
| 110 | 8 | return value; |
| 111 | |
} |
| 112 | |
|
| 113 | |
public List<String> getValidDatabases() { |
| 114 | 0 | return validDatabases; |
| 115 | |
} |
| 116 | |
|
| 117 | |
public List<String> getValidContexts() { |
| 118 | 0 | return validContexts; |
| 119 | |
} |
| 120 | |
|
| 121 | |
@Override |
| 122 | |
public String toString() { |
| 123 | 0 | return getValue().toString(); |
| 124 | |
} |
| 125 | |
|
| 126 | |
public boolean isValid() { |
| 127 | 10 | boolean isValid = true; |
| 128 | 10 | if (validContexts != null && validContexts.size() > 0) { |
| 129 | 2 | if (ChangeLogParameters.this.currentContexts != null |
| 130 | |
&& ChangeLogParameters.this.currentContexts.size() > 0) { |
| 131 | 2 | isValid = false; |
| 132 | 2 | for (String currentContext : ChangeLogParameters.this.currentContexts) { |
| 133 | 2 | if (validContexts.contains(currentContext)) { |
| 134 | 1 | isValid = true; |
| 135 | |
} |
| 136 | |
} |
| 137 | |
} |
| 138 | |
} |
| 139 | |
|
| 140 | 10 | if (isValid && validDatabases != null && validDatabases.size() > 0) { |
| 141 | 3 | isValid = validDatabases.contains(currentDatabase.getTypeName()); |
| 142 | |
} |
| 143 | |
|
| 144 | 10 | return isValid; |
| 145 | |
} |
| 146 | |
} |
| 147 | |
|
| 148 | |
protected static class ExpressionExpander { |
| 149 | |
private ChangeLogParameters changeLogParameters; |
| 150 | |
|
| 151 | 31 | public ExpressionExpander(ChangeLogParameters changeLogParameters) { |
| 152 | 31 | this.changeLogParameters = changeLogParameters; |
| 153 | 31 | } |
| 154 | |
|
| 155 | |
public String expandExpressions(String text) { |
| 156 | 565 | if (text == null) { |
| 157 | 287 | return null; |
| 158 | |
} |
| 159 | 278 | Pattern expressionPattern = Pattern.compile("(\\$\\{[^\\}]+\\})"); |
| 160 | 278 | Matcher matcher = expressionPattern.matcher(text); |
| 161 | 278 | String originalText = text; |
| 162 | 283 | while (matcher.find()) { |
| 163 | 5 | String expressionString = originalText.substring(matcher.start(), matcher.end()); |
| 164 | 5 | String valueTolookup = expressionString.replaceFirst("\\$\\{", "").replaceFirst("\\}$", ""); |
| 165 | |
|
| 166 | 5 | int dotIndex = valueTolookup.indexOf('.'); |
| 167 | 5 | Object value = changeLogParameters.getValue(valueTolookup); |
| 168 | |
|
| 169 | 5 | if (value != null) { |
| 170 | 3 | text = text.replace(expressionString, value.toString()); |
| 171 | |
} |
| 172 | 5 | } |
| 173 | 278 | return text; |
| 174 | |
} |
| 175 | |
} |
| 176 | |
} |