01: package liquibase.change;
02:
03: import liquibase.change.custom.CustomChangeWrapper;
04:
05: import java.util.HashMap;
06: import java.util.Map;
07:
08: /**
09: * Factory class for constructing the correct liquibase.change.Change implementation based on the tag name.
10: * It is currently implemented by a static array of Change implementations, although that may change in
11: * later revisions. The best way to get an instance of ChangeFactory is off the Liquibase.getChangeFactory() method.
12: *
13: * @see liquibase.change.Change
14: */
15: public class ChangeFactory {
16:
17: @SuppressWarnings("unchecked")
18: private final Map<String, Class> tagToClassMap;
19:
20: private static final ChangeFactory instance = new ChangeFactory();
21:
22: @SuppressWarnings("unchecked")
23: private ChangeFactory() {
24: tagToClassMap = new HashMap<String, Class>();
25: Class[] changes = new Class[] { AddColumnChange.class,
26: AlterSequenceChange.class, CreateIndexChange.class,
27: CreateSequenceChange.class, CreateTableChange.class,
28: DropColumnChange.class, DropIndexChange.class,
29: DropSequenceChange.class, DropTableChange.class,
30: InsertDataChange.class, ModifyColumnChange.class,
31: RawSQLChange.class, RenameColumnChange.class,
32: RenameTableChange.class,
33: AddNotNullConstraintChange.class,
34: DropNotNullConstraintChange.class,
35: CreateViewChange.class, DropViewChange.class,
36: MergeColumnChange.class, RenameViewChange.class,
37: AddForeignKeyConstraintChange.class,
38: DropForeignKeyConstraintChange.class,
39: AddLookupTableChange.class, AddPrimaryKeyChange.class,
40: DropPrimaryKeyChange.class,
41: AddAutoIncrementChange.class,
42: AddDefaultValueChange.class,
43: DropDefaultValueChange.class,
44: AddUniqueConstraintChange.class,
45: DropUniqueConstraintChange.class, SQLFileChange.class,
46: CustomChangeWrapper.class, CreateProcedureChange.class,
47: ExecuteShellCommandChange.class,
48: UpdateDataChange.class, DeleteDataChange.class, };
49:
50: try {
51: for (Class<Change> changeClass : changes) {
52: Change change = changeClass.newInstance();
53: tagToClassMap.put(change.getTagName(), changeClass);
54: }
55: } catch (Exception e) {
56: throw new RuntimeException(e);
57: }
58: }
59:
60: public static ChangeFactory getInstance() {
61: return instance;
62: }
63:
64: /**
65: * Create a new Change subclass based on the given tag name.
66: */
67: public Change create(String tagName) {
68: Class<?> aClass = tagToClassMap.get(tagName);
69: if (aClass == null) {
70: throw new RuntimeException("Unknown tag: " + tagName);
71: }
72: try {
73: return (Change) aClass.newInstance();
74: } catch (Exception e) {
75: throw new RuntimeException(e);
76: }
77: }
78: }
|