Skip to content

Commit

Permalink
OGM-662 Add support for multiple hosts in document store configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelbernard committed May 18, 2015
1 parent fe9e84a commit 1e41a35
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
7 changes: 7 additions & 0 deletions core/src/main/java/org/hibernate/ogm/cfg/OgmProperties.java
Expand Up @@ -50,12 +50,19 @@ public interface OgmProperties {

/**
* Property for setting the host name to connect to. Accepts {@code String}.
* Accepts a comma separated list of host / ports.
* Note that for IPv6, the host must be surrounded by square bracket if a port is defined: [2001:db8::ff00:42:8329]:123
*
* For example
* www.example.com, www2.example.com:123, 192.0.2.1, 192.0.2.2:123, 2001:db8::ff00:42:8329, [2001:db8::ff00:42:8329]:123
*/
String HOST = "hibernate.ogm.datastore.host";

/**
* Property for setting the port number of the database to connect to. Accepts {@code int}.
* @deprecated ignored when multiple hosts are defined in {@link #HOST}, prefer the {@code host:port} approach
*/
@Deprecated
String PORT = "hibernate.ogm.datastore.port";

/**
Expand Down
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.ogm.cfg.spi;

import org.hibernate.ogm.cfg.OgmProperties;
import org.hibernate.ogm.cfg.impl.HostParser;
import org.hibernate.ogm.util.configurationreader.impl.Validators;
import org.hibernate.ogm.util.configurationreader.spi.ConfigurationPropertyReader;

Expand All @@ -24,23 +25,24 @@ public abstract class DocumentStoreConfiguration {



private final String host;
private final int port;
private final Hosts hosts;
private final String databaseName;
private final String username;
private final String password;
private final boolean createDatabase;

public DocumentStoreConfiguration(ConfigurationPropertyReader propertyReader, int defaultPort) {
this.host = propertyReader.property( OgmProperties.HOST, String.class )
String host = propertyReader.property( OgmProperties.HOST, String.class )
.withDefault( DEFAULT_HOST )
.getValue();

this.port = propertyReader.property( OgmProperties.PORT, int.class )
.withDefault( defaultPort )
Integer port = propertyReader.property( OgmProperties.PORT, Integer.class )
.withValidator( Validators.PORT )
.withDefault( null )
.getValue();

hosts = HostParser.parse( host, port, defaultPort );

this.databaseName = propertyReader.property( OgmProperties.DATABASE, String.class )
.required()
.getValue();
Expand All @@ -55,18 +57,11 @@ public DocumentStoreConfiguration(ConfigurationPropertyReader propertyReader, in

/**
* @see OgmProperties#HOST
* @return The host name of the data store instance
*/
public String getHost() {
return host;
}

/**
* @see OgmProperties#PORT
* @return The port of the data store instance to connect to
* @return The host name of the data store instance
*/
public int getPort() {
return port;
public Hosts getHosts() {
return hosts;
}

/**
Expand Down
Expand Up @@ -14,6 +14,7 @@
import org.hibernate.HibernateException;
import org.hibernate.ogm.cfg.OgmProperties;
import org.hibernate.ogm.cfg.spi.DocumentStoreConfiguration;
import org.hibernate.ogm.cfg.spi.Hosts;
import org.hibernate.ogm.util.configurationreader.spi.ConfigurationPropertyReader;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -44,15 +45,17 @@ public void setUp() {
public void shouldReturnTheDefaultValueIfThePortKeyIsNotPresentAsAConfigurationValue() {
configuration = new TestDocumentStoreConfiguration( configurationValues );

assertThat( configuration.getPort() ).isEqualTo( 1234 );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getPort() ).isEqualTo( 1234 );
}

@Test
public void shouldReturnTheDefaultValueIfThePortConfigurationValueIsTheEmptyString() {
configurationValues.put( OgmProperties.PORT, "" );
configuration = new TestDocumentStoreConfiguration( configurationValues );

assertThat( configuration.getPort() ).isEqualTo( 1234 );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getPort() ).isEqualTo( 1234 );
}

@Test
Expand All @@ -61,22 +64,25 @@ public void shouldReturnThePortConfigured() {
configurationValues.put( OgmProperties.PORT, String.valueOf( configuredPortValue ) );
configuration = new TestDocumentStoreConfiguration( configurationValues );

assertThat( configuration.getPort() ).isEqualTo( configuredPortValue );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getPort() ).isEqualTo( configuredPortValue );
}

@Test
public void shouldReturnLocalhostIfTheHostNotPresentAsAConfigurationValue() {
configuration = new TestDocumentStoreConfiguration( configurationValues );

assertThat( configuration.getHost() ).isEqualTo( "localhost" );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getHost() ).isEqualTo( "localhost" );
}

@Test
public void shouldReturnLocalhostIfTheHostConfigurationValueIsTheEmptyString() {
configurationValues.put( OgmProperties.HOST, " " );
configuration = new TestDocumentStoreConfiguration( configurationValues );

assertThat( configuration.getHost() ).isEqualTo( "localhost" );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getHost() ).isEqualTo( "localhost" );
}

@Test
Expand All @@ -85,7 +91,8 @@ public void shouldReturnTheHostConfiguredValue() {
configurationValues.put( OgmProperties.HOST, configuredHostValue );
configuration = new TestDocumentStoreConfiguration( configurationValues );

assertThat( configuration.getHost() ).isEqualTo( configuredHostValue );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getHost() ).isEqualTo( configuredHostValue );
}

@Test
Expand Down

0 comments on commit 1e41a35

Please sign in to comment.