Repository: ambari Updated Branches: refs/heads/trunk 61a3ac44d -> 51c0cb4ff
AMBARI-16207 - Provide Ability To Pass JPA / EclipseLink Properties to the DataSource (jonathanhurley) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/51c0cb4f Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/51c0cb4f Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/51c0cb4f Branch: refs/heads/trunk Commit: 51c0cb4ff1d56704265014e890275dd6284bdb8c Parents: 61a3ac4 Author: Jonathan Hurley <jhur...@hortonworks.com> Authored: Mon May 2 14:15:31 2016 -0400 Committer: Jonathan Hurley <jhur...@hortonworks.com> Committed: Tue May 3 09:43:22 2016 -0400 ---------------------------------------------------------------------- .../server/configuration/Configuration.java | 53 ++++++++++++++++++-- .../server/controller/ControllerModule.java | 16 +++--- .../server/configuration/ConfigurationTest.java | 26 ++++++++++ 3 files changed, 81 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/51c0cb4f/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java index 51ada16..bf11bf6 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java @@ -235,6 +235,7 @@ public class Configuration { public static final String SERVER_JDBC_DRIVER_KEY = "server.jdbc.driver"; public static final String SERVER_JDBC_URL_KEY = "server.jdbc.url"; public static final String SERVER_JDBC_PROPERTIES_PREFIX = "server.jdbc.properties."; + public static final String SERVER_PERSISTENCE_PROPERTIES_PREFIX = "server.persistence.properties."; public static final String SERVER_HTTP_REQUEST_HEADER_SIZE = "server.http.request.header.size"; public static final String SERVER_HTTP_RESPONSE_HEADER_SIZE = "server.http.response.header.size"; @@ -726,7 +727,8 @@ public class Configuration { private Map<String, String> agentConfigsMap; private CredentialProvider credentialProvider = null; private volatile boolean credentialProviderInitialized = false; - private Map<String, String> customDbProperties = null; + private Properties customDbProperties = null; + private Properties customPersistenceProperties = null; private Long configLastModifiedDate = 0L; private Map<String, String> databaseConnectorNames = new HashMap<>(); @@ -2004,20 +2006,33 @@ public class Configuration { } /** + * Gets all properties that begin with {@value #SERVER_JDBC_PROPERTIES_PREFIX} + * , removing the prefix. The properties are then pre-pending with + * {@code eclipselink.jdbc.property.} before being returned. + * <p/> + * These properties are used to pass JDBC driver-specific connection + * properties to EclipseLink. + * <p/> + * server.jdbc.properties.loginTimeout -> + * eclipselink.jdbc.property.loginTimeout <br/> + * server.jdbc.properties.oraclecustomname -> + * eclipselink.jdbc.property.oraclecustomname + * * @return custom properties for database connections */ - public Map<String,String> getDatabaseCustomProperties() { + public Properties getDatabaseCustomProperties() { if (null != customDbProperties) { return customDbProperties; } - customDbProperties = new HashMap<String, String>(); + customDbProperties = new Properties(); for (Entry<Object, Object> entry : properties.entrySet()) { String key = entry.getKey().toString(); String val = entry.getValue().toString(); if (key.startsWith(SERVER_JDBC_PROPERTIES_PREFIX)) { - customDbProperties.put(key.substring(SERVER_JDBC_PROPERTIES_PREFIX.length()), val); + key = "eclipselink.jdbc.property." + key.substring(SERVER_JDBC_PROPERTIES_PREFIX.length()); + customDbProperties.put(key, val); } } @@ -2025,6 +2040,36 @@ public class Configuration { } /** + * Gets all properties that begin with + * {@value #SERVER_PERSISTENCE_PROPERTIES_PREFIX} , removing the prefix. These + * properties are used to pass JPA-specific properties to the persistence + * provider (such as EclipseLink). + * <p/> + * server.persistence.properties.eclipselink.jdbc.batch-writing.size=25 -> + * eclipselink.jdbc.batch-writing.size=25 + * + * @return custom properties for database connections + */ + public Properties getPersistenceCustomProperties() { + if (null != customPersistenceProperties) { + return customPersistenceProperties; + } + + customPersistenceProperties = new Properties(); + + for (Entry<Object, Object> entry : properties.entrySet()) { + String key = entry.getKey().toString(); + String val = entry.getValue().toString(); + if (key.startsWith(SERVER_PERSISTENCE_PROPERTIES_PREFIX)) { + key = key.substring(SERVER_PERSISTENCE_PROPERTIES_PREFIX.length()); + customPersistenceProperties.put(key, val); + } + } + + return customPersistenceProperties; + } + + /** * @return Custom property for request header size */ public int getHttpRequestHeaderSize() { http://git-wip-us.apache.org/repos/asf/ambari/blob/51c0cb4f/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java index 7fb93c7..bb2a374 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java @@ -40,8 +40,6 @@ import java.text.MessageFormat; import java.util.Arrays; import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import java.util.Properties; import java.util.Set; @@ -199,15 +197,13 @@ public class ControllerModule extends AbstractModule { DatabaseType databaseType = configuration.getDatabaseType(); LOG.info("Detected {} as the database type from the JDBC URL", databaseType); - // custom jdbc properties - Map<String, String> customProperties = configuration.getDatabaseCustomProperties(); + // custom jdbc driver properties + Properties customDatabaseDriverProperties = configuration.getDatabaseCustomProperties(); + properties.putAll(customDatabaseDriverProperties); - if (0 != customProperties.size()) { - for (Entry<String, String> entry : customProperties.entrySet()) { - properties.setProperty("eclipselink.jdbc.property." + entry.getKey(), - entry.getValue()); - } - } + // custom persistence properties + Properties customPersistenceProperties = configuration.getPersistenceCustomProperties(); + properties.putAll(customPersistenceProperties); switch (configuration.getPersistenceType()) { case IN_MEMORY: http://git-wip-us.apache.org/repos/asf/ambari/blob/51c0cb4f/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java b/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java index f635f1b..7e8c144 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java @@ -755,4 +755,30 @@ public class ConfigurationTest { Assert.assertEquals(false, actual); } + @Test + public void testCustomDatabaseProperties() throws Exception { + final Properties ambariProperties = new Properties(); + final Configuration configuration = new Configuration(ambariProperties); + ambariProperties.setProperty("server.jdbc.properties.foo", "fooValue"); + ambariProperties.setProperty("server.jdbc.properties.bar", "barValue"); + + Properties properties = configuration.getDatabaseCustomProperties(); + Assert.assertEquals(2, properties.size()); + Assert.assertEquals("fooValue", properties.getProperty("eclipselink.jdbc.property.foo")); + Assert.assertEquals("barValue", properties.getProperty("eclipselink.jdbc.property.bar")); + } + + @Test + public void testCustomPersistenceProperties() throws Exception { + final Properties ambariProperties = new Properties(); + final Configuration configuration = new Configuration(ambariProperties); + ambariProperties.setProperty("server.persistence.properties.eclipselink.cache.coordination.channel", "FooChannel"); + ambariProperties.setProperty("server.persistence.properties.eclipselink.persistence-context.flush-mode", "commit"); + + Properties properties = configuration.getPersistenceCustomProperties(); + Assert.assertEquals(2, properties.size()); + Assert.assertEquals("FooChannel", properties.getProperty("eclipselink.cache.coordination.channel")); + Assert.assertEquals("commit", properties.getProperty("eclipselink.persistence-context.flush-mode")); + } + }