-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
CustomChangeWrapper.java
341 lines (302 loc) · 13.3 KB
/
CustomChangeWrapper.java
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
package liquibase.change.custom;
import liquibase.Scope;
import liquibase.change.AbstractChange;
import liquibase.change.ChangeMetaData;
import liquibase.change.DatabaseChange;
import liquibase.change.DatabaseChangeProperty;
import liquibase.database.Database;
import liquibase.exception.*;
import liquibase.parser.core.ParsedNode;
import liquibase.parser.core.ParsedNodeException;
import liquibase.resource.ResourceAccessor;
import liquibase.statement.SqlStatement;
import liquibase.util.ObjectUtil;
import java.util.*;
import liquibase.util.OsgiUtil;
/**
* Adapts CustomChange implementations to the standard change system used by Liquibase.
* Custom change implementations should implement CustomSqlChange or CustomTaskChange
*
* @see liquibase.change.custom.CustomSqlChange
* @see liquibase.change.custom.CustomTaskChange
*/
@DatabaseChange(name="customChange",
description = "Although Liquibase tries to provide a wide range of database refactorings, there are times you may" +
" want to create your own custom refactoring class.\n" +
"\n" +
"To create your own custom refactoring, simply create a class that implements the liquibase.change.custom.CustomSqlChange " +
"or liquibase.change.custom.CustomTaskChange interface and use the <custom> tag in your change set.\n" +
"\n" +
"If your change can be rolled back, implement the liquibase.change.custom.CustomSqlRollback interface as well.\n" +
"\n" +
"For a sample custom change class, see liquibase.change.custom.ExampleCustomSqlChange",
priority = ChangeMetaData.PRIORITY_DEFAULT)
public class CustomChangeWrapper extends AbstractChange {
/**
* Non-private access only for testing.
*/
CustomChange customChange;
private String className;
private SortedSet<String> params = new TreeSet<>();
private Map<String, String> paramValues = new LinkedHashMap<>();
private boolean configured;
@Override
public boolean generateStatementsVolatile(Database database) {
return true;
}
/**
* Return the CustomChange instance created by the call to {@link #setClass(String)}.
*/
@DatabaseChangeProperty(isChangeProperty = false)
public CustomChange getCustomChange() {
return customChange;
}
/**
* Specify the name of the class to use as the CustomChange and assigns it to {@link #getCustomChange()}.
*/
public CustomChangeWrapper setClass(String className) throws CustomChangeException {
if (className == null) {
return this;
}
this.className = className;
try {
Boolean osgiPlatform = Scope.getCurrentScope().get(Scope.Attr.osgiPlatform, Boolean.class);
if (Boolean.TRUE.equals(osgiPlatform)) {
customChange = (CustomChange)OsgiUtil.loadClass(className).getConstructor().newInstance();
} else {
try {
customChange = (CustomChange) Class.forName(className, true, Scope.getCurrentScope().getClassLoader()).getConstructor().newInstance();
} catch (ClassCastException e) { //fails in Ant in particular
try {
customChange = (CustomChange) Thread.currentThread().getContextClassLoader().loadClass(className).getConstructor().newInstance();
} catch (ClassNotFoundException e1) {
customChange = (CustomChange) Class.forName(className).getConstructor().newInstance();
}
}
}
} catch (Exception e) {
throw new CustomChangeException(e);
}
return this;
}
/**
* Returns the name of the custom class set in {@link #setClass(String)}
*/
@DatabaseChangeProperty(description = "Name class that implements the custom change.")
public String getClassName() {
return className;
}
/**
* Specify a parameter on the CustomChange object to set before executing {@link liquibase.change.Change#generateStatements(liquibase.database.Database)} or {@link #generateRollbackStatements(liquibase.database.Database)} on it.
* The CustomChange class must have a set method for the given parameter. For example, to call setParam("lastName", "X") you must have a method setLastName(String val) on your class.
*/
public void setParam(String name, String value) {
this.params.add(name);
this.paramValues.put(name, value);
}
/**
* Returns the parameters set by {@link #setParam(String, String)}. If no parameters are set, an empty set will be returned
*/
@DatabaseChangeProperty(isChangeProperty = false)
public SortedSet<String> getParams() {
return Collections.unmodifiableSortedSet(params);
}
/**
* Get the value of a parameter set by {@link #setParam(String, String)}. If the parameter was not set, null will be returned.
*/
public String getParamValue(String key) {
return paramValues.get(key);
}
/**
* Call the {@link CustomChange#validate(liquibase.database.Database)} method and return the result.
*/
@Override
public ValidationErrors validate(Database database) {
if (!configured) {
try {
configureCustomChange();
} catch (CustomChangeException e) {
throw new UnexpectedLiquibaseException(e);
}
}
try {
return customChange.validate(database);
} catch (Exception e) {
return new ValidationErrors().addError("Exception thrown calling "+getClassName()+".validate():"+ e.getMessage());
}
}
/**
* Required for the Change interface, but not supported by CustomChanges. Returns an empty Warnings object.
*/
@Override
public Warnings warn(Database database) {
//does not support warns
return new Warnings();
}
/**
* Finishes configuring the CustomChange based on the values passed to {@link #setParam(String, String)} then calls {@link CustomSqlChange#generateStatements(liquibase.database.Database)}
* or {@link CustomTaskChange#execute(liquibase.database.Database)} depending on the CustomChange implementation.
* <p></p>
* If the CustomChange returns a null SqlStatement array, this method returns an empty array. If a CustomTaskChange is being used, this method will return an empty array.
*/
@Override
public SqlStatement[] generateStatements(Database database) {
SqlStatement[] statements = null;
try {
configureCustomChange();
if (customChange instanceof CustomSqlChange) {
statements = ((CustomSqlChange) customChange).generateStatements(database);
} else if (customChange instanceof CustomTaskChange) {
((CustomTaskChange) customChange).execute(database);
} else {
throw new UnexpectedLiquibaseException(customChange.getClass().getName() + " does not implement " + CustomSqlChange.class.getName() + " or " + CustomTaskChange.class.getName());
}
} catch (CustomChangeException e) {
throw new UnexpectedLiquibaseException(e);
}
if (statements == null) {
statements = new SqlStatement[0];
}
return statements;
}
/**
* Finishes configuring the CustomChange based on the values passed to {@link #setParam(String, String)} then calls {@link CustomSqlRollback#generateRollbackStatements(liquibase.database.Database)}
* or {@link CustomTaskRollback#rollback(liquibase.database.Database)} depending on the CustomChange implementation.
* <p></p>
* If the CustomChange returns a null SqlStatement array, this method returns an empty array. If a CustomTaskChange is being used, this method will return an empty array.
* Any {@link RollbackImpossibleException} exceptions thrown by the CustomChange will thrown by this method.
*/
@Override
public SqlStatement[] generateRollbackStatements(Database database) throws RollbackImpossibleException {
SqlStatement[] statements = null;
try {
configureCustomChange();
if (customChange instanceof CustomSqlRollback) {
statements = ((CustomSqlRollback) customChange).generateRollbackStatements(database);
} else if (customChange instanceof CustomTaskRollback) {
((CustomTaskRollback) customChange).rollback(database);
} else {
throw new RollbackImpossibleException("Unknown rollback type: "+customChange.getClass().getName());
}
} catch (CustomChangeException e) {
throw new UnexpectedLiquibaseException(e);
}
if (statements == null) {
statements = new SqlStatement[0];
}
return statements;
}
/**
* Returns true if the customChange supports rolling back.
* {@link #generateRollbackStatements} may still trow a {@link RollbackImpossibleException} when it is actually exectued, even if this method returns true.
* Currently only checks if the customChange implements {@link CustomSqlRollback}
*/
@Override
public boolean supportsRollback(Database database) {
return (customChange instanceof CustomSqlRollback) || (customChange instanceof CustomTaskRollback);
}
/**
* Return the customChange's {@link CustomChange#getConfirmationMessage} message as the Change's message.
*/
@Override
public String getConfirmationMessage() {
try {
configureCustomChange();
} catch (CustomChangeException e) {
throw new UnexpectedLiquibaseException(e);
}
return customChange.getConfirmationMessage();
}
private void configureCustomChange() throws CustomChangeException {
if (configured) {
return;
}
try {
for (String param : params) {
ObjectUtil.setProperty(customChange, param, paramValues.get(param));
}
customChange.setFileOpener(Scope.getCurrentScope().getResourceAccessor());
customChange.setUp();
configured = true;
} catch (Exception e) {
throw new CustomChangeException(e);
}
}
@Override
public SerializationType getSerializableFieldType(String field) {
switch (field) {
case "class":
return SerializationType.NAMED_FIELD;
case "param":
return SerializationType.NAMED_FIELD;
default:
throw new UnexpectedLiquibaseException("Unexpected CustomChangeWrapper field " + field);
}
}
@Override
public Object getSerializableFieldValue(String field) {
switch (field) {
case "class":
return getClassName();
case "param":
return this.paramValues;
default:
throw new UnexpectedLiquibaseException("Unexpected CustomChangeWrapper field " + field);
}
}
@Override
public Set<String> getSerializableFields() {
return new HashSet<>(Arrays.asList("class", "param"));
}
@Override
public String getSerializedObjectNamespace() {
return STANDARD_CHANGELOG_NAMESPACE;
}
@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
try {
setClass(parsedNode.getChildValue(null, "class", String.class));
} catch (CustomChangeException e) {
throw new ParsedNodeException(e);
}
super.load(parsedNode, resourceAccessor);
}
@Override
public void customLoadLogic(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
ParsedNode paramsNode = parsedNode.getChild(null, "params");
if (paramsNode == null) {
paramsNode = parsedNode;
}
for (ParsedNode child : paramsNode.getChildren(null, "param")) {
Object value = child.getValue();
if (value == null) {
value = child.getChildValue(null, "value");
}
if (value != null) {
value = value.toString();
}
String paramName = child.getChildValue(null, "name", String.class);
if (paramName == null) {
throw new ParsedNodeException("Custom change param " + child + " does not have a 'name' attribute");
}
this.setParam(paramName, (String) value);
}
CustomChange customChange = null;
try {
Boolean osgiPlatform = Scope.getCurrentScope().get(Scope.Attr.osgiPlatform, Boolean.class);
if (Boolean.TRUE.equals(osgiPlatform)) {
customChange = (CustomChange)OsgiUtil.loadClass(className).getConstructor().newInstance();
} else {
customChange = (CustomChange) Class.forName(className, false, Scope.getCurrentScope().getClassLoader()).getConstructor().newInstance();
}
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
for (ParsedNode node : parsedNode.getChildren()) {
Object value = node.getValue();
if ((value != null) && ObjectUtil.hasProperty(customChange, node.getName())) {
this.setParam(node.getName(), value.toString());
}
}
}
}