1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender.db;
18
19 import java.util.Date;
20
21 import org.apache.logging.log4j.Logger;
22 import org.apache.logging.log4j.core.Core;
23 import org.apache.logging.log4j.core.StringLayout;
24 import org.apache.logging.log4j.core.config.Configuration;
25 import org.apache.logging.log4j.core.config.plugins.Plugin;
26 import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
27 import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
28 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
29 import org.apache.logging.log4j.core.config.plugins.PluginElement;
30 import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
31 import org.apache.logging.log4j.core.layout.PatternLayout;
32 import org.apache.logging.log4j.spi.ThreadContextMap;
33 import org.apache.logging.log4j.spi.ThreadContextStack;
34 import org.apache.logging.log4j.status.StatusLogger;
35 import org.apache.logging.log4j.util.ReadOnlyStringMap;
36
37
38
39
40
41
42 @Plugin(name = "ColumnMapping", category = Core.CATEGORY_NAME, printObject = true)
43 public class ColumnMapping {
44
45
46
47
48 public static class Builder implements org.apache.logging.log4j.core.util.Builder<ColumnMapping> {
49
50 @PluginConfiguration
51 private Configuration configuration;
52
53 @PluginElement("Layout")
54 private StringLayout layout;
55
56 @PluginBuilderAttribute
57 private String literal;
58
59 @PluginBuilderAttribute
60 @Required(message = "No column name provided")
61 private String name;
62
63 @PluginBuilderAttribute
64 private String parameter;
65
66 @PluginBuilderAttribute
67 private String pattern;
68
69 @PluginBuilderAttribute
70 private String source;
71
72 @PluginBuilderAttribute
73 @Required(message = "No conversion type provided")
74 private Class<?> type = String.class;
75
76 @Override
77 public ColumnMapping build() {
78 if (pattern != null) {
79 layout = PatternLayout.newBuilder()
80 .withPattern(pattern)
81 .withConfiguration(configuration)
82 .build();
83 }
84 if (!(layout == null
85 || literal == null
86 || Date.class.isAssignableFrom(type)
87 || ReadOnlyStringMap.class.isAssignableFrom(type)
88 || ThreadContextMap.class.isAssignableFrom(type)
89 || ThreadContextStack.class.isAssignableFrom(type))) {
90 LOGGER.error("No 'layout' or 'literal' value specified and type ({}) is not compatible with ThreadContextMap, ThreadContextStack, or java.util.Date for the mapping", type, this);
91 return null;
92 }
93 if (literal != null && parameter != null) {
94 LOGGER.error("Only one of 'literal' or 'parameter' can be set on the column mapping {}", this);
95 return null;
96 }
97 return new ColumnMapping(name, source, layout, literal, parameter, type);
98 }
99
100 public Builder setConfiguration(final Configuration configuration) {
101 this.configuration = configuration;
102 return this;
103 }
104
105
106
107
108
109
110
111 public Builder setLayout(final StringLayout layout) {
112 this.layout = layout;
113 return this;
114 }
115
116
117
118
119
120
121
122 public Builder setLiteral(final String literal) {
123 this.literal = literal;
124 return this;
125 }
126
127
128
129
130
131
132 public Builder setName(final String name) {
133 this.name = name;
134 return this;
135 }
136
137
138
139
140
141
142
143 public Builder setParameter(final String parameter) {
144 this.parameter= parameter;
145 return this;
146 }
147
148
149
150
151
152
153
154 public Builder setPattern(final String pattern) {
155 this.pattern = pattern;
156 return this;
157 }
158
159
160
161
162
163
164
165 public Builder setSource(final String source) {
166 this.source = source;
167 return this;
168 }
169
170
171
172
173
174
175
176
177 public Builder setType(final Class<?> type) {
178 this.type = type;
179 return this;
180 }
181
182 @Override
183 public String toString() {
184 return "Builder [name=" + name + ", source=" + source + ", literal=" + literal + ", parameter=" + parameter
185 + ", pattern=" + pattern + ", type=" + type + ", layout=" + layout + "]";
186 }
187 }
188
189 private static final Logger LOGGER = StatusLogger.getLogger();
190 @PluginBuilderFactory
191 public static Builder newBuilder() {
192 return new Builder();
193 }
194
195 private final StringLayout layout;
196 private final String literalValue;
197 private final String name;
198 private final String parameter;
199 private final String source;
200 private final Class<?> type;
201
202 private ColumnMapping(final String name, final String source, final StringLayout layout, final String literalValue, final String parameter, final Class<?> type) {
203 this.name = name;
204 this.source = source;
205 this.layout = layout;
206 this.literalValue = literalValue;
207 this.parameter = parameter;
208 this.type = type;
209 }
210
211 public StringLayout getLayout() {
212 return layout;
213 }
214
215 public String getLiteralValue() {
216 return literalValue;
217 }
218
219 public String getName() {
220 return name;
221 }
222
223 public String getParameter() {
224 return parameter;
225 }
226
227 public String getSource() {
228 return source;
229 }
230
231 public Class<?> getType() {
232 return type;
233 }
234
235 @Override
236 public String toString() {
237 return "ColumnMapping [name=" + name + ", source=" + source + ", literalValue=" + literalValue + ", parameter="
238 + parameter + ", type=" + type + ", layout=" + layout + "]";
239 }
240
241 }