1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.scripting.freemarker;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.nio.charset.Charset;
23 import java.nio.charset.StandardCharsets;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.Optional;
29 import java.util.Properties;
30 import java.util.function.Consumer;
31 import java.util.function.Function;
32
33 import org.apache.commons.text.WordUtils;
34 import org.apache.ibatis.io.Resources;
35 import org.apache.ibatis.logging.Log;
36 import org.apache.ibatis.logging.LogFactory;
37 import org.apache.ibatis.reflection.DefaultReflectorFactory;
38 import org.apache.ibatis.reflection.MetaObject;
39 import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
40 import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
41
42
43
44
45
46
47
48
49 public class FreeMarkerLanguageDriverConfig {
50 private static final String PROPERTY_KEY_CONFIG_FILE = "mybatis-freemarker.config.file";
51 private static final String PROPERTY_KEY_CONFIG_ENCODING = "mybatis-freemarker.config.encoding";
52 private static final String DEFAULT_PROPERTIES_FILE = "mybatis-freemarker.properties";
53 private static final Map<Class<?>, Function<String, Object>> TYPE_CONVERTERS;
54
55 static {
56 Map<Class<?>, Function<String, Object>> converters = new HashMap<>();
57 converters.put(String.class, String::trim);
58 converters.put(boolean.class, v -> Boolean.valueOf(v.trim()));
59 converters.put(Object.class, v -> v);
60 TYPE_CONVERTERS = Collections.unmodifiableMap(converters);
61 }
62
63 private static final Log log = LogFactory.getLog(FreeMarkerLanguageDriverConfig.class);
64
65
66
67
68 private final Map<String, String> freemarkerSettings = new HashMap<>();
69
70
71
72
73 private final TemplateFileConfig templateFile = new TemplateFileConfig();
74
75
76
77
78
79
80 public Map<String, String> getFreemarkerSettings() {
81 return freemarkerSettings;
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95 @Deprecated
96 public String getBasePackage() {
97 return templateFile.getBaseDir();
98 }
99
100
101
102
103
104
105
106
107
108
109 @Deprecated
110 public void setBasePackage(String basePackage) {
111 log.warn("The 'basePackage' has been deprecated since 1.2.0. Please use the 'templateFile.baseDir'.");
112 templateFile.setBaseDir(basePackage);
113 }
114
115
116
117
118
119
120 public TemplateFileConfig getTemplateFile() {
121 return templateFile;
122 }
123
124
125
126
127 public static class TemplateFileConfig {
128
129
130
131
132 private String baseDir = "";
133
134
135
136
137 private final PathProviderConfig pathProvider = new PathProviderConfig();
138
139
140
141
142
143
144
145
146
147 public String getBaseDir() {
148 return baseDir;
149 }
150
151
152
153
154
155
156
157 public void setBaseDir(String baseDir) {
158 this.baseDir = baseDir;
159 }
160
161
162
163
164
165
166 public PathProviderConfig getPathProvider() {
167 return pathProvider;
168 }
169
170
171
172
173 public static class PathProviderConfig {
174
175
176
177
178 private String prefix = "";
179
180
181
182
183 private boolean includesPackagePath = true;
184
185
186
187
188 private boolean separateDirectoryPerMapper = true;
189
190
191
192
193 private boolean includesMapperNameWhenSeparateDirectory = true;
194
195
196
197
198 private boolean cacheEnabled = true;
199
200
201
202
203
204
205
206
207
208 public String getPrefix() {
209 return prefix;
210 }
211
212
213
214
215
216
217
218 public void setPrefix(String prefix) {
219 this.prefix = prefix;
220 }
221
222
223
224
225
226
227
228
229
230 public boolean isIncludesPackagePath() {
231 return includesPackagePath;
232 }
233
234
235
236
237
238
239
240 public void setIncludesPackagePath(boolean includesPackagePath) {
241 this.includesPackagePath = includesPackagePath;
242 }
243
244
245
246
247
248
249 public boolean isSeparateDirectoryPerMapper() {
250 return separateDirectoryPerMapper;
251 }
252
253
254
255
256
257
258
259
260
261
262 public void setSeparateDirectoryPerMapper(boolean separateDirectoryPerMapper) {
263 this.separateDirectoryPerMapper = separateDirectoryPerMapper;
264 }
265
266
267
268
269
270
271
272
273
274 public boolean isIncludesMapperNameWhenSeparateDirectory() {
275 return includesMapperNameWhenSeparateDirectory;
276 }
277
278
279
280
281
282
283
284
285
286
287 public void setIncludesMapperNameWhenSeparateDirectory(boolean includesMapperNameWhenSeparateDirectory) {
288 this.includesMapperNameWhenSeparateDirectory = includesMapperNameWhenSeparateDirectory;
289 }
290
291
292
293
294
295
296
297
298
299 public boolean isCacheEnabled() {
300 return cacheEnabled;
301 }
302
303
304
305
306
307
308
309 public void setCacheEnabled(boolean cacheEnabled) {
310 this.cacheEnabled = cacheEnabled;
311 }
312
313 }
314
315 }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 public static FreeMarkerLanguageDriverConfig newInstance() {
348 return newInstance(loadDefaultProperties());
349 }
350
351
352
353
354
355
356
357
358
359
360
361 public static FreeMarkerLanguageDriverConfig newInstance(Properties customProperties) {
362 FreeMarkerLanguageDriverConfig config = new FreeMarkerLanguageDriverConfig();
363 Properties properties = loadDefaultProperties();
364 Optional.ofNullable(customProperties).ifPresent(properties::putAll);
365 override(config, properties);
366 return config;
367 }
368
369
370
371
372
373
374
375
376
377
378
379 public static FreeMarkerLanguageDriverConfig newInstance(Consumer<FreeMarkerLanguageDriverConfig> customizer) {
380 FreeMarkerLanguageDriverConfig config = new FreeMarkerLanguageDriverConfig();
381 customizer.accept(config);
382 override(config, loadDefaultProperties());
383 return config;
384 }
385
386 private static void override(FreeMarkerLanguageDriverConfig config, Properties properties) {
387 MetaObject metaObject = MetaObject.forObject(config, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(),
388 new DefaultReflectorFactory());
389 properties.forEach((key, value) -> {
390 String propertyPath = WordUtils
391 .uncapitalize(WordUtils.capitalize(Objects.toString(key), '-').replaceAll("-", ""));
392 Optional.ofNullable(value).ifPresent(v -> {
393 Object convertedValue = TYPE_CONVERTERS.get(metaObject.getSetterType(propertyPath)).apply(value.toString());
394 metaObject.setValue(propertyPath, convertedValue);
395 });
396 });
397 }
398
399 private static Properties loadDefaultProperties() {
400 return loadProperties(System.getProperty(PROPERTY_KEY_CONFIG_FILE, DEFAULT_PROPERTIES_FILE));
401 }
402
403 private static Properties loadProperties(String resourcePath) {
404 Properties properties = new Properties();
405 InputStream in;
406 try {
407 in = Resources.getResourceAsStream(resourcePath);
408 } catch (IOException e) {
409 in = null;
410 }
411 if (in != null) {
412 Charset encoding = Optional.ofNullable(System.getProperty(PROPERTY_KEY_CONFIG_ENCODING)).map(Charset::forName)
413 .orElse(StandardCharsets.UTF_8);
414 try (InputStreamReader inReader = new InputStreamReader(in, encoding);
415 BufferedReader bufReader = new BufferedReader(inReader)) {
416 properties.load(bufReader);
417 } catch (IOException e) {
418 throw new IllegalStateException(e);
419 }
420 }
421 return properties;
422 }
423
424 }