Skip to content

Commit 2dab779

Browse files
committed
Address review comments
* use shorter name `routingDriver` instead of `routingDriverFromFirstAvailableAddress` * no overloads * take `Iterable<URI>` of server URIs and verify 'bolt+routing' scheme * introduce constants for URIs schemes
1 parent a8882ab commit 2dab779

File tree

4 files changed

+36
-37
lines changed

4 files changed

+36
-37
lines changed

driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@
4747

4848
import static java.lang.String.format;
4949
import static org.neo4j.driver.internal.security.SecurityPlan.insecure;
50-
import static org.neo4j.driver.v1.Config.EncryptionLevel.REQUIRED;
5150

5251
public class DriverFactory
5352
{
53+
public static final String BOLT_URI_SCHEME = "bolt";
54+
public static final String BOLT_ROUTING_URI_SCHEME = "bolt+routing";
55+
5456
public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings routingSettings,
5557
RetrySettings retrySettings, Config config )
5658
{
@@ -86,10 +88,10 @@ private Driver createDriver( URI uri, BoltServerAddress address, ConnectionPool
8688
String scheme = uri.getScheme().toLowerCase();
8789
switch ( scheme )
8890
{
89-
case "bolt":
91+
case BOLT_URI_SCHEME:
9092
assertNoRoutingContext( uri, routingSettings );
9193
return createDirectDriver( address, connectionPool, config, securityPlan, retryLogic );
92-
case "bolt+routing":
94+
case BOLT_ROUTING_URI_SCHEME:
9395
return createRoutingDriver( address, connectionPool, config, routingSettings, securityPlan, retryLogic );
9496
default:
9597
throw new ClientException( format( "Unsupported URI scheme: %s", scheme ) );

driver/src/main/java/org/neo4j/driver/v1/GraphDatabase.java

+22-19
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
package org.neo4j.driver.v1;
2020

2121
import java.net.URI;
22-
import java.util.List;
2322

2423
import org.neo4j.driver.internal.DriverFactory;
2524
import org.neo4j.driver.internal.cluster.RoutingSettings;
2625
import org.neo4j.driver.internal.retry.RetrySettings;
2726
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
2827

28+
import static org.neo4j.driver.internal.DriverFactory.BOLT_ROUTING_URI_SCHEME;
29+
2930
/**
3031
* Creates {@link Driver drivers}, optionally letting you {@link #driver(URI, Config)} to configure them.
3132
* @see Driver
@@ -135,42 +136,44 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
135136
}
136137

137138
/**
138-
* Try to create a bolt+routing driver from the first available address.
139-
* This is wrapper for the {@link #driver} method that finds the first
139+
* Try to create a bolt+routing driver from the <b>first</b> available address.
140+
* This is wrapper for the {@link #driver} method that finds the <b>first</b>
140141
* server to respond positively.
141142
*
142-
* @param addresses a list of server addresses for Neo4j instances
143+
* @param routingUris an {@link Iterable} of server {@link URI}s for Neo4j instances. All given URIs should
144+
* have 'bolt+routing' scheme.
143145
* @param authToken authentication to use, see {@link AuthTokens}
144146
* @param config user defined configuration
145147
* @return a new driver instance
146148
*/
147-
public static Driver routingDriverFromFirstAvailableAddress( List<String> addresses, AuthToken authToken, Config config )
149+
public static Driver routingDriver( Iterable<URI> routingUris, AuthToken authToken, Config config )
148150
{
149-
for( String address: addresses )
151+
assertRoutingUris( routingUris );
152+
153+
for ( URI uri : routingUris )
150154
{
151155
try
152156
{
153-
return driver( "bolt+routing://" + address, authToken, config );
157+
return driver( uri, authToken, config );
154158
}
155-
catch( ServiceUnavailableException e )
159+
catch ( ServiceUnavailableException e )
156160
{
157161
// try the next one
158162
}
159163
}
164+
160165
throw new ServiceUnavailableException( "Failed to discover an available server" );
161166
}
162167

163-
/**
164-
* Try to create a bolt+routing driver from the first available address.
165-
* This is wrapper for the {@link #driver} method that finds the first
166-
* server to respond positively.
167-
*
168-
* @param addresses a list of server addresses for Neo4j instances
169-
* @param authToken authentication to use, see {@link AuthTokens}
170-
* @return a new driver instance
171-
*/
172-
public static Driver routingDriverFromFirstAvailableAddress( List<String> addresses, AuthToken authToken )
168+
private static void assertRoutingUris( Iterable<URI> uris )
173169
{
174-
return routingDriverFromFirstAvailableAddress( addresses, authToken, Config.defaultConfig() );
170+
for ( URI uri : uris )
171+
{
172+
if ( !BOLT_ROUTING_URI_SCHEME.equals( uri.getScheme() ) )
173+
{
174+
throw new IllegalArgumentException(
175+
"Illegal URI scheme, expected '" + BOLT_ROUTING_URI_SCHEME + "' in '" + uri + "'" );
176+
}
177+
}
175178
}
176179
}

driver/src/test/java/org/neo4j/driver/v1/integration/CausalClusteringIT.java

+6-14
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import static org.junit.Assert.assertThat;
6868
import static org.junit.Assert.assertTrue;
6969
import static org.junit.Assert.fail;
70+
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
7071
import static org.neo4j.driver.v1.Values.parameters;
7172

7273
public class CausalClusteringIT
@@ -459,10 +460,10 @@ private int executeWriteAndReadThroughBolt( ClusterMember member ) throws Timeou
459460

460461
private int executeWriteAndReadThroughBoltOnFirstAvailableAddress( ClusterMember... members ) throws TimeoutException, InterruptedException
461462
{
462-
List<String> addresses = new ArrayList<>( members.length );
463+
List<URI> addresses = new ArrayList<>( members.length );
463464
for ( ClusterMember member : members )
464465
{
465-
addresses.add( member.getRoutingUri().getAuthority() );
466+
addresses.add( member.getRoutingUri() );
466467
}
467468
try ( Driver driver = discoverDriver( addresses ) )
468469
{
@@ -616,22 +617,13 @@ public Logger getLog( String name )
616617
return GraphDatabase.driver( boltUri, clusterRule.getDefaultAuthToken(), config );
617618
}
618619

619-
private Driver discoverDriver( List<String> addresses )
620+
private Driver discoverDriver( List<URI> routingUris )
620621
{
621-
Logging devNullLogging = new Logging()
622-
{
623-
@Override
624-
public Logger getLog( String name )
625-
{
626-
return DevNullLogger.DEV_NULL_LOGGER;
627-
}
628-
};
629-
630622
Config config = Config.build()
631-
.withLogging( devNullLogging )
623+
.withLogging( DEV_NULL_LOGGING )
632624
.toConfig();
633625

634-
return GraphDatabase.routingDriverFromFirstAvailableAddress( addresses, clusterRule.getDefaultAuthToken(), config );
626+
return GraphDatabase.routingDriver( routingUris, clusterRule.getDefaultAuthToken(), config );
635627
}
636628

637629
private static void createNodesInDifferentThreads( int count, final Driver driver ) throws Exception

driver/src/test/java/org/neo4j/driver/v1/util/cc/LocalOrRemoteClusterRule.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.neo4j.driver.v1.AuthToken;
2626
import org.neo4j.driver.v1.AuthTokens;
2727

28+
import static org.neo4j.driver.internal.DriverFactory.BOLT_ROUTING_URI_SCHEME;
29+
2830
public class LocalOrRemoteClusterRule extends ExternalResource
2931
{
3032
private static final String CLUSTER_URI_SYSTEM_PROPERTY_NAME = "externalClusterUri";
@@ -88,7 +90,7 @@ private static void assertValidSystemPropertiesDefined()
8890
"Both cluster uri and 'neo4j' user password system properties should be set. " +
8991
"Uri: '" + uri + "', Password: '" + password + "'" );
9092
}
91-
if ( uri != null && !"bolt+routing".equals( uri.getScheme() ) )
93+
if ( uri != null && !BOLT_ROUTING_URI_SCHEME.equals( uri.getScheme() ) )
9294
{
9395
throw new IllegalStateException( "CLuster uri should have bolt+routing scheme: '" + uri + "'" );
9496
}

0 commit comments

Comments
 (0)