1 | |
package liquibase.precondition; |
2 | |
|
3 | |
import java.util.HashMap; |
4 | |
import java.util.Map; |
5 | |
import java.util.SortedSet; |
6 | |
import java.util.TreeSet; |
7 | |
|
8 | |
import liquibase.changelog.ChangeSet; |
9 | |
import liquibase.changelog.DatabaseChangeLog; |
10 | |
import liquibase.database.Database; |
11 | |
import liquibase.exception.CustomPreconditionErrorException; |
12 | |
import liquibase.exception.CustomPreconditionFailedException; |
13 | |
import liquibase.exception.PreconditionErrorException; |
14 | |
import liquibase.exception.PreconditionFailedException; |
15 | |
import liquibase.exception.ValidationErrors; |
16 | |
import liquibase.exception.Warnings; |
17 | |
import liquibase.precondition.core.ErrorPrecondition; |
18 | |
import liquibase.precondition.core.FailedPrecondition; |
19 | |
import liquibase.util.ObjectUtil; |
20 | |
|
21 | 8 | public class CustomPreconditionWrapper implements Precondition { |
22 | |
|
23 | |
private String className; |
24 | |
private ClassLoader classLoader; |
25 | |
|
26 | 8 | private SortedSet<String> params = new TreeSet<String>(); |
27 | 8 | private Map<String, String> paramValues = new HashMap<String, String>(); |
28 | |
|
29 | |
public String getClassName() { |
30 | 0 | return className; |
31 | |
} |
32 | |
|
33 | |
public void setClassName(String className) { |
34 | 0 | this.className = className; |
35 | 0 | } |
36 | |
|
37 | |
public ClassLoader getClassLoader() { |
38 | 0 | return classLoader; |
39 | |
} |
40 | |
|
41 | |
public void setClassLoader(ClassLoader classLoader) { |
42 | 0 | this.classLoader = classLoader; |
43 | 0 | } |
44 | |
|
45 | |
public void setParam(String name, String value) { |
46 | 0 | this.params.add(name); |
47 | 0 | this.paramValues.put(name, value); |
48 | 0 | } |
49 | |
|
50 | |
@Override |
51 | |
public Warnings warn(Database database) { |
52 | 0 | return new Warnings(); |
53 | |
} |
54 | |
|
55 | |
@Override |
56 | |
public ValidationErrors validate(Database database) { |
57 | 0 | return new ValidationErrors(); |
58 | |
} |
59 | |
|
60 | |
@Override |
61 | |
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) |
62 | |
throws PreconditionFailedException, PreconditionErrorException { |
63 | |
CustomPrecondition customPrecondition; |
64 | |
try { |
65 | |
|
66 | |
try { |
67 | 0 | customPrecondition = (CustomPrecondition) Class.forName(className, true, classLoader).newInstance(); |
68 | 0 | } catch (ClassCastException e) { |
69 | 0 | customPrecondition = (CustomPrecondition) Class.forName(className).newInstance(); |
70 | 0 | } |
71 | 0 | } catch (Exception e) { |
72 | 0 | throw new PreconditionFailedException("Could not open custom precondition class " + className, changeLog, |
73 | |
this); |
74 | 0 | } |
75 | |
|
76 | 0 | for (String param : params) { |
77 | |
try { |
78 | 0 | ObjectUtil.setProperty(customPrecondition, param, paramValues.get(param)); |
79 | 0 | } catch (Exception e) { |
80 | 0 | throw new PreconditionFailedException("Error setting parameter " + param + " on custom precondition " |
81 | |
+ className, changeLog, this); |
82 | 0 | } |
83 | |
} |
84 | |
|
85 | |
try { |
86 | 0 | customPrecondition.check(database); |
87 | 0 | } catch (CustomPreconditionFailedException e) { |
88 | 0 | throw new PreconditionFailedException(new FailedPrecondition("Custom Precondition Failed: " |
89 | |
+ e.getMessage(), changeLog, this)); |
90 | 0 | } catch (CustomPreconditionErrorException e) { |
91 | 0 | throw new PreconditionErrorException(new ErrorPrecondition(e, changeLog, this)); |
92 | 0 | } |
93 | 0 | } |
94 | |
|
95 | |
@Override |
96 | |
public String getName() { |
97 | 8 | return "customPrecondition"; |
98 | |
} |
99 | |
} |