1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.dynamic.sql.configuration;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Objects;
21 import java.util.Properties;
22
23 import org.mybatis.dynamic.sql.exception.DynamicSqlException;
24 import org.mybatis.dynamic.sql.util.Messages;
25
26 public class GlobalConfiguration {
27 public static final String CONFIGURATION_FILE_PROPERTY = "mybatis-dynamic-sql.configurationFile";
28 private static final String DEFAULT_PROPERTY_FILE = "mybatis-dynamic-sql.properties";
29 private boolean isNonRenderingWhereClauseAllowed = false;
30 private final Properties properties = new Properties();
31
32 public GlobalConfiguration() {
33 initialize();
34 }
35
36 private void initialize() {
37 initializeProperties();
38 initializeKnownProperties();
39 }
40
41 private void initializeProperties() {
42 String configFileName = getConfigurationFileName();
43 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(configFileName);
44 if (inputStream != null) {
45 loadProperties(inputStream, configFileName);
46 }
47 }
48
49 private String getConfigurationFileName() {
50 String property = System.getProperty(CONFIGURATION_FILE_PROPERTY);
51 return Objects.requireNonNullElse(property, DEFAULT_PROPERTY_FILE);
52 }
53
54 void loadProperties(InputStream inputStream, String propertyFile) {
55 try {
56 properties.load(inputStream);
57 } catch (IOException e) {
58 throw new DynamicSqlException(Messages.getString("ERROR.3", propertyFile), e);
59 }
60 }
61
62 private void initializeKnownProperties() {
63 String value = properties.getProperty("nonRenderingWhereClauseAllowed", "false");
64 isNonRenderingWhereClauseAllowed = Boolean.parseBoolean(value);
65 }
66
67 public boolean isIsNonRenderingWhereClauseAllowed() {
68 return isNonRenderingWhereClauseAllowed;
69 }
70 }