All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.avaje.datasource.ConfigPropertiesHelper Maven / Gradle / Ivy

There is a newer version: 4.1
Show newest version
package org.avaje.datasource;

import java.util.Properties;

/**
 * Helper used to read Properties.
 */
class ConfigPropertiesHelper {

  private final Properties properties;

  private final String prefix;

  private final String poolName;

  /**
   * Construct with a prefix, serverName and properties.
   */
  ConfigPropertiesHelper(String prefix, String poolName, Properties properties) {
    this.poolName = poolName;
    this.prefix = prefix;
    this.properties = properties;
  }

  /**
   * Get a property with no default value.
   */
  private String read(String key) {
    String val = properties.getProperty(key.toLowerCase());
    if (val == null) {
      return properties.getProperty(key);
    }
    return val;
  }

  /**
   * Get a property with a default value.
   * 

* This performs a search using the prefix and server name (if supplied) to search for the property * value in order based on: *

{@code
   *
   *   prefix.serverName.key
   *   prefix.key
   *   key
   *
   * }
*

*/ String get(String key, String defaultValue) { String value = null; if (poolName != null && prefix != null) { value = read(prefix + "." + poolName + "." + key); } if (value == null && prefix != null) { value = read(prefix + "." + key); } if (value == null) { value = read(key); } return value == null ? defaultValue : value; } /** * Return an int property value. */ int getInt(String key, int defaultValue) { String value = get(key, String.valueOf(defaultValue)); return Integer.parseInt(value); } /** * Return a boolean property value. */ boolean getBoolean(String key, boolean defaultValue) { String value = get(key, String.valueOf(defaultValue)); return Boolean.parseBoolean(value); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy