1 | |
package liquibase.change; |
2 | |
|
3 | |
import java.util.Comparator; |
4 | |
import java.util.Map; |
5 | |
import java.util.SortedSet; |
6 | |
import java.util.TreeSet; |
7 | |
import java.util.concurrent.ConcurrentHashMap; |
8 | |
|
9 | |
import liquibase.exception.UnexpectedLiquibaseException; |
10 | |
import liquibase.servicelocator.ServiceLocator; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
public class ChangeFactory { |
20 | |
|
21 | |
private static ChangeFactory instance; |
22 | |
|
23 | 9 | private Map<String, SortedSet<Class<? extends Change>>> registry = new ConcurrentHashMap<String, SortedSet<Class<? extends Change>>>(); |
24 | |
|
25 | 9 | private ChangeFactory() { |
26 | |
Class<? extends Change>[] classes; |
27 | |
try { |
28 | 9 | classes = ServiceLocator.getInstance().findClasses(Change.class); |
29 | |
|
30 | 396 | for (Class<? extends Change> clazz : classes) { |
31 | |
|
32 | 387 | register(clazz); |
33 | |
} |
34 | |
|
35 | 0 | } catch (Exception e) { |
36 | 0 | throw new UnexpectedLiquibaseException(e); |
37 | 9 | } |
38 | |
|
39 | 9 | } |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public static synchronized ChangeFactory getInstance() { |
45 | 38 | if (instance == null) { |
46 | 0 | instance = new ChangeFactory(); |
47 | |
} |
48 | 38 | return instance; |
49 | |
} |
50 | |
|
51 | |
public static void reset() { |
52 | 9 | instance = new ChangeFactory(); |
53 | 9 | } |
54 | |
|
55 | |
public void register(Class<? extends Change> changeClass) { |
56 | |
try { |
57 | 394 | String name = changeClass.newInstance().getChangeMetaData().getName(); |
58 | 394 | if (registry.get(name) == null) { |
59 | 394 | registry.put(name, new TreeSet<Class<? extends Change>>(new Comparator<Class<? extends Change>>() { |
60 | |
public int compare(Class<? extends Change> o1, Class<? extends Change> o2) { |
61 | |
try { |
62 | 0 | return -1 |
63 | |
* new Integer(o1.newInstance().getChangeMetaData().getPriority()).compareTo(o2 |
64 | |
.newInstance().getChangeMetaData().getPriority()); |
65 | 0 | } catch (Exception e) { |
66 | 0 | throw new UnexpectedLiquibaseException(e); |
67 | |
} |
68 | |
} |
69 | |
})); |
70 | |
} |
71 | 394 | registry.get(name).add(changeClass); |
72 | 0 | } catch (Exception e) { |
73 | 0 | throw new UnexpectedLiquibaseException(e); |
74 | 394 | } |
75 | 394 | } |
76 | |
|
77 | |
public void unregister(String name) { |
78 | 2 | registry.remove(name); |
79 | 2 | } |
80 | |
|
81 | |
public Map<String, SortedSet<Class<? extends Change>>> getRegistry() { |
82 | 13 | return registry; |
83 | |
} |
84 | |
|
85 | |
public Change create(String name) { |
86 | 25 | SortedSet<Class<? extends Change>> classes = registry.get(name); |
87 | |
|
88 | 25 | if (classes == null) { |
89 | 1 | return null; |
90 | |
} |
91 | |
|
92 | |
try { |
93 | 24 | return classes.iterator().next().newInstance(); |
94 | 0 | } catch (Exception e) { |
95 | 0 | throw new UnexpectedLiquibaseException(e); |
96 | |
} |
97 | |
} |
98 | |
|
99 | |
} |