001package com.avaje.ebean.config; 002 003import java.util.Properties; 004 005public class PropertiesWrapper { 006 007 protected final Properties properties; 008 009 protected final String prefix; 010 011 protected final String serverName; 012 013 protected final PropertyMap propertyMap; 014 015 /** 016 * Construct with a prefix, serverName and properties. 017 */ 018 public PropertiesWrapper(String prefix, String serverName, Properties properties) { 019 this.serverName = serverName; 020 this.prefix = prefix; 021 this.propertyMap = PropertyMapLoader.load(null, properties); 022 this.properties = propertyMap.asProperties(); 023 } 024 025 /** 026 * Construct without prefix of serverName. 027 */ 028 public PropertiesWrapper(Properties properties) { 029 this(null, null, properties); 030 } 031 032 /** 033 * Internal copy constructor when changing prefix. 034 */ 035 protected PropertiesWrapper(String prefix, String serverName, PropertyMap propertyMap, Properties properties) { 036 this.serverName = serverName; 037 this.prefix = prefix; 038 this.propertyMap = propertyMap; 039 this.properties = properties; 040 } 041 042 /** 043 * Return a PropertiesWrapper instance with a different prefix but same underlying properties. 044 * <p/> 045 * Used when wanting to use "datasource" as the prefix rather than "ebean". 046 * <p/> 047 * The returning instance should only be used in a read only fashion. 048 */ 049 public PropertiesWrapper withPrefix(String prefix) { 050 return new PropertiesWrapper(prefix, serverName, propertyMap, properties); 051 } 052 053 /** 054 * Return the serverName (optional). 055 */ 056 public String getServerName() { 057 return serverName; 058 } 059 060 /** 061 * Return as Properties with lower case keys and after evaluation and additional properties loading has occurred. 062 * <p> 063 * Ebean has historically ignored the case of keys hence returning the Properties with all the keys lower cased. 064 * </p> 065 */ 066 public Properties asPropertiesLowerCase() { 067 return properties; 068 } 069 070 /** 071 * Get a property with no default value. 072 */ 073 public String get(String key) { 074 return get(key, null); 075 } 076 077 /** 078 * Get a property with a default value. 079 * <p> 080 * This performs a search using the prefix and server name (if supplied) to search for the property 081 * value in order based on: 082 * <pre>{@code 083 * prefix.serverName.key 084 * prefix.key 085 * key 086 * }</pre> 087 * </p> 088 */ 089 public String get(String key, String defaultValue) { 090 091 String value = null; 092 if (serverName != null && prefix != null) { 093 value = propertyMap.get(prefix + "." + serverName + "." + key, null); 094 } 095 if (value == null && prefix != null) { 096 value = propertyMap.get(prefix + "." + key, null); 097 } 098 if (value == null) { 099 value = propertyMap.get(key, null); 100 } 101 return value == null ? defaultValue : value; 102 } 103 104 /** 105 * Return a double property value. 106 */ 107 public double getDouble(String key, double defaultValue) { 108 109 String value = get(key, String.valueOf(defaultValue)); 110 return Double.parseDouble(value); 111 } 112 113 /** 114 * Return an int property value. 115 */ 116 public int getInt(String key, int defaultValue) { 117 118 String value = get(key, String.valueOf(defaultValue)); 119 return Integer.parseInt(value); 120 } 121 122 /** 123 * Return a long property value. 124 */ 125 public long getLong(String key, long defaultValue) { 126 127 String value = get(key, String.valueOf(defaultValue)); 128 return Long.parseLong(value); 129 } 130 131 /** 132 * Return a boolean property value. 133 */ 134 public boolean getBoolean(String key, boolean defaultValue) { 135 136 String value = get(key, String.valueOf(defaultValue)); 137 return Boolean.parseBoolean(value); 138 } 139 140 /** 141 * Return a Enum property value. 142 */ 143 public <T extends Enum<T>> T getEnum(Class<T> enumType, String key, T defaultValue) { 144 String level = get(key, null); 145 return (level == null) ? defaultValue : Enum.valueOf(enumType, level.toUpperCase()); 146 } 147 148}