forked from temporalio/sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientConfig.java
More file actions
240 lines (218 loc) · 8.43 KB
/
ClientConfig.java
File metadata and controls
240 lines (218 loc) · 8.43 KB
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
package io.temporal.envconfig;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.toml.TomlMapper;
import java.io.*;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* ClientConfig represents a client config file.
*
* <p>The default config file path is OS-specific:
*
* <ul>
* <li>macOS: $HOME/Library/Application Support/temporalio/temporal.toml
* <li>Windows: %APPDATA%\temporalio\temporal.toml
* <li>Linux/other: $HOME/.config/temporalio/temporal.toml
* </ul>
*/
public class ClientConfig {
/** Creates a new builder to build a {@link ClientConfig}. */
public static Builder newBuilder() {
return new Builder();
}
/**
* Creates a new builder to build a {@link ClientConfig} based on an existing config.
*
* @param profile the existing profile to base the builder on
* @return a new Builder instance
*/
public static Builder newBuilder(ClientConfig profile) {
return new Builder(profile);
}
/** Returns a default instance of {@link ClientConfig} with all fields unset. */
public static ClientConfig getDefaultInstance() {
return new ClientConfig.Builder().build();
}
private static String getDefaultConfigFilePath() {
String userDir = System.getProperty("user.home");
if (userDir == null || userDir.isEmpty()) {
return null;
}
return getDefaultConfigFilePath(userDir, System.getProperty("os.name"), System.getenv());
}
static String getDefaultConfigFilePath(
String userDir, String osName, Map<String, String> environment) {
if (userDir == null || userDir.isEmpty()) {
return null;
}
if (osName != null) {
String osNameLower = osName.toLowerCase();
if (osNameLower.contains("mac")) {
return Paths.get(userDir, "Library", "Application Support", "temporalio", "temporal.toml")
.toString();
}
if (osNameLower.contains("win")) {
String appData = environment != null ? environment.get("APPDATA") : null;
if (appData == null || appData.isEmpty()) {
return null;
}
return Paths.get(appData, "temporalio", "temporal.toml").toString();
}
}
return Paths.get(userDir, ".config", "temporalio", "temporal.toml").toString();
}
/**
* Load all client profiles from given sources.
*
* <p>This does not apply environment variable overrides to the profiles, it only uses an
* environment variable to find the default config file path (TEMPORAL_CONFIG_FILE). To get a
* single profile with environment variables applied, use {@link ClientConfigProfile#load}.
*/
public static ClientConfig load() throws IOException {
return load(LoadClientConfigOptions.newBuilder().build());
}
/**
* Load all client profiles from given sources.
*
* <p>This does not apply environment variable overrides to the profiles, it only uses an
* environment variable to find the default config file path (TEMPORAL_CONFIG_FILE). To get a
* single profile with environment variables applied, use {@link ClientConfigProfile#load}.
*
* @param options options to control loading the config
* @throws IOException if the config file cannot be read or parsed
*/
public static ClientConfig load(LoadClientConfigOptions options) throws IOException {
ObjectReader reader = new TomlMapper().readerFor(ClientConfigToml.TomlClientConfig.class);
if (options.isStrictConfigFile()) {
reader =
reader.withFeatures(
com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
} else {
reader =
reader.withoutFeatures(
com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
if (options.getConfigFileData() != null && options.getConfigFileData().length > 0) {
if (options.getConfigFilePath() != null && !options.getConfigFilePath().isEmpty()) {
throw new IllegalArgumentException(
"Cannot have both ConfigFileData and ConfigFilePath set");
}
ClientConfigToml.TomlClientConfig result = reader.readValue(options.getConfigFileData());
return new ClientConfig(ClientConfigToml.getClientProfiles(result));
} else {
// Get file name which is either set value, env var, or default path
String file = options.getConfigFilePath();
if (file == null || file.isEmpty()) {
Map<String, String> env = options.getEnvOverrides();
if (env == null) {
env = System.getenv();
}
// Unlike env vars for the config values, empty and unset env var
// for config file path are both treated as unset
file = env.get("TEMPORAL_CONFIG_FILE");
}
if (file == null || file.isEmpty()) {
file = getDefaultConfigFilePath();
}
// No config dir available — return default empty config
if (file == null) {
return getDefaultInstance();
}
try {
ClientConfigToml.TomlClientConfig result = reader.readValue(new File(file));
return new ClientConfig(ClientConfigToml.getClientProfiles(result));
} catch (FileNotFoundException e) {
// File not found is ok - return default empty config
return getDefaultInstance();
}
}
}
/**
* Load client config from given TOML data.
*
* @param tomlData TOML data to parse
* @return the parsed client config
* @throws IOException if the TOML data cannot be parsed
*/
public static ClientConfig fromToml(byte[] tomlData) throws IOException {
return fromToml(tomlData, ClientConfigFromTomlOptions.getDefaultInstance());
}
/**
* Load client config from given TOML data.
*
* @param tomlData TOML data to parse
* @param options options to control parsing the TOML data
* @return the parsed client config
* @throws IOException if the TOML data cannot be parsed
*/
public static ClientConfig fromToml(byte[] tomlData, ClientConfigFromTomlOptions options)
throws IOException {
ObjectReader reader = new TomlMapper().readerFor(ClientConfigToml.TomlClientConfig.class);
if (options.isStrictConfigFile()) {
reader =
reader.withFeatures(
com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
} else {
reader =
reader.withoutFeatures(
com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
ClientConfigToml.TomlClientConfig result = reader.readValue(tomlData);
return new ClientConfig(ClientConfigToml.getClientProfiles(result));
}
/**
* Convert the client config to TOML data. Encoding is UTF-8.
*
* @param config the client config to convert
* @return the TOML data as bytes
* <p>Note: The output will not be identical to the input if the config was loaded from a file
* because comments and formatting are not preserved.
*/
public static byte[] toTomlAsBytes(ClientConfig config) throws IOException {
ObjectWriter writer = new TomlMapper().writerFor(ClientConfigToml.TomlClientConfig.class);
return writer.writeValueAsBytes(
new ClientConfigToml.TomlClientConfig(
ClientConfigToml.fromClientProfiles(config.getProfiles())));
}
private ClientConfig(Map<String, ClientConfigProfile> profiles) {
this.profiles = profiles;
}
private final Map<String, ClientConfigProfile> profiles;
/** All profiles loaded from the config file, may be empty but never null. */
public Map<String, ClientConfigProfile> getProfiles() {
return new HashMap<>(profiles);
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
ClientConfig that = (ClientConfig) o;
return Objects.equals(profiles, that.profiles);
}
@Override
public int hashCode() {
return Objects.hashCode(profiles);
}
@Override
public String toString() {
return "ClientConfig{" + "profiles=" + profiles + '}';
}
public static final class Builder {
private final Map<String, ClientConfigProfile> profiles;
public Builder(ClientConfig config) {
this.profiles = config.getProfiles();
}
public Builder() {
this.profiles = new HashMap<>();
}
public Builder putProfile(String name, ClientConfigProfile profile) {
profiles.put(name, profile);
return this;
}
public ClientConfig build() {
return new ClientConfig(profiles);
}
}
}