This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
ConfigurationBuilder.java
239 lines (216 loc) · 8.7 KB
/
ConfigurationBuilder.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
/*
* Copyright 2014-2021 the original author or authors.
*
* 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.
*/
package org.liquigraph.core.configuration;
import org.liquigraph.core.configuration.validators.DatasourceConfigurationValidator;
import org.liquigraph.core.configuration.validators.ExecutionModeValidator;
import org.liquigraph.core.configuration.validators.MandatoryOptionValidator;
import org.liquigraph.core.configuration.validators.UserCredentialsOptionValidator;
import org.liquigraph.core.io.ChangelogLoader;
import org.liquigraph.core.io.ClassLoaderChangelogLoader;
import javax.sql.DataSource;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
/**
* Fluent {@link Configuration} builder.
* It also validates configuration parameters.
*/
public final class ConfigurationBuilder {
private String masterChangelog;
private Optional<DataSource> dataSource = Optional.empty();
private Optional<String> uri = Optional.empty();
private Optional<String> database = Optional.empty();
private Optional<String> username = Optional.empty();
private Optional<String> password = Optional.empty();
private ExecutionContexts executionContexts = ExecutionContexts.DEFAULT_CONTEXT;
private ExecutionMode executionMode;
private MandatoryOptionValidator mandatoryOptionValidator = new MandatoryOptionValidator();
private DatasourceConfigurationValidator datasourceConnectionValidator = new DatasourceConfigurationValidator();
private ExecutionModeValidator executionModeValidator = new ExecutionModeValidator();
private UserCredentialsOptionValidator userCredentialsOptionValidator = new UserCredentialsOptionValidator();
private ChangelogLoader changelogLoader = ClassLoaderChangelogLoader.currentThreadContextClassLoader();
/**
* Specifies the location of the master changelog file.
* Please note that this location should point to a readable Liquigraph changelog file.
*
* @param masterChangelog Liquigraph changelog
* @return itself for chaining purposes
*/
public ConfigurationBuilder withMasterChangelogLocation(String masterChangelog) {
this.masterChangelog = masterChangelog;
return this;
}
/**
* Specifies the JDBC connection URI of the graph database instance.
* Alternatively, you can set a {@code DataSource} directly
*
* @param uri connection URI
* @return itself for chaining purposes
*/
public ConfigurationBuilder withUri(String uri) {
this.uri = Optional.ofNullable(uri);
return this;
}
/**
* Specifies the data source of the graph database instance.
* Alternatively, you can set the URI
*
* @param dataSource data source
* @return itself for chaining purposes
*/
public ConfigurationBuilder withDataSource(DataSource dataSource) {
this.dataSource = Optional.ofNullable(dataSource);
return this;
}
/**
* Specifies the database to run changes on.
* If not specified, the driver will select the default instance.
*
* @param database database
* @return itself for chaining purposes
*/
public ConfigurationBuilder withDatabase(String database) {
this.database = Optional.ofNullable(database);
return this;
}
/**
* Specifies the username allowed to connect to the remote graph database instance.
* Please be sure to provide a password, if you provide a username, too.
*
* @param username username
* @return itself for chaining purposes
*/
public ConfigurationBuilder withUsername(String username) {
this.username = Optional.ofNullable(username);
return this;
}
/**
* Specifies the password allowed to connect to the remote graph database instance.
* Please be sure to provide a username, if you provide a password, too.
*
* @param password password
* @return itself for chaining purposes
*/
public ConfigurationBuilder withPassword(String password) {
this.password = Optional.ofNullable(password);
return this;
}
/**
* @param executionContexts 0 or more Liquigraph execution contexts to allow changeset
* filtering
* @return itself for chaining purposes
* @see ConfigurationBuilder#withExecutionContexts(java.util.Collection)
*/
public ConfigurationBuilder withExecutionContexts(String... executionContexts) {
return withExecutionContexts(Arrays.asList(executionContexts));
}
/**
* Specifies one or more execution contexts.
*
* @param executionContexts non-nullable execution contexts
* @return itself for chaining purposes
*/
public ConfigurationBuilder withExecutionContexts(Collection<String> executionContexts) {
if (!executionContexts.isEmpty()) {
this.executionContexts = new ExecutionContexts(executionContexts);
}
return this;
}
/**
* Sets Liquigraph to execute changesets against the configured graph database.
*
* @return itself for chaining purposes
*/
public ConfigurationBuilder withRunMode() {
this.executionMode = RunMode.RUN_MODE;
return this;
}
/**
* Sets Liquigraph to write changesets in a <code>output.cypher</code>
* in the specified outputDirectory.
* Note that it won't write to the graph database.
*
* @param outputDirectory writable directory where the file is written
* @return itself for chaining purposes
*/
public ConfigurationBuilder withDryRunMode(Path outputDirectory) {
this.executionMode = new DryRunMode(outputDirectory);
return this;
}
/**
* @param classLoader class loader
* @return itself for chaining purposes
* @deprecated Please use {@link #withChangelogLoader(ChangelogLoader)} with a {@link ClassLoaderChangelogLoader}.
* <p>
* Sets ClassLoader to use when reading Liquigraph changelogs.
* Default is <code>Thread.currentThread().getContextClassLoader()</code>.
* Don't call this unless you REALLY know what you're doing.
*/
@Deprecated
public ConfigurationBuilder withClassLoader(ClassLoader classLoader) {
if (classLoader != null) {
this.changelogLoader = new ClassLoaderChangelogLoader(classLoader);
}
return this;
}
/**
* Sets {@link ChangelogLoader} to use when reading Liquigraph changelogs.
* Default is {@link ClassLoaderChangelogLoader#currentThreadContextClassLoader()}.
* Don't call this unless you REALLY know what you're doing.
*
* @param changelogLoader the changelog loader to use
* @return itself for chaining purposes
*/
public ConfigurationBuilder withChangelogLoader(ChangelogLoader changelogLoader) {
if (changelogLoader != null) {
this.changelogLoader = changelogLoader;
}
return this;
}
/**
* Builds a {@link Configuration} instance after validating the specified
* parameters.
*
* @return Liquigraph configuration
*/
public Configuration build() {
Collection<String> errors = new ArrayList<>();
errors.addAll(mandatoryOptionValidator.validate(changelogLoader, masterChangelog));
errors.addAll(datasourceConnectionValidator.validate(uri, dataSource, database));
errors.addAll(executionModeValidator.validate(executionMode));
errors.addAll(userCredentialsOptionValidator.validate(username.orElse(null), password.orElse(null)));
if (!errors.isEmpty()) {
throw new RuntimeException(formatErrors(errors));
}
return new Configuration(
changelogLoader,
masterChangelog,
dataSourceConfiguration(),
executionContexts,
executionMode,
database.orElse(null)
);
}
private ConnectionConfiguration dataSourceConfiguration() {
return Connections.provide(uri, database, username, password, dataSource);
}
private String formatErrors(Collection<String> errors) {
String separator = "\n\t - ";
return String.format("%s%s", separator, String.join(separator, errors));
}
}