Skip to content

Commit 9b2dad4

Browse files
authored
feat: use jackson for setting up configuration (#21059)
Use jackson for setting up configuration settings. part of #20741
1 parent db7dd80 commit 9b2dad4

File tree

5 files changed

+103
-100
lines changed

5 files changed

+103
-100
lines changed

flow-server/src/main/java/com/vaadin/flow/server/DeploymentConfigurationFactory.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@
2121
import java.util.Map;
2222
import java.util.Properties;
2323

24+
import com.fasterxml.jackson.databind.JsonNode;
2425
import org.slf4j.LoggerFactory;
2526

2627
import com.vaadin.flow.component.UI;
2728
import com.vaadin.flow.function.DeploymentConfiguration;
29+
import com.vaadin.flow.internal.JacksonUtils;
2830
import com.vaadin.flow.server.startup.AbstractConfigurationFactory;
2931
import com.vaadin.flow.server.startup.ApplicationConfiguration;
3032

31-
import elemental.json.JsonObject;
32-
import elemental.json.impl.JsonUtil;
33-
3433
/**
3534
* Creates {@link DeploymentConfiguration} filled with all parameters specified
3635
* by the framework users.
@@ -114,7 +113,7 @@ private void readBuildInfo(Properties initParameters) {
114113
// Read the json and set the appropriate system properties if not
115114
// already set.
116115
if (json != null) {
117-
JsonObject buildInfo = JsonUtil.parse(json);
116+
JsonNode buildInfo = JacksonUtils.readTree(json);
118117
Map<String, String> properties = getConfigParametersUsingTokenData(
119118
buildInfo);
120119
// only insert properties that haven't been defined

flow-server/src/main/java/com/vaadin/flow/server/startup/AbstractConfigurationFactory.java

+84-79
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
import java.util.Map;
2525
import java.util.function.Function;
2626

27+
import com.fasterxml.jackson.databind.JsonNode;
2728
import org.apache.commons.io.FileUtils;
2829

2930
import com.vaadin.flow.internal.UsageStatistics;
3031
import com.vaadin.flow.server.Constants;
3132
import com.vaadin.flow.server.InitParameters;
3233
import com.vaadin.flow.server.frontend.FrontendUtils;
3334

34-
import elemental.json.JsonObject;
35-
3635
import static com.vaadin.flow.server.Constants.CONNECT_APPLICATION_PROPERTIES_TOKEN;
3736
import static com.vaadin.flow.server.Constants.CONNECT_JAVA_SOURCE_FOLDER_TOKEN;
3837
import static com.vaadin.flow.server.Constants.CONNECT_OPEN_API_FILE_TOKEN;
@@ -80,122 +79,127 @@ public class AbstractConfigurationFactory implements Serializable {
8079
* @return the config parameters
8180
*/
8281
protected Map<String, String> getConfigParametersUsingTokenData(
83-
JsonObject buildInfo) {
82+
JsonNode buildInfo) {
8483
Map<String, String> params = new HashMap<>();
85-
if (buildInfo.hasKey(SERVLET_PARAMETER_PRODUCTION_MODE)) {
86-
params.put(SERVLET_PARAMETER_PRODUCTION_MODE, String.valueOf(
87-
buildInfo.getBoolean(SERVLET_PARAMETER_PRODUCTION_MODE)));
84+
if (buildInfo.has(SERVLET_PARAMETER_PRODUCTION_MODE)) {
85+
params.put(SERVLET_PARAMETER_PRODUCTION_MODE,
86+
String.valueOf(
87+
buildInfo.get(SERVLET_PARAMETER_PRODUCTION_MODE)
88+
.booleanValue()));
8889
}
89-
if (buildInfo.hasKey(EXTERNAL_STATS_FILE_TOKEN)
90-
|| buildInfo.hasKey(EXTERNAL_STATS_URL_TOKEN)) {
90+
if (buildInfo.has(EXTERNAL_STATS_FILE_TOKEN)
91+
|| buildInfo.has(EXTERNAL_STATS_URL_TOKEN)) {
9192
params.put(EXTERNAL_STATS_FILE, Boolean.toString(true));
92-
if (buildInfo.hasKey(EXTERNAL_STATS_URL_TOKEN)) {
93+
if (buildInfo.has(EXTERNAL_STATS_URL_TOKEN)) {
9394
params.put(EXTERNAL_STATS_URL,
94-
buildInfo.getString(EXTERNAL_STATS_URL_TOKEN));
95+
buildInfo.get(EXTERNAL_STATS_URL_TOKEN).textValue());
9596
}
9697
// NO OTHER CONFIGURATION:
9798
return params;
9899
}
99-
if (buildInfo.hasKey(SERVLET_PARAMETER_INITIAL_UIDL)) {
100-
params.put(SERVLET_PARAMETER_INITIAL_UIDL, String.valueOf(
101-
buildInfo.getBoolean(SERVLET_PARAMETER_INITIAL_UIDL)));
100+
if (buildInfo.has(SERVLET_PARAMETER_INITIAL_UIDL)) {
101+
params.put(SERVLET_PARAMETER_INITIAL_UIDL, String.valueOf(buildInfo
102+
.get(SERVLET_PARAMETER_INITIAL_UIDL).booleanValue()));
102103
// Need to be sure that we remove the system property,
103104
// because it has priority in the configuration getter
104105
System.clearProperty(
105106
VAADIN_PREFIX + SERVLET_PARAMETER_INITIAL_UIDL);
106107
}
107108

108-
if (buildInfo.hasKey(NPM_TOKEN)) {
109-
params.put(PROJECT_BASEDIR, buildInfo.getString(NPM_TOKEN));
110-
verifyFolderExists(params, buildInfo.getString(NPM_TOKEN));
109+
if (buildInfo.has(NPM_TOKEN)) {
110+
params.put(PROJECT_BASEDIR, buildInfo.get(NPM_TOKEN).textValue());
111+
verifyFolderExists(params, buildInfo.get(NPM_TOKEN).textValue());
111112
}
112113

113-
if (buildInfo.hasKey(NODE_VERSION)) {
114-
params.put(NODE_VERSION, buildInfo.getString(NODE_VERSION));
114+
if (buildInfo.has(NODE_VERSION)) {
115+
params.put(NODE_VERSION, buildInfo.get(NODE_VERSION).textValue());
115116
}
116-
if (buildInfo.hasKey(NODE_DOWNLOAD_ROOT)) {
117+
if (buildInfo.has(NODE_DOWNLOAD_ROOT)) {
117118
params.put(NODE_DOWNLOAD_ROOT,
118-
buildInfo.getString(NODE_DOWNLOAD_ROOT));
119+
buildInfo.get(NODE_DOWNLOAD_ROOT).textValue());
119120
}
120121

121-
if (buildInfo.hasKey(FRONTEND_TOKEN)) {
122+
if (buildInfo.has(FRONTEND_TOKEN)) {
122123
params.put(FrontendUtils.PARAM_FRONTEND_DIR,
123-
buildInfo.getString(FRONTEND_TOKEN));
124+
buildInfo.get(FRONTEND_TOKEN).textValue());
124125
// Only verify frontend folder if it's not a subfolder of the
125126
// npm folder.
126-
if (!buildInfo.hasKey(NPM_TOKEN)
127-
|| !buildInfo.getString(FRONTEND_TOKEN)
128-
.startsWith(buildInfo.getString(NPM_TOKEN))) {
129-
verifyFolderExists(params, buildInfo.getString(FRONTEND_TOKEN));
127+
if (!buildInfo.has(NPM_TOKEN)
128+
|| !buildInfo.get(FRONTEND_TOKEN).textValue()
129+
.startsWith(buildInfo.get(NPM_TOKEN).textValue())) {
130+
verifyFolderExists(params,
131+
buildInfo.get(FRONTEND_TOKEN).textValue());
130132
}
131133
}
132134

133135
// These should be internal only so if there is a System
134136
// property override then the user probably knows what
135137
// they are doing.
136-
if (buildInfo.hasKey(FRONTEND_HOTDEPLOY)) {
137-
params.put(FRONTEND_HOTDEPLOY,
138-
String.valueOf(buildInfo.getBoolean(FRONTEND_HOTDEPLOY)));
139-
} else if (buildInfo.hasKey(SERVLET_PARAMETER_ENABLE_DEV_SERVER)) {
140-
params.put(FRONTEND_HOTDEPLOY, String.valueOf(
141-
buildInfo.getBoolean(SERVLET_PARAMETER_ENABLE_DEV_SERVER)));
138+
if (buildInfo.has(FRONTEND_HOTDEPLOY)) {
139+
params.put(FRONTEND_HOTDEPLOY, String
140+
.valueOf(buildInfo.get(FRONTEND_HOTDEPLOY).booleanValue()));
141+
} else if (buildInfo.has(SERVLET_PARAMETER_ENABLE_DEV_SERVER)) {
142+
params.put(FRONTEND_HOTDEPLOY, String.valueOf(buildInfo
143+
.get(SERVLET_PARAMETER_ENABLE_DEV_SERVER).booleanValue()));
142144
}
143-
if (buildInfo.hasKey(SERVLET_PARAMETER_REUSE_DEV_SERVER)) {
144-
params.put(SERVLET_PARAMETER_REUSE_DEV_SERVER, String.valueOf(
145-
buildInfo.getBoolean(SERVLET_PARAMETER_REUSE_DEV_SERVER)));
145+
if (buildInfo.has(SERVLET_PARAMETER_REUSE_DEV_SERVER)) {
146+
params.put(SERVLET_PARAMETER_REUSE_DEV_SERVER,
147+
String.valueOf(
148+
buildInfo.get(SERVLET_PARAMETER_REUSE_DEV_SERVER)
149+
.booleanValue()));
146150
}
147-
if (buildInfo.hasKey(CONNECT_JAVA_SOURCE_FOLDER_TOKEN)) {
148-
params.put(CONNECT_JAVA_SOURCE_FOLDER_TOKEN,
149-
buildInfo.getString(CONNECT_JAVA_SOURCE_FOLDER_TOKEN));
151+
if (buildInfo.has(CONNECT_JAVA_SOURCE_FOLDER_TOKEN)) {
152+
params.put(CONNECT_JAVA_SOURCE_FOLDER_TOKEN, buildInfo
153+
.get(CONNECT_JAVA_SOURCE_FOLDER_TOKEN).textValue());
150154
}
151-
if (buildInfo.hasKey(Constants.JAVA_RESOURCE_FOLDER_TOKEN)) {
152-
params.put(Constants.JAVA_RESOURCE_FOLDER_TOKEN,
153-
buildInfo.getString(Constants.JAVA_RESOURCE_FOLDER_TOKEN));
155+
if (buildInfo.has(Constants.JAVA_RESOURCE_FOLDER_TOKEN)) {
156+
params.put(Constants.JAVA_RESOURCE_FOLDER_TOKEN, buildInfo
157+
.get(Constants.JAVA_RESOURCE_FOLDER_TOKEN).textValue());
154158
}
155-
if (buildInfo.hasKey(CONNECT_OPEN_API_FILE_TOKEN)) {
159+
if (buildInfo.has(CONNECT_OPEN_API_FILE_TOKEN)) {
156160
params.put(CONNECT_OPEN_API_FILE_TOKEN,
157-
buildInfo.getString(CONNECT_OPEN_API_FILE_TOKEN));
161+
buildInfo.get(CONNECT_OPEN_API_FILE_TOKEN).textValue());
158162
}
159-
if (buildInfo.hasKey(CONNECT_APPLICATION_PROPERTIES_TOKEN)) {
160-
params.put(CONNECT_APPLICATION_PROPERTIES_TOKEN,
161-
buildInfo.getString(CONNECT_APPLICATION_PROPERTIES_TOKEN));
163+
if (buildInfo.has(CONNECT_APPLICATION_PROPERTIES_TOKEN)) {
164+
params.put(CONNECT_APPLICATION_PROPERTIES_TOKEN, buildInfo
165+
.get(CONNECT_APPLICATION_PROPERTIES_TOKEN).textValue());
162166
}
163-
if (buildInfo.hasKey(PROJECT_FRONTEND_GENERATED_DIR_TOKEN)) {
164-
params.put(PROJECT_FRONTEND_GENERATED_DIR_TOKEN,
165-
buildInfo.getString(PROJECT_FRONTEND_GENERATED_DIR_TOKEN));
167+
if (buildInfo.has(PROJECT_FRONTEND_GENERATED_DIR_TOKEN)) {
168+
params.put(PROJECT_FRONTEND_GENERATED_DIR_TOKEN, buildInfo
169+
.get(PROJECT_FRONTEND_GENERATED_DIR_TOKEN).textValue());
166170
}
167-
if (buildInfo.hasKey(BUILD_FOLDER)) {
168-
params.put(BUILD_FOLDER, buildInfo.getString(BUILD_FOLDER));
171+
if (buildInfo.has(BUILD_FOLDER)) {
172+
params.put(BUILD_FOLDER, buildInfo.get(BUILD_FOLDER).textValue());
169173
}
170-
if (buildInfo.hasKey(DISABLE_PREPARE_FRONTEND_CACHE)) {
174+
if (buildInfo.has(DISABLE_PREPARE_FRONTEND_CACHE)) {
171175
UsageStatistics.markAsUsed("flow/always-execute-prepare-frontend",
172176
null);
173177
}
174-
if (buildInfo.hasKey(REACT_ENABLE)) {
178+
if (buildInfo.has(REACT_ENABLE)) {
175179
params.put(REACT_ENABLE,
176-
String.valueOf(buildInfo.getBoolean(REACT_ENABLE)));
180+
String.valueOf(buildInfo.get(REACT_ENABLE).booleanValue()));
177181
}
178-
if (buildInfo.hasKey(APPLICATION_IDENTIFIER)) {
182+
if (buildInfo.has(APPLICATION_IDENTIFIER)) {
179183
params.put(APPLICATION_IDENTIFIER,
180-
buildInfo.getString(APPLICATION_IDENTIFIER));
184+
buildInfo.get(APPLICATION_IDENTIFIER).textValue());
181185
}
182-
if (buildInfo.hasKey(DAU_TOKEN)) {
186+
if (buildInfo.has(DAU_TOKEN)) {
183187
params.put(DAU_TOKEN,
184-
String.valueOf(buildInfo.getBoolean(DAU_TOKEN)));
188+
String.valueOf(buildInfo.get(DAU_TOKEN).booleanValue()));
185189
}
186-
if (buildInfo.hasKey(PREMIUM_FEATURES)) {
187-
params.put(PREMIUM_FEATURES,
188-
String.valueOf(buildInfo.getBoolean(PREMIUM_FEATURES)));
190+
if (buildInfo.has(PREMIUM_FEATURES)) {
191+
params.put(PREMIUM_FEATURES, String
192+
.valueOf(buildInfo.get(PREMIUM_FEATURES).booleanValue()));
189193
}
190194

191-
if (buildInfo.hasKey(InitParameters.FRONTEND_EXTRA_EXTENSIONS)) {
195+
if (buildInfo.has(InitParameters.FRONTEND_EXTRA_EXTENSIONS)) {
192196
params.put(InitParameters.FRONTEND_EXTRA_EXTENSIONS, buildInfo
193-
.getString(InitParameters.FRONTEND_EXTRA_EXTENSIONS));
197+
.get(InitParameters.FRONTEND_EXTRA_EXTENSIONS).textValue());
194198
}
195199

196-
if (buildInfo.hasKey(NPM_EXCLUDE_WEB_COMPONENTS)) {
197-
params.put(NPM_EXCLUDE_WEB_COMPONENTS, String
198-
.valueOf(buildInfo.getBoolean(NPM_EXCLUDE_WEB_COMPONENTS)));
200+
if (buildInfo.has(NPM_EXCLUDE_WEB_COMPONENTS)) {
201+
params.put(NPM_EXCLUDE_WEB_COMPONENTS, String.valueOf(
202+
buildInfo.get(NPM_EXCLUDE_WEB_COMPONENTS).booleanValue()));
199203
}
200204

201205
setDevModePropertiesUsingTokenData(params, buildInfo);
@@ -205,37 +209,38 @@ protected Map<String, String> getConfigParametersUsingTokenData(
205209
/**
206210
* Sets to the dev mode properties to the configuration parameters.
207211
*
208-
* @see #getConfigParametersUsingTokenData(JsonObject)
212+
* @see #getConfigParametersUsingTokenData(JsonNode)
209213
*
210214
* @param params
211215
* the configuration parameters to set dev mode properties to
212216
* @param buildInfo
213217
* the token file data
214218
*/
215219
protected void setDevModePropertiesUsingTokenData(
216-
Map<String, String> params, JsonObject buildInfo) {
220+
Map<String, String> params, JsonNode buildInfo) {
217221
// read dev mode properties from the token and set init parameter only
218222
// if it's not yet set
219223
if (params.get(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM) == null
220224
&& buildInfo
221-
.hasKey(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM)) {
225+
.has(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM)) {
222226
params.put(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM,
223-
String.valueOf(buildInfo.getBoolean(
224-
InitParameters.SERVLET_PARAMETER_ENABLE_PNPM)));
227+
String.valueOf(buildInfo
228+
.get(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM)
229+
.booleanValue()));
225230
}
226231
if (params.get(InitParameters.SERVLET_PARAMETER_ENABLE_BUN) == null
227-
&& buildInfo
228-
.hasKey(InitParameters.SERVLET_PARAMETER_ENABLE_BUN)) {
232+
&& buildInfo.has(InitParameters.SERVLET_PARAMETER_ENABLE_BUN)) {
229233
params.put(InitParameters.SERVLET_PARAMETER_ENABLE_BUN,
230-
String.valueOf(buildInfo.getBoolean(
231-
InitParameters.SERVLET_PARAMETER_ENABLE_BUN)));
234+
String.valueOf(buildInfo
235+
.get(InitParameters.SERVLET_PARAMETER_ENABLE_BUN)
236+
.booleanValue()));
232237
}
233238
if (params.get(InitParameters.REQUIRE_HOME_NODE_EXECUTABLE) == null
234-
&& buildInfo
235-
.hasKey(InitParameters.REQUIRE_HOME_NODE_EXECUTABLE)) {
239+
&& buildInfo.has(InitParameters.REQUIRE_HOME_NODE_EXECUTABLE)) {
236240
params.put(InitParameters.REQUIRE_HOME_NODE_EXECUTABLE,
237-
String.valueOf(buildInfo.getBoolean(
238-
InitParameters.REQUIRE_HOME_NODE_EXECUTABLE)));
241+
String.valueOf(buildInfo
242+
.get(InitParameters.REQUIRE_HOME_NODE_EXECUTABLE)
243+
.booleanValue()));
239244
}
240245
}
241246

flow-server/src/main/java/com/vaadin/flow/server/startup/DefaultApplicationConfigurationFactory.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@
2525
import java.util.Map;
2626
import java.util.Objects;
2727

28+
import com.fasterxml.jackson.databind.JsonNode;
2829
import org.osgi.framework.Constants;
2930
import org.osgi.service.component.annotations.Component;
3031
import org.slf4j.Logger;
3132
import org.slf4j.LoggerFactory;
3233

3334
import com.vaadin.flow.di.Lookup;
3435
import com.vaadin.flow.di.ResourceProvider;
36+
import com.vaadin.flow.internal.JacksonUtils;
3537
import com.vaadin.flow.server.AbstractPropertyConfiguration;
3638
import com.vaadin.flow.server.VaadinContext;
3739
import com.vaadin.flow.server.frontend.FrontendUtils;
3840

39-
import elemental.json.JsonObject;
40-
import elemental.json.impl.JsonUtil;
41-
4241
import static com.vaadin.flow.server.Constants.VAADIN_SERVLET_RESOURCES;
4342
import static com.vaadin.flow.server.InitParameters.APPLICATION_PARAMETER_DEVMODE_ENABLE_SERIALIZE_SESSION;
4443
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE;
@@ -101,13 +100,13 @@ public ApplicationConfiguration create(VaadinContext context) {
101100
final String name = paramNames.nextElement();
102101
props.put(name, context.getContextParameter(name));
103102
}
104-
JsonObject buildInfo = null;
103+
JsonNode buildInfo = null;
105104
try {
106105
String content = getTokenFileContent(props::get);
107106
if (content == null) {
108107
content = getTokenFileFromClassloader(context);
109108
}
110-
buildInfo = content == null ? null : JsonUtil.parse(content);
109+
buildInfo = content == null ? null : JacksonUtils.readTree(content);
111110
if (buildInfo != null) {
112111
props.putAll(getConfigParametersUsingTokenData(buildInfo));
113112
}

flow-server/src/test/java/com/vaadin/flow/server/DeploymentConfigurationFactoryTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,11 @@ private String generateJsonStringFromFields(Field[] fields,
427427
for (int i = 0; i < fields.length; i++) {
428428
try {
429429
String paramName = (String) fields[i].get(null);
430-
mockTokenJsonString += "'" + paramName + "': ";
430+
mockTokenJsonString += "\"" + paramName + "\": ";
431431
if (!stringParams.contains(paramName)) {
432432
mockTokenJsonString += "true";
433433
} else {
434-
mockTokenJsonString += " 'bar'";
434+
mockTokenJsonString += " \"bar\"";
435435
}
436436

437437
} catch (IllegalAccessException illegalAccess) {
@@ -452,7 +452,7 @@ public void createInitParameters_tokenFileIsSetViaContext_externalStatsUrlIsRead
452452
DeploymentConfigurationFactory factory = new DeploymentConfigurationFactory();
453453

454454
VaadinConfig config = mockTokenFileViaContextParam(
455-
"{ 'externalStatsUrl': 'http://my.server/static/stats.json'}");
455+
"{ \"externalStatsUrl\": \"http://my.server/static/stats.json\"}");
456456

457457
VaadinContext context = Mockito.mock(VaadinContext.class);
458458

@@ -475,7 +475,7 @@ public void createInitParameters_tokenFileIsSetViaContext_externalStatsFileIsRea
475475
DeploymentConfigurationFactory factory = new DeploymentConfigurationFactory();
476476

477477
VaadinConfig config = mockTokenFileViaContextParam(
478-
"{ 'externalStatsFile': true}");
478+
"{ \"externalStatsFile\": true}");
479479

480480
VaadinContext context = Mockito.mock(VaadinContext.class);
481481

@@ -496,7 +496,7 @@ public void createInitParameters_tokenFileIsSetViaContext_setPropertyFromTokenFi
496496
DeploymentConfigurationFactory factory = new DeploymentConfigurationFactory();
497497

498498
VaadinConfig config = mockTokenFileViaContextParam(
499-
"{ '" + SERVLET_PARAMETER_PRODUCTION_MODE + "': true}");
499+
"{ \"" + SERVLET_PARAMETER_PRODUCTION_MODE + "\": true}");
500500

501501
VaadinContext context = Mockito.mock(VaadinContext.class);
502502

@@ -571,7 +571,7 @@ public void externalStatsFileTrue_predefinedValuesAreNotOverridden_productionMod
571571
@Test
572572
public void createInitParameters_readDevModeProperties() throws Exception {
573573
FileUtils.writeLines(tokenFile, Arrays.asList("{",
574-
"\"pnpm.enable\": true,", "\"require.home.node\": true,", "}"));
574+
"\"pnpm.enable\": true,", "\"require.home.node\": true", "}"));
575575

576576
DeploymentConfiguration config = createConfig(Collections
577577
.singletonMap(PARAM_TOKEN_FILE, tokenFile.getPath()));
@@ -586,7 +586,7 @@ public void createInitParameters_readDevModeProperties() throws Exception {
586586
public void createInitParameters_initParamtersAreSet_tokenDevModePropertiesAreNotSet()
587587
throws Exception {
588588
FileUtils.writeLines(tokenFile, Arrays.asList("{",
589-
"\"pnpm.enable\": true,", "\"require.home.node\": true,", "}"));
589+
"\"pnpm.enable\": true,", "\"require.home.node\": true", "}"));
590590

591591
DeploymentConfiguration config = createConfig(Collections
592592
.singletonMap(PARAM_TOKEN_FILE, tokenFile.getPath()));

0 commit comments

Comments
 (0)