1   /*
2    * Copyright 2019 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.client.spring;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.annotation.Nullable;
23  import javax.validation.constraints.NotBlank;
24  import javax.validation.constraints.NotEmpty;
25  import javax.validation.constraints.PositiveOrZero;
26  
27  import org.springframework.boot.context.properties.ConfigurationProperties;
28  import org.springframework.validation.annotation.Validated;
29  
30  import com.google.common.base.MoreObjects;
31  
32  /**
33   * Settings for a Central Dogma client.
34   *
35   * <h2>Example with {@code "hosts"} option</h2>
36   * <pre>{@code
37   * centraldogma:
38   *   hosts:
39   *   - replica1.examples.com:36462
40   *   - replica2.examples.com:36462
41   *   - replica3.examples.com:36462
42   *   access-token: appToken-cffed349-d573-457f-8f74-4727ad9341ce
43   *   health-check-interval-millis: 15000
44   *   initialization-timeout-millis: 15000
45   *   use-tls: false
46   * }</pre>
47   *
48   * <h2>Example with {@code "profile"} option</h2>
49   * <pre>{@code
50   * centraldogma:
51   *   profile: beta
52   *   access-token: appToken-cffed349-d573-457f-8f74-4727ad9341ce
53   *   health-check-interval-millis: 15000
54   *   initialization-timeout-millis: 15000
55   *   use-tls: false
56   * }</pre>
57   *
58   * <p>Note that {@code "healthCheckIntervalMillis"} and {@code "useTls"} are optional.</p>
59   */
60  @ConfigurationProperties(prefix = "centraldogma")
61  @Validated
62  public class CentralDogmaSettings {
63      /**
64       * The Central Dogma client profile name. If not specified, {@code "hosts"} must be specified.
65       */
66      @Nullable
67      private String profile;
68  
69      /**
70       * The list of Central Dogma hosts. e.g. {@code "foo.example.com"} or {@code "foo.example.com:36462"}.
71       * If not specified, {@code "profile"} must be specified.
72       */
73      @Nullable
74      private List<String> hosts;
75  
76      /**
77       * The access token which is used to authenticate the Central Dogma client.
78       */
79      @Nullable
80      private String accessToken;
81  
82      /**
83       * The number of milliseconds between each health-check requests to Central Dogma servers.
84       * {@code 0} disables the health check requests.
85       */
86      @Nullable
87      private Long healthCheckIntervalMillis;
88  
89      /**
90       * Whether to use a TLS connection when communicating with a Central Dogma server.
91       */
92      @Nullable
93      private Boolean useTls;
94  
95      /**
96       * The maximum number of retries to perform when replication lag is detected.
97       */
98      @Nullable
99      private Integer maxNumRetriesOnReplicationLag;
100 
101     /**
102      * The number of milliseconds between retries which occurred due to replication lag.
103      */
104     @Nullable
105     private Long retryIntervalOnReplicationLagMillis;
106 
107     /**
108      * The number of milliseconds the Central Dogma client waits for initialization to complete.
109      * {@code 0} means the Central Dogma client is immediately returned without waiting for the initialization.
110      * If unspecified, defaults to
111      * {@value CentralDogmaClientAutoConfiguration#DEFAULT_INITIALIZATION_TIMEOUT_MILLIS}.
112      */
113     @Nullable
114     private Long initializationTimeoutMillis;
115 
116     /**
117      * Returns the Central Dogma client profile name.
118      *
119      * @return {@code null} if not specified.
120      */
121     @Nullable
122     public String getProfile() {
123         return profile;
124     }
125 
126     /**
127      * Sets the Central Dogma client profile name.
128      */
129     public void setProfile(@NotBlank String profile) {
130         this.profile = profile;
131     }
132 
133     /**
134      * Returns the list of Central Dogma hosts.
135      *
136      * @return {@code null} if not specified.
137      */
138     @Nullable
139     public List<String> getHosts() {
140         return hosts;
141     }
142 
143     /**
144      * Sets the list of Central Dogma hosts.
145      */
146     public void setHosts(@NotEmpty List<String> hosts) {
147         // Cannot use ImmutableList.copyOf() because Spring Boot 1.x attempts an add() call on it.
148         this.hosts = new ArrayList<>(hosts);
149     }
150 
151     /**
152      * Returns the access token which is used to authenticate the Central Dogma client.
153      *
154      * @return {@code null} if not specified.
155      */
156     @Nullable
157     public String getAccessToken() {
158         return accessToken;
159     }
160 
161     /**
162      * Sets the access token which is used to authenticate the Central Dogma client.
163      */
164     public void setAccessToken(@NotBlank String accessToken) {
165         this.accessToken = accessToken;
166     }
167 
168     /**
169      * Returns the number of milliseconds between each health-check requests to Central Dogma servers.
170      *
171      * @return {@code null} if not specified.
172      */
173     @Nullable
174     public Long getHealthCheckIntervalMillis() {
175         return healthCheckIntervalMillis;
176     }
177 
178     /**
179      * Sets the number of milliseconds between each health-check requests to Central Dogma servers.
180      */
181     public void setHealthCheckIntervalMillis(@PositiveOrZero long healthCheckIntervalMillis) {
182         this.healthCheckIntervalMillis = healthCheckIntervalMillis;
183     }
184 
185     /**
186      * Returns whether to use a TLS connection when communicating with a Central Dogma server.
187      *
188      * @return {@code null} if not specified.
189      */
190     @Nullable
191     public Boolean getUseTls() {
192         return useTls;
193     }
194 
195     /**
196      * Sets whether to use a TLS connection when communicating with a Central Dogma server.
197      */
198     public void setUseTls(boolean useTls) {
199         this.useTls = useTls;
200     }
201 
202     /**
203      * Returns the maximum number of retries to perform when replication lag is detected.
204      *
205      * @return {@code null} if not specified.
206      */
207     @Nullable
208     public Integer getMaxNumRetriesOnReplicationLag() {
209         return maxNumRetriesOnReplicationLag;
210     }
211 
212     /**
213      * Sets the maximum number of retries to perform when replication lag is detected.
214      */
215     public void setMaxNumRetriesOnReplicationLag(@Nullable Integer maxNumRetriesOnReplicationLag) {
216         this.maxNumRetriesOnReplicationLag = maxNumRetriesOnReplicationLag;
217     }
218 
219     /**
220      * Returns the number of milliseconds between retries which occurred due to replication lag.
221      *
222      * @return {@code null} if not specified.
223      */
224     @Nullable
225     public Long getRetryIntervalOnReplicationLagMillis() {
226         return retryIntervalOnReplicationLagMillis;
227     }
228 
229     /**
230      * Sets the number of milliseconds between retries which occurred due to replication lag.
231      */
232     public void setRetryIntervalOnReplicationLagMillis(@Nullable Long retryIntervalOnReplicationLagMillis) {
233         this.retryIntervalOnReplicationLagMillis = retryIntervalOnReplicationLagMillis;
234     }
235 
236     /**
237      * Returns the number of milliseconds the Central Dogma client waits for initialization to complete.
238      * If {@code 0} is specified, Central Dogma client is immediately returned without waiting.
239      *
240      * @return {@code null} if not specified.
241      */
242     @Nullable
243     public Long initializationTimeoutMillis() {
244         return initializationTimeoutMillis;
245     }
246 
247     /**
248      * Returns the number of milliseconds the Central Dogma client waits for initialization to complete.
249      * If {@code 0} is specified, the Central Dogma client is immediately returned without waiting.
250      * If unspecified, defaults to
251      * {@value CentralDogmaClientAutoConfiguration#DEFAULT_INITIALIZATION_TIMEOUT_MILLIS}.
252      */
253     public void setInitializationTimeoutMillis(@Nullable Long initializationTimeoutMillis) {
254         this.initializationTimeoutMillis = initializationTimeoutMillis;
255     }
256 
257     @Override
258     public String toString() {
259         return MoreObjects.toStringHelper(this).omitNullValues()
260                           .add("profile", profile)
261                           .add("hosts", hosts)
262                           .add("accessToken", accessToken != null ? "********" : null)
263                           .add("healthCheckIntervalMillis", healthCheckIntervalMillis)
264                           .add("initializationTimeoutMillis", initializationTimeoutMillis)
265                           .add("useTls", useTls)
266                           .add("maxNumRetriesOnReplicationLag", maxNumRetriesOnReplicationLag)
267                           .add("retryIntervalOnReplicationLagMillis", retryIntervalOnReplicationLagMillis)
268                           .toString();
269     }
270 }