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 public class CustomPreconditionWrapper implements Precondition {
22
23 private String className;
24 private ClassLoader classLoader;
25
26 private SortedSet<String> params = new TreeSet<String>();
27 private Map<String, String> paramValues = new HashMap<String, String>();
28
29 public String getClassName() {
30 return className;
31 }
32
33 public void setClassName(String className) {
34 this.className = className;
35 }
36
37 public ClassLoader getClassLoader() {
38 return classLoader;
39 }
40
41 public void setClassLoader(ClassLoader classLoader) {
42 this.classLoader = classLoader;
43 }
44
45 public void setParam(String name, String value) {
46 this.params.add(name);
47 this.paramValues.put(name, value);
48 }
49
50 @Override
51 public Warnings warn(Database database) {
52 return new Warnings();
53 }
54
55 @Override
56 public ValidationErrors validate(Database database) {
57 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 customPrecondition = (CustomPrecondition) Class.forName(className, true, classLoader).newInstance();
68 } catch (ClassCastException e) {
69 customPrecondition = (CustomPrecondition) Class.forName(className).newInstance();
70 }
71 } catch (Exception e) {
72 throw new PreconditionFailedException("Could not open custom precondition class " + className, changeLog,
73 this);
74 }
75
76 for (String param : params) {
77 try {
78 ObjectUtil.setProperty(customPrecondition, param, paramValues.get(param));
79 } catch (Exception e) {
80 throw new PreconditionFailedException("Error setting parameter " + param + " on custom precondition "
81 + className, changeLog, this);
82 }
83 }
84
85 try {
86 customPrecondition.check(database);
87 } catch (CustomPreconditionFailedException e) {
88 throw new PreconditionFailedException(new FailedPrecondition("Custom Precondition Failed: "
89 + e.getMessage(), changeLog, this));
90 } catch (CustomPreconditionErrorException e) {
91 throw new PreconditionErrorException(new ErrorPrecondition(e, changeLog, this));
92 }
93 }
94
95 @Override
96 public String getName() {
97 return "customPrecondition";
98 }
99 }