| 1 | |
package liquibase.change.custom; |
| 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.change.AbstractChange; |
| 9 | |
import liquibase.change.ChangeMetaData; |
| 10 | |
import liquibase.change.ChangeProperty; |
| 11 | |
import liquibase.database.Database; |
| 12 | |
import liquibase.exception.CustomChangeException; |
| 13 | |
import liquibase.exception.RollbackImpossibleException; |
| 14 | |
import liquibase.exception.UnexpectedLiquibaseException; |
| 15 | |
import liquibase.exception.UnsupportedChangeException; |
| 16 | |
import liquibase.exception.ValidationErrors; |
| 17 | |
import liquibase.exception.Warnings; |
| 18 | |
import liquibase.statement.SqlStatement; |
| 19 | |
import liquibase.util.ObjectUtil; |
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
public class CustomChangeWrapper extends AbstractChange { |
| 29 | |
|
| 30 | |
@ChangeProperty(includeInSerialization = false) |
| 31 | |
private CustomChange customChange; |
| 32 | |
|
| 33 | |
private String className; |
| 34 | |
|
| 35 | 12 | @ChangeProperty(includeInSerialization = false) |
| 36 | |
private SortedSet<String> params = new TreeSet<String>(); |
| 37 | |
|
| 38 | 12 | private Map<String, String> paramValues = new HashMap<String, String>(); |
| 39 | |
|
| 40 | |
@ChangeProperty(includeInSerialization = false) |
| 41 | |
private ClassLoader classLoader; |
| 42 | |
|
| 43 | |
public CustomChangeWrapper() { |
| 44 | 12 | super("customChange", "Custom Change", ChangeMetaData.PRIORITY_DEFAULT); |
| 45 | 12 | } |
| 46 | |
|
| 47 | |
public CustomChange getCustomChange() { |
| 48 | 2 | return customChange; |
| 49 | |
} |
| 50 | |
|
| 51 | |
public ClassLoader getClassLoader() { |
| 52 | 0 | return classLoader; |
| 53 | |
} |
| 54 | |
|
| 55 | |
public void setClassLoader(ClassLoader classLoader) { |
| 56 | 2 | this.classLoader = classLoader; |
| 57 | 2 | } |
| 58 | |
|
| 59 | |
public void setClass(String className) throws CustomChangeException { |
| 60 | 2 | this.className = className; |
| 61 | |
try { |
| 62 | |
try { |
| 63 | 2 | customChange = (CustomChange) Class.forName(className, true, classLoader).newInstance(); |
| 64 | 0 | } catch (ClassCastException e) { |
| 65 | |
try { |
| 66 | 0 | customChange = (CustomChange) Thread.currentThread().getContextClassLoader().loadClass(className) |
| 67 | |
.newInstance(); |
| 68 | 0 | } catch (ClassNotFoundException e1) { |
| 69 | 0 | customChange = (CustomChange) Class.forName(className).newInstance(); |
| 70 | 0 | } |
| 71 | 2 | } |
| 72 | 0 | } catch (Exception e) { |
| 73 | 0 | throw new CustomChangeException(e); |
| 74 | 2 | } |
| 75 | 2 | } |
| 76 | |
|
| 77 | |
public String getClassName() { |
| 78 | 0 | return className; |
| 79 | |
} |
| 80 | |
|
| 81 | |
public void setParam(String name, String value) { |
| 82 | 5 | this.params.add(name); |
| 83 | 5 | this.paramValues.put(name, value); |
| 84 | 5 | } |
| 85 | |
|
| 86 | |
public SortedSet<String> getParams() { |
| 87 | 0 | return params; |
| 88 | |
} |
| 89 | |
|
| 90 | |
public Map<String, String> getParamValues() { |
| 91 | 0 | return paramValues; |
| 92 | |
} |
| 93 | |
|
| 94 | |
@Override |
| 95 | |
public ValidationErrors validate(Database database) { |
| 96 | |
try { |
| 97 | 0 | return customChange.validate(database); |
| 98 | 0 | } catch (AbstractMethodError e) { |
| 99 | 0 | return new ValidationErrors(); |
| 100 | |
} |
| 101 | |
} |
| 102 | |
|
| 103 | |
@Override |
| 104 | |
public Warnings warn(Database database) { |
| 105 | |
|
| 106 | 0 | return new Warnings(); |
| 107 | |
} |
| 108 | |
|
| 109 | |
@Override |
| 110 | |
public SqlStatement[] generateStatements(Database database) { |
| 111 | 1 | SqlStatement[] statements = null; |
| 112 | |
try { |
| 113 | 1 | configureCustomChange(); |
| 114 | 1 | if (customChange instanceof CustomSqlChange) { |
| 115 | 1 | statements = ((CustomSqlChange) customChange).generateStatements(database); |
| 116 | 0 | } else if (customChange instanceof CustomTaskChange) { |
| 117 | 0 | ((CustomTaskChange) customChange).execute(database); |
| 118 | |
} else { |
| 119 | 0 | throw new UnexpectedLiquibaseException(customChange.getClass().getName() + " does not implement " |
| 120 | |
+ CustomSqlChange.class.getName() + " or " + CustomTaskChange.class.getName()); |
| 121 | |
} |
| 122 | 0 | } catch (CustomChangeException e) { |
| 123 | 0 | throw new UnexpectedLiquibaseException(e); |
| 124 | 1 | } |
| 125 | |
|
| 126 | 1 | if (statements == null) { |
| 127 | 0 | statements = new SqlStatement[0]; |
| 128 | |
} |
| 129 | 1 | return statements; |
| 130 | |
} |
| 131 | |
|
| 132 | |
@Override |
| 133 | |
public SqlStatement[] generateRollbackStatements(Database database) throws UnsupportedChangeException, |
| 134 | |
RollbackImpossibleException { |
| 135 | 0 | SqlStatement[] statements = null; |
| 136 | |
try { |
| 137 | 0 | configureCustomChange(); |
| 138 | 0 | if (customChange instanceof CustomSqlRollback) { |
| 139 | 0 | statements = ((CustomSqlRollback) customChange).generateRollbackStatements(database); |
| 140 | 0 | } else if (customChange instanceof CustomTaskRollback) { |
| 141 | 0 | ((CustomTaskRollback) customChange).rollback(database); |
| 142 | |
} else { |
| 143 | 0 | throw new UnsupportedChangeException("Unknown rollback type: " + customChange.getClass().getName()); |
| 144 | |
} |
| 145 | 0 | } catch (CustomChangeException e) { |
| 146 | 0 | throw new UnsupportedChangeException(e); |
| 147 | 0 | } |
| 148 | |
|
| 149 | 0 | if (statements == null) { |
| 150 | 0 | statements = new SqlStatement[0]; |
| 151 | |
} |
| 152 | 0 | return statements; |
| 153 | |
|
| 154 | |
} |
| 155 | |
|
| 156 | |
@Override |
| 157 | |
public boolean supportsRollback(Database database) { |
| 158 | 0 | return customChange instanceof CustomSqlRollback || customChange instanceof CustomTaskRollback; |
| 159 | |
} |
| 160 | |
|
| 161 | |
private void configureCustomChange() throws CustomChangeException { |
| 162 | |
try { |
| 163 | 1 | for (String param : params) { |
| 164 | 2 | ObjectUtil.setProperty(customChange, param, paramValues.get(param)); |
| 165 | |
} |
| 166 | 1 | customChange.setFileOpener(getResourceAccessor()); |
| 167 | 1 | customChange.setUp(); |
| 168 | 0 | } catch (Exception e) { |
| 169 | 0 | throw new CustomChangeException(e); |
| 170 | 1 | } |
| 171 | 1 | } |
| 172 | |
|
| 173 | |
@Override |
| 174 | |
public String getConfirmationMessage() { |
| 175 | 0 | return customChange.getConfirmationMessage(); |
| 176 | |
} |
| 177 | |
} |