-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNakshaHubConfig.java
310 lines (271 loc) · 10.1 KB
/
NakshaHubConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (C) 2017-2023 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.lib.hub;
import static com.here.naksha.lib.core.NakshaLogger.currentLogger;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.here.naksha.lib.core.NakshaVersion;
import com.here.naksha.lib.core.models.geojson.implementation.XyzFeature;
import com.here.naksha.lib.core.util.json.JsonSerializable;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* The Naksha-Hub service configuration.
*/
@JsonTypeName(value = "Config")
public final class NakshaHubConfig extends XyzFeature implements JsonSerializable {
/**
* The default application name, used for example as identifier when accessing the PostgresQL database and to read the configuration file
* using the <a href="https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html">XGD</a> standard, therefore from
* directory ({@code ~/.config/<APP_NAME>/...}).
*/
public static final @NotNull String APP_NAME = "naksha";
/**
* Returns a default application name used at many placed.
* @return The default application name.
*/
public static @NotNull String defaultAppName() {
return APP_NAME + "/v" + NakshaVersion.latest;
}
/**
* Returns a className of default NakshaHub instance
* @return The default NakshaHub className
*/
public static @NotNull String defaultHubClassName() {
return NakshaHub.class.getName();
}
@JsonCreator
NakshaHubConfig(
@JsonProperty("id") @NotNull String id,
@JsonProperty("hubClassName") @Nullable String hubClassName,
@JsonProperty("userAgent") @Nullable String userAgent,
@JsonProperty("appId") @Nullable String appId,
@JsonProperty("author") @Nullable String author,
@JsonProperty("httpPort") @Nullable Integer httpPort,
@JsonProperty("hostname") @Nullable String hostname,
@JsonProperty("endpoint") @Nullable String endpoint,
@JsonProperty("env") @Nullable String env,
@JsonProperty("webRoot") @Nullable String webRoot,
@JsonProperty("jwtName") @Nullable String jwtName,
@JsonProperty("debug") @Nullable Boolean debug,
@JsonProperty("maintenanceIntervalInMins") @Nullable Integer maintenanceIntervalInMins,
@JsonProperty("maintenanceInitialDelayInMins") @Nullable Integer maintenanceInitialDelayInMins,
@JsonProperty("maintenancePoolCoreSize") @Nullable Integer maintenancePoolCoreSize,
@JsonProperty("maintenancePoolMaxSize") @Nullable Integer maintenancePoolMaxSize,
@JsonProperty("storageParams") @Nullable Map<String, Object> storageParams) {
super(id);
if (httpPort != null && (httpPort < 0 || httpPort > 65535)) {
currentLogger()
.atError("Invalid port in Naksha configuration: {}")
.add(httpPort)
.log();
httpPort = 8080;
} else if (httpPort == null || httpPort == 0) {
httpPort = 8080;
}
if (hostname == null || hostname.length() == 0) {
try {
hostname = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
currentLogger()
.atError("Unable to resolve the hostname using Java's API.")
.setCause(e)
.log();
hostname = "localhost";
}
}
URL __endpoint = null;
if (endpoint != null && endpoint.length() > 0) {
try {
__endpoint = new URL(endpoint);
} catch (MalformedURLException e) {
currentLogger()
.atError("Invalid configuration of endpoint: {}")
.add(endpoint)
.setCause(e)
.log();
}
}
if (__endpoint == null) {
try {
//noinspection HttpUrlsUsage
__endpoint = new URL("http://" + hostname + ":" + httpPort);
} catch (MalformedURLException e) {
currentLogger()
.atError("Invalid hostname: {}")
.add(hostname)
.setCause(e)
.log();
hostname = "localhost";
try {
__endpoint = new URL("http://localhost:" + httpPort);
} catch (MalformedURLException ignore) {
}
}
assert __endpoint != null;
}
if (env == null) {
env = "local";
}
this.hubClassName = (hubClassName != null && !hubClassName.isEmpty()) ? hubClassName : defaultHubClassName();
this.appId = appId != null && appId.length() > 0 ? appId : "naksha";
this.author = author;
this.httpPort = httpPort;
this.hostname = hostname;
this.endpoint = __endpoint;
this.env = env;
this.webRoot = webRoot;
this.jwtName = jwtName != null && !jwtName.isEmpty() ? jwtName : "jwt";
this.userAgent = userAgent != null && !userAgent.isEmpty() ? userAgent : defaultAppName();
this.debug = Boolean.TRUE.equals(debug);
this.maintenanceIntervalInMins =
maintenanceIntervalInMins != null ? maintenanceIntervalInMins : defaultMaintenanceIntervalInMins();
this.maintenanceInitialDelayInMins = maintenanceInitialDelayInMins != null
? maintenanceInitialDelayInMins
: defaultMaintenanceInitialDelayInMins();
this.maintenancePoolCoreSize =
maintenancePoolCoreSize != null ? maintenancePoolCoreSize : defaultMaintenancePoolCoreSize();
this.maintenancePoolMaxSize =
maintenancePoolMaxSize != null ? maintenancePoolMaxSize : defaultMaintenancePoolMaxSize();
this.storageParams = storageParams;
}
public static final String HTTP_PORT = "httpPort";
/**
* The port at which to listen for HTTP requests.
*/
@JsonProperty(HTTP_PORT)
public final int httpPort;
public static final String HOSTNAME = "hostname";
/**
* The hostname to use to refer to this instance, if {@code null}, then auto-detected.
*/
@JsonProperty(HOSTNAME)
public final @NotNull String hostname;
public static final String APP_ID = "appId";
/**
* The application-id to be used when modifying the admin-database.
*/
@JsonProperty(APP_ID)
public final @NotNull String appId;
public static final String AUTHOR = "author";
/**
* The author to be used when modifying the admin-database.
*/
@JsonProperty(AUTHOR)
public final @Nullable String author;
public static final String ENDPOINT = "endpoint";
/**
* The public endpoint, for example "https://naksha.foo.com/". If {@code null}, then the hostname and HTTP port used.
*/
@JsonProperty(ENDPOINT)
public @NotNull URL endpoint;
public static final String ENV = "env";
/**
* The environment, for example "local", "dev", "e2e" or "prd".
*/
@JsonProperty(ENV)
public final @NotNull String env;
public static final String WEB_ROOT = "webRoot";
/**
* If set, then serving static files from this directory.
*/
@JsonProperty(WEB_ROOT)
public final @Nullable String webRoot;
public static final String JWT_NAME = "jwtName";
/**
* The JWT key files to be read from the disk ({@code "~/.config/naksha/auth/$<jwtName>.(key|pub)"}).
*/
@JsonProperty(JWT_NAME)
public final @NotNull String jwtName;
public static final String USER_AGENT = "userAgent";
/**
* The user-agent to be used for external communication.
*/
@JsonProperty(USER_AGENT)
public final @NotNull String userAgent;
public static final String DEBUG = "debug";
/**
* If debugging mode is enabled.
*/
@JsonProperty(DEBUG)
@JsonInclude(Include.NON_DEFAULT)
public boolean debug;
public static final String HUB_CLASS_NAME = "hubClassName";
/**
* The fully qualified class name to be used to initiate NakshaHub instance
*/
@JsonProperty(HUB_CLASS_NAME)
public final @NotNull String hubClassName;
/**
* The initial delay (in minutes) after the service start up, when the Storage Maintenance job should perform first execution
*/
public final int maintenanceInitialDelayInMins;
/**
* Returns a default initial delay in mins, for starting Storage Maintenance job.
* @return The default interval
*/
public static int defaultMaintenanceInitialDelayInMins() {
return 1 * 60; // 1 hour
}
/**
* The interval in minutes, with which the Storage Maintenance job is to be scheduled
*/
public final int maintenanceIntervalInMins;
/**
* Returns a default interval in mins, for scheduling Storage Maintenance job.
* @return The default interval
*/
public static int defaultMaintenanceIntervalInMins() {
return 12 * 60; // 12 hours
}
/**
* The initial size of thread pool for running Storage Maintenance jobs in parallel
*/
public final int maintenancePoolCoreSize;
/**
* Returns a default initial size of thread pool for running Storage Maintenance jobs in parallel
* @return the default core size of maintenance thread pool
*/
public static int defaultMaintenancePoolCoreSize() {
return 5;
}
/**
* The maximum size of thread pool for running Storage Maintenance jobs in parallel
*/
public final int maintenancePoolMaxSize;
/**
* Returns a default maximum size of thread pool for running Storage Maintenance jobs in parallel
* @return the default max size of maintenance thread pool
*/
public static int defaultMaintenancePoolMaxSize() {
return 5;
}
/**
* Optional storage-specific parameters
*/
public final Map<String, Object> storageParams;
}