01: package liquibase.preconditions;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: public class PreconditionFactory {
07: @SuppressWarnings("unchecked")
08: private final Map<String, Class> tagToClassMap;
09:
10: private static final PreconditionFactory instance = new PreconditionFactory();
11:
12: @SuppressWarnings("unchecked")
13: private PreconditionFactory() {
14: tagToClassMap = new HashMap<String, Class>();
15: Class[] preconditions = new Class[] { AndPrecondition.class,
16: OrPrecondition.class, NotPrecondition.class,
17: DBMSPrecondition.class, RunningAsPrecondition.class,
18: SqlPrecondition.class, };
19:
20: try {
21: for (Class<Precondition> preconditionClass : preconditions) {
22: Precondition precondition = preconditionClass
23: .newInstance();
24: tagToClassMap.put(precondition.getTagName(),
25: preconditionClass);
26: }
27: } catch (Exception e) {
28: throw new RuntimeException(e);
29: }
30: }
31:
32: public static PreconditionFactory getInstance() {
33: return instance;
34: }
35:
36: /**
37: * Create a new Precondition subclass based on the given tag name.
38: */
39: public Precondition create(String tagName) {
40: Class<?> aClass = tagToClassMap.get(tagName);
41: if (aClass == null) {
42: throw new RuntimeException("Unknown tag: " + tagName);
43: }
44: try {
45: return (Precondition) aClass.newInstance();
46: } catch (Exception e) {
47: throw new RuntimeException(e);
48: }
49: }
50: }
|