1 | |
package liquibase.serializer; |
2 | |
|
3 | |
import liquibase.exception.UnexpectedLiquibaseException; |
4 | |
import liquibase.servicelocator.ServiceLocator; |
5 | |
|
6 | |
import java.util.ArrayList; |
7 | |
import java.util.HashMap; |
8 | |
import java.util.List; |
9 | |
import java.util.Map; |
10 | |
|
11 | |
public class ChangeLogSerializerFactory { |
12 | |
private static ChangeLogSerializerFactory instance; |
13 | |
|
14 | 7 | private Map<String, ChangeLogSerializer> serializers = new HashMap<String, ChangeLogSerializer>(); |
15 | |
|
16 | |
public static void reset() { |
17 | 7 | instance = new ChangeLogSerializerFactory(); |
18 | 7 | } |
19 | |
|
20 | |
public static ChangeLogSerializerFactory getInstance() { |
21 | 12 | if (instance == null) { |
22 | 0 | instance = new ChangeLogSerializerFactory(); |
23 | |
} |
24 | |
|
25 | 12 | return instance; |
26 | |
} |
27 | |
|
28 | 7 | private ChangeLogSerializerFactory() { |
29 | |
Class<? extends ChangeLogSerializer>[] classes; |
30 | |
try { |
31 | 7 | classes = ServiceLocator.getInstance().findClasses(ChangeLogSerializer.class); |
32 | |
|
33 | 21 | for (Class<? extends ChangeLogSerializer> clazz : classes) { |
34 | 14 | register((ChangeLogSerializer) clazz.getConstructor().newInstance()); |
35 | |
} |
36 | 0 | } catch (Exception e) { |
37 | 0 | throw new UnexpectedLiquibaseException(e); |
38 | 7 | } |
39 | |
|
40 | 7 | } |
41 | |
|
42 | |
public Map<String, ChangeLogSerializer> getSerializers() { |
43 | 8 | return serializers; |
44 | |
} |
45 | |
|
46 | |
public ChangeLogSerializer getSerializer(String fileNameOrExtension) { |
47 | 1 | fileNameOrExtension = fileNameOrExtension.replaceAll(".*\\.", ""); |
48 | 1 | return serializers.get(fileNameOrExtension); |
49 | |
} |
50 | |
|
51 | |
public void register(ChangeLogSerializer changeLogSerializer) { |
52 | 34 | for (String extension : changeLogSerializer.getValidFileExtensions()) { |
53 | 17 | serializers.put(extension, changeLogSerializer); |
54 | |
} |
55 | 17 | } |
56 | |
|
57 | |
public void unregister(ChangeLogSerializer changeLogSerializer) { |
58 | 1 | List<Map.Entry<String, ChangeLogSerializer>> entrysToRemove = new ArrayList<Map.Entry<String, ChangeLogSerializer>>(); |
59 | 1 | for (Map.Entry<String, ChangeLogSerializer> entry : serializers.entrySet()) { |
60 | 2 | if (entry.getValue().equals(changeLogSerializer)) { |
61 | 1 | entrysToRemove.add(entry); |
62 | |
} |
63 | |
} |
64 | |
|
65 | 1 | for (Map.Entry<String, ChangeLogSerializer> entry : entrysToRemove) { |
66 | 1 | serializers.remove(entry.getKey()); |
67 | |
} |
68 | |
|
69 | 1 | } |
70 | |
|
71 | |
} |