2323import java .io .IOException ;
2424import java .io .InputStream ;
2525import java .lang .annotation .Annotation ;
26+ import java .util .ArrayList ;
2627import java .util .Collection ;
28+ import java .util .List ;
2729import java .util .Properties ;
2830import java .util .concurrent .CompletableFuture ;
2931
4648import com .arangodb .internal .velocystream .CommunicationSync ;
4749import com .arangodb .internal .velocystream .ConnectionAsync ;
4850import com .arangodb .internal .velocystream .ConnectionSync ;
51+ import com .arangodb .internal .velocystream .DefaultHostHandler ;
52+ import com .arangodb .internal .velocystream .Host ;
53+ import com .arangodb .internal .velocystream .HostHandler ;
4954import com .arangodb .model .LogOptions ;
5055import com .arangodb .model .UserCreateOptions ;
5156import com .arangodb .model .UserUpdateOptions ;
@@ -70,17 +75,8 @@ public class ArangoDBAsync extends InternalArangoDB<ArangoExecutorAsync, Complet
7075
7176 public static class Builder {
7277
73- private static final String PROPERTY_KEY_HOST = "arangodb.host" ;
74- private static final String PROPERTY_KEY_PORT = "arangodb.port" ;
75- private static final String PROPERTY_KEY_TIMEOUT = "arangodb.timeout" ;
76- private static final String PROPERTY_KEY_USER = "arangodb.user" ;
77- private static final String PROPERTY_KEY_PASSWORD = "arangodb.password" ;
78- private static final String PROPERTY_KEY_USE_SSL = "arangodb.usessl" ;
79- private static final String PROPERTY_KEY_V_STREAM_CHUNK_CONTENT_SIZE = "arangodb.chunksize" ;
80- private static final String DEFAULT_PROPERTY_FILE = "/arangodb.properties" ;
81-
82- private String host ;
83- private Integer port ;
78+ private final List <Host > hosts ;
79+ private Host host ;
8480 private Integer timeout ;
8581 private String user ;
8682 private String password ;
@@ -98,6 +94,8 @@ public Builder() {
9894 vpackParser = new VPackParser ();
9995 VPackConfigure .configure (vpackBuilder , vpackParser , collectionCache );
10096 VPackConfigureAsync .configure (vpackBuilder );
97+ host = new Host (ArangoDBConstants .DEFAULT_HOST , ArangoDBConstants .DEFAULT_PORT );
98+ hosts = new ArrayList <>();
10199 loadProperties (ArangoDBAsync .class .getResourceAsStream (DEFAULT_PROPERTY_FILE ));
102100 }
103101
@@ -106,40 +104,57 @@ public Builder loadProperties(final InputStream in) {
106104 final Properties properties = new Properties ();
107105 try {
108106 properties .load (in );
109- host = getProperty (properties , PROPERTY_KEY_HOST , host , ArangoDBConstants .DEFAULT_HOST );
110- port = Integer
111- .parseInt (getProperty (properties , PROPERTY_KEY_PORT , port , ArangoDBConstants .DEFAULT_PORT ));
112- timeout = Integer .parseInt (
113- getProperty (properties , PROPERTY_KEY_TIMEOUT , timeout , ArangoDBConstants .DEFAULT_TIMEOUT ));
114- user = getProperty (properties , PROPERTY_KEY_USER , user , null );
115- password = getProperty (properties , PROPERTY_KEY_PASSWORD , password , null );
116- useSsl = Boolean .parseBoolean (
117- getProperty (properties , PROPERTY_KEY_USE_SSL , useSsl , ArangoDBConstants .DEFAULT_USE_SSL ));
118- chunksize = Integer .parseInt (getProperty (properties , PROPERTY_KEY_V_STREAM_CHUNK_CONTENT_SIZE ,
119- chunksize , ArangoDBConstants .CHUNK_DEFAULT_CONTENT_SIZE ));
107+ loadHosts (properties , this .hosts );
108+ final String host = loadHost (properties , this .host .getHost ());
109+ final int port = loadPort (properties , this .host .getPort ());
110+ this .host = new Host (host , port );
111+ timeout = loadTimeout (properties , timeout );
112+ user = loadUser (properties , user );
113+ password = loadPassword (properties , password );
114+ useSsl = loadUseSsl (properties , useSsl );
115+ chunksize = loadChunkSize (properties , chunksize );
120116 } catch (final IOException e ) {
121117 throw new ArangoDBException (e );
122118 }
123119 }
124120 return this ;
125121 }
126122
127- private <T > String getProperty (
128- final Properties properties ,
129- final String key ,
130- final T currentValue ,
131- final T defaultValue ) {
132- return properties .getProperty (key ,
133- currentValue != null ? currentValue .toString () : defaultValue != null ? defaultValue .toString () : null );
134- }
135-
123+ /**
124+ * @deprecated will be removed in version 4.2.0 use {@link #host(String, int)} instead
125+ *
126+ * @param host
127+ * @return
128+ */
129+ @ Deprecated
136130 public Builder host (final String host ) {
137- this .host = host ;
131+ this .host = new Host ( host , this . host . getPort ()) ;
138132 return this ;
139133 }
140134
135+ /**
136+ * @deprecated will be removed in version 4.2.0 use {@link #host(String, int)} instead
137+ *
138+ * @param port
139+ * @return
140+ */
141+ @ Deprecated
141142 public Builder port (final Integer port ) {
142- this .port = port ;
143+ host = new Host (host .getHost (), port );
144+ return this ;
145+ }
146+
147+ /**
148+ * Adds a host to connect to. Multiple hosts can be added to provide fallbacks.
149+ *
150+ * @param host
151+ * address of the host
152+ * @param port
153+ * port of the host
154+ * @return {@link ArangoDB.Builder}
155+ */
156+ public Builder host (final String host , final int port ) {
157+ hosts .add (new Host (host , port ));
143158 return this ;
144159 }
145160
@@ -243,17 +258,22 @@ public <T extends Annotation> Builder annotationFieldNaming(
243258 }
244259
245260 public ArangoDBAsync build () {
246- return new ArangoDBAsync (asyncBuilder (), vpackBuilder .build (),
247- vpackBuilder .serializeNullValues (true ).build (), vpackParser , collectionCache , syncBuilder ());
261+ if (hosts .isEmpty ()) {
262+ hosts .add (host );
263+ }
264+ final HostHandler hostHandler = new DefaultHostHandler (hosts );
265+ return new ArangoDBAsync (asyncBuilder (hostHandler ), vpackBuilder .build (),
266+ vpackBuilder .serializeNullValues (true ).build (), vpackParser , collectionCache ,
267+ syncBuilder (hostHandler ));
248268 }
249269
250- private CommunicationAsync .Builder asyncBuilder () {
251- return new CommunicationAsync .Builder (). host ( host ). port ( port ).timeout (timeout ).user (user ).password (password )
270+ private CommunicationAsync .Builder asyncBuilder (final HostHandler hostHandler ) {
271+ return new CommunicationAsync .Builder (hostHandler ).timeout (timeout ).user (user ).password (password )
252272 .useSsl (useSsl ).sslContext (sslContext ).chunksize (chunksize );
253273 }
254274
255- private CommunicationSync .Builder syncBuilder () {
256- return new CommunicationSync .Builder (). host ( host ). port ( port ).timeout (timeout ).user (user ).password (password )
275+ private CommunicationSync .Builder syncBuilder (final HostHandler hostHandler ) {
276+ return new CommunicationSync .Builder (hostHandler ).timeout (timeout ).user (user ).password (password )
257277 .useSsl (useSsl ).sslContext (sslContext ).chunksize (chunksize );
258278 }
259279
0 commit comments