#1246 - Support use of application.yml and application-test.yml ... (as alternative to ebean.properties, test-ebean.properties)

This commit is contained in:
Rob Bygrave committed 2018-02-26 08:35:47 +13:00
1 parent 75462e7410
commit 8e9843ab8a
25 files changed
+788 -615

No files matched your search

+10
View File
@@ -83,6 +83,16 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!--
If snakeyaml is in the classpath we will read yml configuration files
-->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.19</version>
<scope>provided</scope>
</dependency>
<!-- <!--
Class retention Nonnull and Nullable annotations Class retention Nonnull and Nullable annotations
to assist with IDE auto-completion with Ebean API to assist with IDE auto-completion with Ebean API
+2 -2
View File
@@ -1,6 +1,6 @@
package io.ebean; package io.ebean;
import io.ebean.config.PropertyMap; import io.ebean.config.properties.PropertiesLoader;
import java.util.Properties; import java.util.Properties;
@@ -44,7 +44,7 @@ class PrimaryServer {
*/ */
static synchronized Properties getProperties() { static synchronized Properties getProperties() {
if (globalProperties == null) { if (globalProperties == null) {
globalProperties = PropertyMap.defaultProperties(); globalProperties = PropertiesLoader.load();
} }
if (defaultServerName == null) { if (defaultServerName == null) {
defaultServerName = determineDefaultServerName(); defaultServerName = determineDefaultServerName();
@@ -10,16 +10,13 @@ public class PropertiesWrapper {
protected final String serverName; protected final String serverName;
protected final PropertyMap propertyMap;
/** /**
* Construct with a prefix, serverName and properties. * Construct with a prefix, serverName and properties.
*/ */
public PropertiesWrapper(String prefix, String serverName, Properties properties) { public PropertiesWrapper(String prefix, String serverName, Properties properties) {
this.serverName = serverName; this.serverName = serverName;
this.prefix = prefix; this.prefix = prefix;
this.propertyMap = PropertyMapLoader.load(null, properties); this.properties = properties;
this.properties = propertyMap.asProperties();
} }
/** /**
@@ -29,27 +26,6 @@ public class PropertiesWrapper {
this(null, null, properties); this(null, null, properties);
} }
/**
* Internal copy constructor when changing prefix.
*/
protected PropertiesWrapper(String prefix, String serverName, PropertyMap propertyMap, Properties properties) {
this.serverName = serverName;
this.prefix = prefix;
this.propertyMap = propertyMap;
this.properties = properties;
}
/**
* Return a PropertiesWrapper instance with a different prefix but same underlying properties.
* <p/>
* Used when wanting to use "datasource" as the prefix rather than "ebean".
* <p/>
* The returning instance should only be used in a read only fashion.
*/
public PropertiesWrapper withPrefix(String prefix) {
return new PropertiesWrapper(prefix, serverName, propertyMap, properties);
}
/** /**
* Return the serverName (optional). * Return the serverName (optional).
*/ */
@@ -57,16 +33,6 @@ public class PropertiesWrapper {
return serverName; return serverName;
} }
/**
* Return as Properties with lower case keys and after evaluation and additional properties loading has occurred.
* <p>
* Ebean has historically ignored the case of keys hence returning the Properties with all the keys lower cased.
* </p>
*/
public Properties asPropertiesLowerCase() {
return properties;
}
/** /**
* Get a property with no default value. * Get a property with no default value.
*/ */
@@ -90,17 +56,21 @@ public class PropertiesWrapper {
String value = null; String value = null;
if (serverName != null && prefix != null) { if (serverName != null && prefix != null) {
value = propertyMap.get(prefix + "." + serverName + "." + key, null); value = internalGet(prefix + "." + serverName + "." + key);
} }
if (value == null && prefix != null) { if (value == null && prefix != null) {
value = propertyMap.get(prefix + "." + key, null); value = internalGet(prefix + "." + key);
} }
if (value == null) { if (value == null) {
value = propertyMap.get(key, null); value = internalGet(key);
} }
return value == null ? defaultValue : value; return value == null ? defaultValue : value;
} }
private String internalGet(String key) {
return properties.getProperty(key);
}
/** /**
* Return a double property value. * Return a double property value.
*/ */
@@ -1,178 +0,0 @@
package io.ebean.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* Helper used to evaluate expressions such as ${CATALINA_HOME}.
* <p>
* The expressions can contain environment variables, system properties or JNDI
* properties. JNDI expressions take the form ${jndi:propertyName} where you
* substitute propertyName with the name of the jndi property you wish to
* evaluate.
* </p>
*/
final class PropertyExpression {
private static final Logger logger = LoggerFactory.getLogger(PropertyExpression.class);
/**
* Prefix for looking up JNDI Environment variable.
*/
private static final String JAVA_COMP_ENV = "java:comp/env/";
/**
* Used to detect the start of an expression.
*/
private static final String START = "${";
/**
* Used to detect the end of an expression.
*/
private static final String END = "}";
/**
* Specify the PropertyHolder.
*/
private PropertyExpression() {
}
/**
* Return the property value evaluating and replacing any expressions such as
* ${CATALINA_HOME}.
*/
static String eval(String val, PropertyMap map) {
if (val == null) {
return null;
}
int sp = val.indexOf(START);
if (sp > -1) {
int ep = val.indexOf(END, sp + 1);
if (ep > -1) {
return eval(val, sp, ep, map);
}
}
return val;
}
/**
* Convert the expression using JNDI, Environment variables, System Properties
* or existing an property in SystemProperties itself.
*/
private static String evaluateExpression(String exp, PropertyMap map) {
if (isJndiExpression(exp)) {
// JNDI property lookup...
String val = getJndiProperty(exp);
if (val != null) {
return val;
}
}
// check Environment Variables first
String val = System.getenv(exp);
if (val == null) {
// then check system properties
val = System.getProperty(exp);
}
if (val == null && map != null) {
// then check PropertyMap
val = map.get(exp);
}
if (val != null) {
return val;
} else {
// unable to evaluate yet... but maybe later based on the order
// in which properties are being set/loaded. You can use
// GlobalProperties.evaluateExpressions() to get any unresolved
// expressions to be evaluated
logger.debug("Unable to evaluate expression [{}]", exp);
return null;
}
}
private static String eval(String val, int sp, int ep, PropertyMap map) {
StringBuilder sb = new StringBuilder();
sb.append(val.substring(0, sp));
String cal = evalExpression(val, sp, ep, map);
sb.append(cal);
eval(val, ep + 1, sb, map);
return sb.toString();
}
private static void eval(String val, int startPos, StringBuilder sb, PropertyMap map) {
if (startPos < val.length()) {
int sp = val.indexOf(START, startPos);
if (sp > -1) {
// append what is between the last token and the new one (if startPos ==
// sp nothing gets added)
sb.append(val.substring(startPos, sp));
int ep = val.indexOf(END, sp + 1);
if (ep > -1) {
String cal = evalExpression(val, sp, ep, map);
sb.append(cal);
eval(val, ep + 1, sb, map);
return;
}
}
}
// append what is left...
sb.append(val.substring(startPos));
}
private static String evalExpression(String val, int sp, int ep, PropertyMap map) {
// trim off start and end ${ and }
String exp = val.substring(sp + START.length(), ep);
// evaluate the variable
String evaled = evaluateExpression(exp, map);
if (evaled != null) {
return evaled;
} else {
// unable to evaluate at this stage (maybe later)
return START + exp + END;
}
}
private static boolean isJndiExpression(String exp) {
return exp.startsWith("JNDI:") || exp.startsWith("jndi:");
}
/**
* Returns null if JNDI is not setup or if the property is not found.
*
* @param key the key of the JNDI Environment property including a JNDI: prefix.
*/
private static String getJndiProperty(String key) {
try {
// remove the JNDI: prefix
key = key.substring(5);
return (String) getJndiObject(key);
} catch (NamingException ex) {
return null;
}
}
/**
* Similar to getProperty but throws NamingException if JNDI is not setup or
* if the property is not found.
*/
private static Object getJndiObject(String key) throws NamingException {
InitialContext ctx = new InitialContext();
return ctx.lookup(JAVA_COMP_ENV + key);
}
}
@@ -1,168 +0,0 @@
package io.ebean.config;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
/**
* A map like structure of properties.
* <p/>
* Handles evaluation of expressions like ${home} and provides convenience methods for int, long and boolean.
*/
public final class PropertyMap implements Serializable {
private static final long serialVersionUID = 1L;
private final LinkedHashMap<String, String> map = new LinkedHashMap<>();
/**
* Return properties loaded from test-ebean.properties.
*/
public static Properties testProperties() {
PropertyMap propertyMap = PropertyMapLoader.loadTestProperties();
return (propertyMap == null) ? new Properties() : propertyMap.asProperties();
}
public static Properties defaultProperties() {
PropertyMap propertyMap = PropertyMapLoader.loadGlobalProperties();
return (propertyMap == null) ? new Properties() : propertyMap.asProperties();
}
public static boolean loadTestProperties() {
String disable = System.getProperty("disableTestProperties");
return (disable == null || !disable.equalsIgnoreCase("true"));
}
@Override
public String toString() {
return map.toString();
}
/**
* Return as standard Properties.
*/
public Properties asProperties() {
Properties properties = new Properties();
for (Entry<String, String> e : entrySet()) {
properties.put(e.getKey(), e.getValue());
}
return properties;
}
/**
* Go through all the properties and evaluate any expressions that have not
* been resolved.
*/
public void evaluateProperties() {
for (Entry<String, String> e : entrySet()) {
String key = e.getKey();
String val = e.getValue();
String eval = eval(val);
if (eval != null && !eval.equals(val)) {
put(key, eval);
}
}
}
/**
* Returns the value with expressions like ${home} evaluated using system properties and environment variables.
*/
public synchronized String eval(String val) {
return PropertyExpression.eval(val, this);
}
/**
* Return the boolean property value with a given default.
*/
public synchronized boolean getBoolean(String key, boolean defaultValue) {
String value = get(key);
if (value == null) {
return defaultValue;
} else {
return Boolean.parseBoolean(value);
}
}
/**
* Return the int property value with a given default.
*/
public synchronized int getInt(String key, int defaultValue) {
String value = get(key);
if (value == null) {
return defaultValue;
} else {
return Integer.parseInt(value);
}
}
/**
* Return the long property value with a given default.
*/
public synchronized long getLong(String key, long defaultValue) {
String value = get(key);
if (value == null) {
return defaultValue;
} else {
return Long.parseLong(value);
}
}
/**
* Return the string property value with a given default.
*/
public synchronized String get(String key, String defaultValue) {
String value = map.get(key.toLowerCase());
return value == null ? defaultValue : value;
}
/**
* Return the property value returning null if there is no value defined.
*/
public synchronized String get(String key) {
return map.get(key.toLowerCase());
}
/**
* Put all evaluating any expressions in the values.
*/
public synchronized void putEvalAll(Map<String, String> keyValueMap) {
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
putEval(entry.getKey(), entry.getValue());
}
}
/**
* Put a single key value evaluating any expressions in the value.
*/
public synchronized String putEval(String key, String value) {
value = PropertyExpression.eval(value, this);
return map.put(key.toLowerCase(), value);
}
/**
* Put a single key value with no expression evaluation.
*/
public synchronized String put(String key, String value) {
return map.put(key.toLowerCase(), value);
}
/**
* Remove an entry.
*/
public synchronized String remove(String key) {
return map.remove(key.toLowerCase());
}
/**
* Return the entries.
*/
public synchronized Set<Entry<String, String>> entrySet() {
return map.entrySet();
}
}
@@ -1,163 +0,0 @@
package io.ebean.config;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
/**
* Helper used to load the ebean.properties into a PropertyMap.
*/
final class PropertyMapLoader {
private static final Logger logger = LoggerFactory.getLogger(PropertyMapLoader.class);
private static final Pattern OTHER_PROPS_REPLACE = Pattern.compile("\\", Pattern.LITERAL);
/**
* Load the <code>test-ebean.properties</code>.
*/
public static PropertyMap loadTestProperties() {
if (!PropertyMap.loadTestProperties()) {
return new PropertyMap();
}
return load(null, "test-ebean.properties");
}
/**
* Load the ebean.properties (and test-ebean.properties if present).
*/
public static PropertyMap loadGlobalProperties() {
boolean loadTestProperties = false;
String fileName = System.getenv("EBEAN_PROPS_FILE");
if (fileName == null) {
fileName = System.getProperty("ebean.props.file");
if (fileName == null) {
loadTestProperties = PropertyMap.loadTestProperties();
fileName = "ebean.properties";
}
}
PropertyMap map = load(null, fileName);
if (loadTestProperties) {
// load test properties if present in classpath
map = load(map, "test-ebean.properties");
}
return map;
}
/**
* Load the file returning the property map.
*
* @param p an existing property map to load into.
* @param fileName the name of the properties file to load.
*/
public static PropertyMap load(PropertyMap p, String fileName) {
InputStream is = findInputStream(fileName);
if (is == null) {
return p;
} else {
return load(p, is);
}
}
/**
* Load the InputStream returning the property map.
*
* @param p an existing property map to load into.
* @param in the InputStream of the properties file to load.
*/
public static PropertyMap load(PropertyMap p, InputStream in) {
Properties props = new Properties();
try {
props.load(in);
in.close();
return load(p, props);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static PropertyMap load(PropertyMap p, Properties props) {
if (p == null) {
p = new PropertyMap();
}
// put values in initially without any evaluation
for (Map.Entry<Object, Object> entry : props.entrySet()) {
String key = ((String) entry.getKey()).toLowerCase();
String val = ((String) entry.getValue());
if (val != null) {
val = val.trim();
}
p.put(key, val);
}
p.evaluateProperties();
String otherProps = p.remove("load.properties");
if (otherProps == null) {
otherProps = p.remove("load.properties.override");
}
if (otherProps != null) {
otherProps = OTHER_PROPS_REPLACE.matcher(otherProps).replaceAll(Matcher.quoteReplacement("/"));
InputStream is = findInputStream(otherProps);
if (is != null) {
logger.debug("loading properties from {}", otherProps);
load(p, is);
} else {
logger.error("load.properties " + otherProps + " not found.");
}
}
return p;
}
/**
* Find the input stream given the file name.
*/
private static InputStream findInputStream(String fileName) {
if (fileName == null) {
throw new NullPointerException("fileName is null?");
}
try {
File f = new File(fileName);
if (f.exists()) {
logger.debug("{} found in file system", fileName);
return new FileInputStream(f);
} else {
InputStream in = findInClassPath(fileName);
if (in != null) {
logger.debug("{} found in classpath", fileName);
}
return in;
}
} catch (FileNotFoundException ex) {
// already made the check so this
// should never be thrown
throw new RuntimeException(ex);
}
}
private static InputStream findInClassPath(String fileName) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
}
}
@@ -13,6 +13,7 @@ import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.DbEncrypt; import io.ebean.config.dbplatform.DbEncrypt;
import io.ebean.config.dbplatform.DbType; import io.ebean.config.dbplatform.DbType;
import io.ebean.config.dbplatform.IdType; import io.ebean.config.dbplatform.IdType;
import io.ebean.config.properties.PropertiesLoader;
import io.ebean.event.BeanFindController; import io.ebean.event.BeanFindController;
import io.ebean.event.BeanPersistController; import io.ebean.event.BeanPersistController;
import io.ebean.event.BeanPersistListener; import io.ebean.event.BeanPersistListener;
@@ -2576,7 +2577,7 @@ public class ServerConfig {
* Load settings from ebean.properties. * Load settings from ebean.properties.
*/ */
public void loadFromProperties() { public void loadFromProperties() {
loadFromProperties(PropertyMap.defaultProperties()); loadFromProperties(PropertiesLoader.load());
} }
/** /**
@@ -2590,18 +2591,17 @@ public class ServerConfig {
} }
/** /**
* Deprecated - this does nothing now, we always try to read test configuration.
* <p>
* Load settings from test-ebean.properties and do nothing if the properties is not found. * Load settings from test-ebean.properties and do nothing if the properties is not found.
* <p> * <p>
* This is typically used when test-ebean.properties is put into the test class path and used * This is typically used when test-ebean.properties is put into the test class path and used
* to configure Ebean for running tests. * to configure Ebean for running tests.
* </p> * </p>
*/ */
@Deprecated
public void loadTestProperties() { public void loadTestProperties() {
Properties properties = PropertyMap.testProperties(); // do nothing now ... as we always try to read test configuration and that should only
if (!properties.isEmpty()) {
PropertiesWrapper p = new PropertiesWrapper("ebean", name, properties);
loadSettings(p);
}
} }
/** /**
@@ -0,0 +1,97 @@
package io.ebean.config.properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* Manages the underlying map of properties we are gathering.
*/
class LoadContext {
private static final Logger log = LoggerFactory.getLogger(LoadContext.class);
/**
* Map we are loading the properties into.
*/
private final Map<String, String> map = new LinkedHashMap<>();
/**
* Names of resources/files that were loaded.
*/
private final Set<String> loadedResources = new LinkedHashSet<>();
/**
* Return the input stream (maybe null) for the given source.
*/
InputStream resource(String resourcePath, Loader.Source source) {
InputStream is = null;
if (source == Loader.Source.RESOURCE) {
is = getClass().getResourceAsStream("/" + resourcePath);
if (is != null) {
loadedResources.add(resourcePath);
}
} else {
File file = new File(resourcePath);
if (file.exists()) {
try {
is = new FileInputStream(file);
loadedResources.add("file:"+resourcePath);
} catch (FileNotFoundException e) {
throw new IllegalStateException(e);
}
}
}
return is;
}
/**
* Add a property entry.
*/
void put(String key, String val) {
if (val != null) {
val = val.trim();
}
map.put(key, val);
}
/**
* Evaluate all the expressions and return as a Properties object.
*/
Properties eval() {
log.info("loaded properties from {}", loadedResources);
Properties properties = new Properties();
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = PropertyEval.eval(entry.getValue());
properties.setProperty(key, value);
}
return properties;
}
/**
* Read the special properties that can point to an external properties source.
*/
String indirectLocation() {
String indirectLocation = map.get("load.properties");
if (indirectLocation == null) {
indirectLocation = map.get("load.properties.override");
}
return indirectLocation;
}
}
@@ -0,0 +1,164 @@
package io.ebean.config.properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
/**
* Loads the configuration from known/expected locations.
* <p>
* Defines the loading order of resources and files.
* </p>
*/
class Loader {
private static final Logger log = LoggerFactory.getLogger(Loader.class);
enum Source {
RESOURCE,
FILE
}
private final LoadContext loadContext = new LoadContext();
private YamlLoader yamlLoader;
Loader() {
try {
Class<?> exists = Class.forName("org.yaml.snakeyaml.Yaml");
if (exists != null) {
yamlLoader = new YamlLoader(loadContext);
}
} catch (Exception e) {
// ignored
}
}
/**
* Load the configuration with the expected ordering.
*/
void load() {
loadMain(Source.RESOURCE);
// external file configuration overrides the resources configuration
loadMain(Source.FILE);
loadViaSystemProperty();
loadViaIndirection();
// test configuration (if found) overrides main configuration
// we should only find these resources when running tests
loadTest();
}
/**
* Load test configuration.
*/
private void loadTest() {
loadProperties("application-test.properties", Source.RESOURCE);
loadYaml("application-test.yml", Source.RESOURCE);
loadProperties("test-ebean.properties", Source.RESOURCE);
}
/**
* Load configuration defined by a <em>load.properties</em> entry in properties file.
*/
private void loadViaIndirection() {
String location = loadContext.indirectLocation();
if (location != null) {
location = PropertyEval.eval(location);
loadWithExtensionCheck(location);
}
}
/**
* Load the main configuration for the given source.
*/
private void loadMain(Source source) {
loadYaml("application.yml", source);
loadProperties("application.properties", source);
loadProperties("ebean.properties", source);
}
private void loadViaSystemProperty() {
String fileName = System.getenv("EBEAN_PROPS_FILE");
if (fileName == null) {
fileName = System.getProperty("ebean.props.file");
if (fileName != null) {
loadWithExtensionCheck(fileName);
}
}
}
void loadWithExtensionCheck(String fileName) {
if (fileName.endsWith("yml")) {
loadYaml(fileName, Source.FILE);
} else if (fileName.endsWith("properties")) {
loadProperties(fileName, Source.FILE);
} else {
throw new IllegalArgumentException("Expecting only yml or properties file but got [" + fileName + "]");
}
}
/**
* Evaluate all the configuration entries and return as properties.
*/
Properties eval() {
return loadContext.eval();
}
void loadYaml(String resourcePath, Source source) {
if (yamlLoader != null) {
yamlLoader.load(resource(resourcePath, source));
}
}
void loadProperties(String resourcePath, Source source) {
InputStream is = resource(resourcePath, source);
if (is != null) {
loadProperties(is);
}
}
private InputStream resource(String resourcePath, Source source) {
return loadContext.resource(resourcePath, source);
}
private void loadProperties(InputStream is) {
if (is != null) {
try {
Properties properties = new Properties();
properties.load(is);
put(properties);
} catch (IOException e) {
throw new RuntimeException("Failed to load properties?", e);
} finally {
close(is);
}
}
}
private void put(Properties properties) {
Enumeration<?> enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
String property = properties.getProperty(key);
loadContext.put(key, property);
}
}
private void close(InputStream is) {
try {
is.close();
} catch (IOException e) {
log.warn("Error closing input stream for properties", e);
}
}
}
@@ -0,0 +1,58 @@
package io.ebean.config.properties;
import java.util.Properties;
/**
* Loads and evaluates properties and yml configuration.
*/
public class PropertiesLoader {
private static Properties properties;
/**
* Provides properties by reading known locations.
* <p>
* <h3>Main configuration</h3>
* <p>
* <p>Firstly loads from main resources</p>
* <pre>
* - application.properties
* - application.yml
* - ebean.properties
* </pre>
* <p>
* <p>Then loads from local files</p>
* <pre>
* - application.properties
* - application.yml
* - ebean.properties
* </pre>
* <p>
* <p>Then loads from environment variable <em>EBEAN_PROPS_FILE</em></p>
* <p>Then loads from system property <em>ebean.props.file</em></p>
* <p>Then loads from <em>load.properties</em></p>
* <p>
* <h3>Test configuration</h3>
* <p>
* Once the main configuration is read it will try to read common test configuration.
* This will only be successful if the test resources are available (i.e. running tests).
* </p>
* <p>Loads from test resources</p>
* <pre>
* - application-test.properties
* - application-test.yml
* - test-ebean.properties
* </pre>
*/
public static synchronized Properties load() {
if (properties == null) {
Loader loader = new Loader();
loader.load();
properties = loader.eval();
}
return properties;
}
}
@@ -0,0 +1,202 @@
package io.ebean.config.properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* Helper used to evaluate expressions such as ${CATALINA_HOME}.
* <p>
* The expressions can contain environment variables, system properties or JNDI
* properties. JNDI expressions take the form ${jndi:propertyName} where you
* substitute propertyName with the name of the jndi property you wish to
* evaluate.
* </p>
*/
final class PropertyEval {
/**
* Prefix for looking up JNDI Environment variable.
*/
private static final String JAVA_COMP_ENV = "java:comp/env/";
/**
* Used to detect the start of an expression.
*/
private static final String START = "${";
/**
* Used to detect the end of an expression.
*/
private static final String END = "}";
/**
* Specify the PropertyHolder.
*/
private PropertyEval() {
}
/**
* Return the property value evaluating and replacing any expressions such as
* ${CATALINA_HOME}.
*/
static String eval(String val) {
if (val == null) {
return null;
}
int sp = val.indexOf(START);
if (sp > -1) {
int ep = val.indexOf(END, sp + 1);
if (ep > -1) {
return eval(val, sp, ep);
}
}
return val;
}
/**
* Convert the expression using JNDI, Environment variables, System Properties
* or existing an property in SystemProperties itself.
*/
private static String evaluateExpression(String exp) {
if (isJndiExpression(exp)) {
// JNDI property lookup...
String val = getJndiProperty(exp);
if (val != null) {
return val;
}
}
String val = System.getProperty(exp);
if (val == null) {
val = System.getenv(exp);
}
return val;
}
private static String eval(String val, int sp, int ep) {
return new EvalBuffer(val, sp, ep).process();
}
private static boolean isJndiExpression(String exp) {
return exp.startsWith("JNDI:") || exp.startsWith("jndi:");
}
/**
* Returns null if JNDI is not setup or if the property is not found.
*
* @param key the key of the JNDI Environment property including a JNDI: prefix.
*/
private static String getJndiProperty(String key) {
try {
// remove the JNDI: prefix
key = key.substring(5);
return (String) getJndiObject(key);
} catch (NamingException ex) {
return null;
}
}
/**
* Similar to getProperty but throws NamingException if JNDI is not setup or
* if the property is not found.
*/
private static Object getJndiObject(String key) throws NamingException {
InitialContext ctx = new InitialContext();
return ctx.lookup(JAVA_COMP_ENV + key);
}
private static class EvalBuffer {
private final StringBuilder buf = new StringBuilder();
private final String original;
private int position;
private int start;
private int end;
private String expression;
private String defaultValue;
EvalBuffer(String val, int start, int ep) {
this.original = val;
this.start = start;
this.end = ep;
this.position = 0;
moveToStart();
}
void moveToStart() {
if (start > position) {
buf.append(original.substring(position, start));
position = start;
}
}
void parseForDefault() {
int colonPos = original.lastIndexOf(':', end);
if (colonPos > start) {
expression = original.substring(start + START.length(), colonPos);
defaultValue = original.substring(colonPos + 1, end);
} else {
expression = original.substring(start + START.length(), end);
}
}
void evaluate() {
String eval = evaluateExpression(expression);
if (eval != null) {
buf.append(eval);
} else {
if (defaultValue != null) {
buf.append(defaultValue);
} else {
buf.append(START).append(expression).append(END);
}
}
}
String end() {
if (end < original.length() - 1) {
buf.append(original.substring(end + 1));
}
return buf.toString();
}
boolean next() {
if (end < original.length()) {
int startPos = original.indexOf(START, end + 1);
if (startPos > -1) {
int endPos = original.indexOf(END, startPos + 1);
if (endPos > -1) {
if (startPos > end + 1) {
buf.append(original.substring(end + 1, startPos));
}
this.start = startPos;
this.end = endPos;
return true;
}
}
}
return false;
}
private void evalNext() {
parseForDefault();
evaluate();
}
String process() {
evalNext();
while (next()) {
evalNext();
}
return end();
}
}
}
@@ -0,0 +1,54 @@
package io.ebean.config.properties;
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.util.Map;
/**
* Loads configuration from Yml into the load context.
* <p>
* Note that this ignores 'lists' so just reads 'maps' and scalar values.
* </p>
*/
class YamlLoader {
private final Yaml yaml = new Yaml();
private final LoadContext loadContext;
YamlLoader(LoadContext loadContext) {
this.loadContext = loadContext;
}
void load(InputStream is) {
if (is != null) {
loadMap(yaml.load(is), null);
}
}
void loadMap(Map<String, Object> map, String path) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
if (path != null) {
key = path + "." + key;
}
Object val = entry.getValue();
if (val instanceof Map) {
loadMap((Map<String, Object>) val, key);
} else {
addScalar(key, val);
}
}
}
private void addScalar(String key, Object val) {
if (val instanceof String) {
loadContext.put(key, (String) val);
} else if (val instanceof Number || val instanceof Boolean) {
loadContext.put(key, val.toString());
}
}
}
@@ -5,12 +5,12 @@ import io.ebean.cache.ServerCacheFactory;
import io.ebean.cache.ServerCacheOptions; import io.ebean.cache.ServerCacheOptions;
import io.ebean.cache.ServerCachePlugin; import io.ebean.cache.ServerCachePlugin;
import io.ebean.config.ContainerConfig; import io.ebean.config.ContainerConfig;
import io.ebean.config.PropertyMap;
import io.ebean.config.ServerConfig; import io.ebean.config.ServerConfig;
import io.ebean.config.TenantMode; import io.ebean.config.TenantMode;
import io.ebean.config.UnderscoreNamingConvention; import io.ebean.config.UnderscoreNamingConvention;
import io.ebean.config.dbplatform.DatabasePlatform; import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.h2.H2Platform; import io.ebean.config.dbplatform.h2.H2Platform;
import io.ebean.config.properties.PropertiesLoader;
import io.ebean.service.SpiContainer; import io.ebean.service.SpiContainer;
import io.ebeaninternal.api.SpiBackgroundExecutor; import io.ebeaninternal.api.SpiBackgroundExecutor;
import io.ebeaninternal.api.SpiContainerBootup; import io.ebeaninternal.api.SpiContainerBootup;
@@ -37,7 +37,6 @@ import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Properties;
import java.util.ServiceLoader; import java.util.ServiceLoader;
/** /**
@@ -84,9 +83,7 @@ public class DefaultContainer implements SpiContainer {
ServerConfig config = new ServerConfig(); ServerConfig config = new ServerConfig();
config.setName(name); config.setName(name);
config.loadFromProperties(PropertiesLoader.load());
Properties prop = PropertyMap.defaultProperties();
config.loadFromProperties(prop);
return createServer(config); return createServer(config);
} }
@@ -12,7 +12,7 @@ public class PropertiesWrapperTest {
@Test @Test
public void testGetServerName() throws Exception { public void testGetServerName() {
PropertiesWrapper pw = new PropertiesWrapper(null, "myserver", new Properties()); PropertiesWrapper pw = new PropertiesWrapper(null, "myserver", new Properties());
assertEquals("myserver", pw.getServerName()); assertEquals("myserver", pw.getServerName());
@@ -35,23 +35,20 @@ public class PropertiesWrapperTest {
Properties properties = new Properties(); Properties properties = new Properties();
properties.put("someBasic", " hello "); properties.put("someBasic", " hello ");
properties.put("someInt", " 42 "); properties.put("someInt", "42");
properties.put("noTrimReqr", "jim"); properties.put("noTrimReqr", "jim");
properties.put("includeSpaces", " jim bob "); properties.put("includeSpaces", " jim bob ");
PropertiesWrapper pw = new PropertiesWrapper("pref", "myserver", properties); PropertiesWrapper pw = new PropertiesWrapper("pref", "myserver", properties);
assertEquals("hello", pw.get("someBasic")); assertEquals(" hello ", pw.get("someBasic"));
assertEquals(42, pw.getInt("someInt", 1)); assertEquals(42, pw.getInt("someInt", 1));
assertNull(pw.get("doesNotExist", null)); assertNull(pw.get("doesNotExist", null));
assertEquals("jim", pw.get("noTrimReqr")); assertEquals("jim", pw.get("noTrimReqr"));
assertEquals("jim bob", pw.get("includeSpaces")); assertEquals(" jim bob ", pw.get("includeSpaces"));
} }
@Test @Test
public void testGetProperties() throws Exception { public void testGetProperties() {
String home = System.getenv("HOME");
String tmpDir = System.getProperty("java.io.tmpdir");
Properties properties = new Properties(); Properties properties = new Properties();
properties.put("someBasic", "hello"); properties.put("someBasic", "hello");
@@ -64,28 +61,12 @@ public class PropertiesWrapperTest {
assertEquals(42, pw.getInt("someInt", 99)); assertEquals(42, pw.getInt("someInt", 99));
assertEquals(Double.valueOf(5.5D), (Double.valueOf(pw.getDouble("someDouble", 99.9D)))); assertEquals(Double.valueOf(5.5D), (Double.valueOf(pw.getDouble("someDouble", 99.9D))));
assertEquals(home + "/hello", pw.get("somePath", null));
assertEquals(tmpDir, "/aaa/" + tmpDir + "/bbb", pw.get("someSystemProp"));
Properties properties1 = pw.asPropertiesLowerCase();
assertEquals("hello", properties1.getProperty("somebasic"));
assertEquals("42", properties1.getProperty("someint"));
assertEquals(home + "/hello", properties1.get("somepath"));
assertEquals(tmpDir, "/aaa/" + tmpDir + "/bbb", properties1.get("somesystemprop"));
pw = new PropertiesWrapper(properties); pw = new PropertiesWrapper(properties);
assertEquals(42, pw.getInt("someInt", 99)); assertEquals(42, pw.getInt("someInt", 99));
assertEquals(Double.valueOf(5.5D), (Double.valueOf(pw.getDouble("someDouble", 99.9D)))); assertEquals(Double.valueOf(5.5D), (Double.valueOf(pw.getDouble("someDouble", 99.9D))));
assertEquals(home + "/hello", pw.get("somePath", null));
assertEquals(tmpDir, "/aaa/" + tmpDir + "/bbb", pw.get("someSystemProp"));
properties1 = pw.asPropertiesLowerCase();
assertEquals("hello", properties1.getProperty("somebasic"));
assertEquals("42", properties1.getProperty("someint"));
assertEquals(home + "/hello", properties1.get("somepath"));
assertEquals(tmpDir, "/aaa/" + tmpDir + "/bbb", properties1.get("somesystemprop"));
} }
} }
@@ -1,23 +0,0 @@
package io.ebean.config;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class PropertyMapTest {
@Test
public void testDefaultProperties() throws Exception {
PropertyMap.defaultProperties();
}
@Test
public void testEval() throws Exception {
String home = System.getenv("HOME");
PropertyMap map = new PropertyMap();
assertEquals(home, map.eval("${HOME}"));
}
}
@@ -0,0 +1,65 @@
package io.ebean.config.properties;
import org.junit.Test;
import java.util.Properties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
public class LoaderTest {
@Test
public void load() {
Loader loader = new Loader();
loader.loadProperties("test-properties/application.properties", Loader.Source.RESOURCE);
loader.loadYaml("test-properties/application.yml", Loader.Source.RESOURCE);
loader.loadProperties("test-properties/one.properties", Loader.Source.RESOURCE);
loader.loadYaml("test-properties/foo.yml", Loader.Source.RESOURCE);
Properties properties = loader.eval();
assertEquals("fromProperties", properties.getProperty("app.fromProperties"));
assertEquals("Two", properties.getProperty("app.two"));
assertEquals("bart", properties.getProperty("eval.withDefault"));
assertEquals("/home/rob/after", properties.getProperty("eval.home"));
assertEquals("rob", properties.getProperty("eval.name"));
}
@Test
public void loadWithExtensionCheck() {
Loader loader = new Loader();
loader.loadWithExtensionCheck("test-dummy.properties");
loader.loadWithExtensionCheck("test-dummy.yml");
Properties properties = loader.eval();
assertThat(properties.getProperty("dummy.yml.foo")).isEqualTo("bar");
assertThat(properties.getProperty("dummy.properties.foo")).isEqualTo("bar");
}
@Test
public void loadYaml() {
Loader loader = new Loader();
loader.loadYaml("test-properties/foo.yml", Loader.Source.RESOURCE);
Properties properties = loader.eval();
assertThat(properties.getProperty("ebean.oracle.password")).isEqualTo("someDefault");
}
@Test
public void loadProperties() {
Loader loader = new Loader();
loader.loadProperties("test-properties/one.properties", Loader.Source.RESOURCE);
Properties properties = loader.eval();
assertThat(properties.getProperty("hello")).isEqualTo("there");
assertThat(properties.getProperty("name")).isEqualTo("Rob");
}
}
@@ -0,0 +1,73 @@
package io.ebean.config.properties;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class PropertyEvalTest {
@Test
public void eval_null() {
assertNull(PropertyEval.eval(null));
}
@Test
public void eval_empty() {
assertEquals("", PropertyEval.eval(""));
}
@Test
public void eval_noExpressions() {
assertEquals("basic", PropertyEval.eval("basic"));
assertEquals("{basic}", PropertyEval.eval("{basic}"));
}
@Test
public void eval_singleExpression() {
System.setProperty("foo", "Hello");
assertEquals("Hello", PropertyEval.eval("${foo}"));
assertEquals("preHello", PropertyEval.eval("pre${foo}"));
assertEquals("HelloPost", PropertyEval.eval("${foo}Post"));
assertEquals("beforeHelloAfter", PropertyEval.eval("before${foo}After"));
System.clearProperty("foo");
}
@Test
public void eval_singleExpression_withDefault() {
System.setProperty("foo", "Hello");
assertEquals("Hello", PropertyEval.eval("${foo:bart}"));
assertEquals("beforeHelloAfter", PropertyEval.eval("before${foo:bart}After"));
assertEquals("preHello", PropertyEval.eval("pre${foo:bart}"));
assertEquals("HelloPost", PropertyEval.eval("${foo:bart}Post"));
System.clearProperty("foo");
assertEquals("bart", PropertyEval.eval("${foo:bart}"));
assertEquals("before-bart-after", PropertyEval.eval("before-${foo:bart}-after"));
assertEquals("pre-bart", PropertyEval.eval("pre-${foo:bart}"));
assertEquals("bart-post", PropertyEval.eval("${foo:bart}-post"));
}
@Test
public void eval_multiExpression_withDefault() {
assertEquals("num1num2", PropertyEval.eval("${one:num1}${two:num2}"));
assertEquals("num1-num2", PropertyEval.eval("${one:num1}-${two:num2}"));
assertEquals("num1abnum2", PropertyEval.eval("${one:num1}ab${two:num2}"));
assertEquals("anum1bcnum2d", PropertyEval.eval("a${one:num1}bc${two:num2}d"));
System.setProperty("one", "first");
System.setProperty("two", "second");
assertEquals("firstsecond", PropertyEval.eval("${one:num1}${two:num2}"));
assertEquals("first-second", PropertyEval.eval("${one:num1}-${two:num2}"));
assertEquals("pre-first-second-post", PropertyEval.eval("pre-${one:num1}-${two:num2}-post"));
assertEquals("AfirstBCsecondD", PropertyEval.eval("A${one:num1}BC${two:num2}D"));
System.clearProperty("one");
System.clearProperty("two");
}
}
@@ -5,14 +5,14 @@ import io.ebean.EbeanServer;
import io.ebean.EbeanServerFactory; import io.ebean.EbeanServerFactory;
import io.ebean.Query; import io.ebean.Query;
import io.ebean.Transaction; import io.ebean.Transaction;
import io.ebean.config.PropertyMap;
import io.ebean.config.ServerConfig; import io.ebean.config.ServerConfig;
import org.tests.model.basic.UTDetail; import io.ebean.config.properties.PropertiesLoader;
import org.tests.model.basic.UTMaster;
import org.avaje.datasource.DataSourceConfig; import org.avaje.datasource.DataSourceConfig;
import org.avaje.datasource.DataSourcePool; import org.avaje.datasource.DataSourcePool;
import org.avaje.datasource.pool.ConnectionPool; import org.avaje.datasource.pool.ConnectionPool;
import org.junit.Test; import org.junit.Test;
import org.tests.model.basic.UTDetail;
import org.tests.model.basic.UTMaster;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
@@ -27,7 +27,7 @@ public class TestAutoCommitDataSource extends BaseTestCase {
@Test @Test
public void test() throws SQLException { public void test() throws SQLException {
Properties properties = PropertyMap.defaultProperties(); Properties properties = PropertiesLoader.load();
DataSourceConfig dsConfig = new DataSourceConfig(); DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.loadSettings(properties, "h2autocommit");//"pg" dsConfig.loadSettings(properties, "h2autocommit");//"pg"
@@ -5,14 +5,14 @@ import io.ebean.EbeanServer;
import io.ebean.EbeanServerFactory; import io.ebean.EbeanServerFactory;
import io.ebean.Query; import io.ebean.Query;
import io.ebean.Transaction; import io.ebean.Transaction;
import io.ebean.config.PropertyMap;
import io.ebean.config.ServerConfig; import io.ebean.config.ServerConfig;
import org.tests.model.basic.UTDetail; import io.ebean.config.properties.PropertiesLoader;
import org.tests.model.basic.UTMaster;
import org.avaje.datasource.DataSourceConfig; import org.avaje.datasource.DataSourceConfig;
import org.avaje.datasource.DataSourcePool; import org.avaje.datasource.DataSourcePool;
import org.avaje.datasource.pool.ConnectionPool; import org.avaje.datasource.pool.ConnectionPool;
import org.junit.Test; import org.junit.Test;
import org.tests.model.basic.UTDetail;
import org.tests.model.basic.UTMaster;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
@@ -27,7 +27,7 @@ public class TestExplicitTransactionMode extends BaseTestCase {
@Test @Test
public void test() throws SQLException { public void test() throws SQLException {
Properties properties = PropertyMap.defaultProperties(); Properties properties = PropertiesLoader.load();
DataSourceConfig dsConfig = new DataSourceConfig(); DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.loadSettings(properties, "h2autocommit");//"h2autocommit","pg" dsConfig.loadSettings(properties, "h2autocommit");//"h2autocommit","pg"
@@ -0,0 +1,2 @@
app.one=Alpha
app.fromProperties=fromProperties
@@ -0,0 +1,8 @@
app:
one: Beta
two: Two
eval:
home: ${user.home}/after
name: ${user.name:bart}
withDefault: ${withDefault:bart}
@@ -0,0 +1,17 @@
ebean:
ddl:
generate: true
run: true
oracle:
username: rob
password: ${db_user:someDefault}
url: something here
driver: com.foo.Bar
mysql:
username: fiona
url: other
maxConnections: 90
@@ -0,0 +1,3 @@
hello=there
name=${some:Rob}
db.driver=${other:com.h2database.Driver}
+1
View File
@@ -0,0 +1 @@
dummy.properties.foo=bar
+3
View File
@@ -0,0 +1,3 @@
dummy:
yml:
foo: bar