1 | |
package liquibase.snapshot; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.Comparator; |
5 | |
import java.util.List; |
6 | |
import java.util.Set; |
7 | |
import java.util.SortedSet; |
8 | |
import java.util.TreeSet; |
9 | |
|
10 | |
import liquibase.database.Database; |
11 | |
import liquibase.diff.DiffStatusListener; |
12 | |
import liquibase.exception.DatabaseException; |
13 | |
import liquibase.servicelocator.ServiceLocator; |
14 | |
|
15 | |
public class DatabaseSnapshotGeneratorFactory { |
16 | |
|
17 | |
private static DatabaseSnapshotGeneratorFactory instance; |
18 | |
|
19 | 0 | private List<DatabaseSnapshotGenerator> registry = new ArrayList<DatabaseSnapshotGenerator>(); |
20 | |
|
21 | 0 | private DatabaseSnapshotGeneratorFactory() { |
22 | |
try { |
23 | 0 | Class[] classes = ServiceLocator.getInstance().findClasses(DatabaseSnapshotGenerator.class); |
24 | |
|
25 | 0 | for (Class<? extends DatabaseSnapshotGenerator> clazz : classes) { |
26 | 0 | register(clazz.getConstructor().newInstance()); |
27 | |
} |
28 | |
|
29 | 0 | } catch (Exception e) { |
30 | 0 | throw new RuntimeException(e); |
31 | 0 | } |
32 | |
|
33 | 0 | } |
34 | |
|
35 | |
public static DatabaseSnapshotGeneratorFactory getInstance() { |
36 | 0 | if (instance == null) { |
37 | 0 | instance = new DatabaseSnapshotGeneratorFactory(); |
38 | |
} |
39 | 0 | return instance; |
40 | |
} |
41 | |
|
42 | |
public DatabaseSnapshotGenerator getGenerator(Database database) { |
43 | 0 | return getGenerators(database).iterator().next(); |
44 | |
} |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public SortedSet<DatabaseSnapshotGenerator> getGenerators(final Database database) { |
53 | 0 | SortedSet<DatabaseSnapshotGenerator> generators = new TreeSet<DatabaseSnapshotGenerator>( |
54 | 0 | new Comparator<DatabaseSnapshotGenerator>() { |
55 | |
@Override |
56 | |
public int compare(DatabaseSnapshotGenerator o1, DatabaseSnapshotGenerator o2) { |
57 | 0 | return -1 * Integer.valueOf(o1.getPriority(database)).compareTo(o2.getPriority(database)); |
58 | |
} |
59 | |
}); |
60 | |
|
61 | 0 | for (DatabaseSnapshotGenerator generator : registry) { |
62 | 0 | if (generator.supports(database)) { |
63 | 0 | generators.add(generator); |
64 | |
} |
65 | |
} |
66 | |
|
67 | 0 | return generators; |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public DatabaseSnapshot createSnapshot(Database database, String schema, Set<DiffStatusListener> listeners) |
74 | |
throws DatabaseException { |
75 | 0 | return getGenerator(database).createSnapshot(database, schema, listeners); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public List<DatabaseSnapshotGenerator> getRegistry() { |
82 | 0 | return registry; |
83 | |
} |
84 | |
|
85 | |
public void register(DatabaseSnapshotGenerator snapshotGenerator) { |
86 | 0 | registry.add(0, snapshotGenerator); |
87 | 0 | } |
88 | |
|
89 | |
public static void resetAll() { |
90 | 0 | instance = null; |
91 | 0 | } |
92 | |
} |