View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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"; //$NON-NLS-1$
28      private static final String DEFAULT_PROPERTY_FILE = "mybatis-dynamic-sql.properties"; //$NON-NLS-1$
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); //$NON-NLS-1$
59          }
60      }
61  
62      private void initializeKnownProperties() {
63          String value = properties.getProperty("nonRenderingWhereClauseAllowed", "false"); //$NON-NLS-1$ //$NON-NLS-2$
64          isNonRenderingWhereClauseAllowed = Boolean.parseBoolean(value);
65      }
66  
67      public boolean isIsNonRenderingWhereClauseAllowed() {
68          return isNonRenderingWhereClauseAllowed;
69      }
70  }