1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.sourceforge.acelogger.factory;
16
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import net.sourceforge.acelogger.constants.FormatterConstants;
23 import net.sourceforge.acelogger.execution.LogController;
24 import net.sourceforge.acelogger.formatter.Formatter;
25
26
27
28
29
30
31
32
33 public final class FormatterFactory {
34
35
36
37
38 private static final FormatterFactory INSTANCE = new FormatterFactory();
39
40
41
42
43 private Formatter defaultFormatter;
44
45
46
47
48 private final Map<String, Formatter> registeredFormatters;
49
50
51
52
53
54
55 private FormatterFactory() {
56 registeredFormatters = new HashMap<String, Formatter>();
57 defaultFormatter = FormatterConstants.EMPTY;
58 }
59
60
61
62
63
64
65
66
67
68
69
70 public static Formatter getFormatter(String identifier) {
71 LogController.ensureInitialization();
72 Formatter intendedFormatter = INSTANCE.registeredFormatters.get(identifier);
73 if (intendedFormatter == null) {
74 int lastDotIndex = identifier.lastIndexOf('.');
75 if (lastDotIndex > 0) {
76 intendedFormatter = getFormatter(identifier.substring(0, lastDotIndex));
77 } else {
78 intendedFormatter = INSTANCE.defaultFormatter;
79 }
80 }
81 return intendedFormatter;
82 }
83
84
85
86
87
88
89
90
91
92
93 public static Formatter setDefaultFormatter(Formatter defaultFormatter) {
94 LogController.ensureInitialization();
95 Formatter previous = INSTANCE.defaultFormatter;
96 if (defaultFormatter != null) {
97 INSTANCE.defaultFormatter = defaultFormatter;
98 }
99 return previous;
100 }
101
102
103
104
105
106
107
108
109 public static Formatter getDefaultFormatter() {
110 LogController.ensureInitialization();
111 return INSTANCE.defaultFormatter;
112 }
113
114
115
116
117
118
119
120 public static List<Formatter> getRegisteredFormatters() {
121 LogController.ensureInitialization();
122 List<Formatter> allFormatters = new ArrayList<Formatter>(INSTANCE.registeredFormatters
123 .values());
124 return allFormatters;
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138 public static Formatter registerFormatter(Formatter formatter) {
139 LogController.ensureInitialization();
140 Formatter intendedFormatter = null;
141 if (formatter != null) {
142 intendedFormatter = INSTANCE.registeredFormatters.put(formatter.getIdentifier(),
143 formatter);
144 }
145 if (intendedFormatter == null) {
146 intendedFormatter = INSTANCE.defaultFormatter;
147 }
148 return intendedFormatter;
149 }
150
151 }