-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract common config functionality and introduce add config
part of #9417
- Loading branch information
Denis Anisimov
committed
Dec 11, 2020
1 parent
f3298e3
commit 13c9fe1
Showing
9 changed files
with
810 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
flow-server/src/main/java/com/vaadin/flow/server/AbstractConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2000-2020 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.flow.server; | ||
|
||
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP; | ||
|
||
/** | ||
* Defines a base contract for configuration (e.g. on an application level, | ||
* servlet level,...). | ||
* | ||
* @author Vaadin Ltd | ||
* @since | ||
* | ||
*/ | ||
public interface AbstractConfiguration { | ||
/** | ||
* Returns whether Vaadin is in production mode. | ||
* | ||
* @return true if in production mode, false otherwise. | ||
*/ | ||
boolean isProductionMode(); | ||
|
||
/** | ||
* Get if the dev server should be enabled. True by default | ||
* | ||
* @return true if dev server should be used | ||
*/ | ||
default boolean enableDevServer() { | ||
return getBooleanProperty( | ||
InitParameters.SERVLET_PARAMETER_ENABLE_DEV_SERVER, true); | ||
} | ||
|
||
/** | ||
* Returns whether Vaadin is running in useDeprecatedV14Bootstrapping. | ||
* | ||
* @return true if in useDeprecatedV14Bootstrapping, false otherwise. | ||
*/ | ||
default boolean useV14Bootstrap() { | ||
return getBooleanProperty(SERVLET_PARAMETER_USE_V14_BOOTSTRAP, false); | ||
} | ||
|
||
/** | ||
* Gets a configured property as a string. | ||
* | ||
* @param name | ||
* The simple of the property, in some contexts, lookup might be | ||
* performed using variations of the provided name. | ||
* @param defaultValue | ||
* the default value that should be used if no value has been | ||
* defined | ||
* @return the property value, or the passed default value if no property | ||
* value is found | ||
*/ | ||
String getStringProperty(String name, String defaultValue); | ||
|
||
/** | ||
* Gets a configured property as a boolean. | ||
* | ||
* | ||
* @param name | ||
* The simple of the property, in some contexts, lookup might be | ||
* performed using variations of the provided name. | ||
* @param defaultValue | ||
* the default value that should be used if no value has been | ||
* defined | ||
* @return the property value, or the passed default value if no property | ||
* value is found | ||
* | ||
*/ | ||
boolean getBooleanProperty(String name, boolean defaultValue); | ||
|
||
/** | ||
* Returns whether pnpm is enabled or not. | ||
* | ||
* @return {@code true} if enabled, {@code false} if not | ||
*/ | ||
default boolean isPnpmEnabled() { | ||
return getBooleanProperty(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM, | ||
Boolean.valueOf(Constants.ENABLE_PNPM_DEFAULT_STRING)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
flow-server/src/main/java/com/vaadin/flow/server/AbstractPropertyConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright 2000-2020 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.flow.server; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import static com.vaadin.flow.server.Constants.VAADIN_PREFIX; | ||
|
||
/** | ||
* @author Vaadin Ltd | ||
* @since | ||
* | ||
*/ | ||
public abstract class AbstractPropertyConfiguration | ||
implements AbstractConfiguration { | ||
|
||
private final Map<String, String> properties; | ||
|
||
public AbstractPropertyConfiguration(Map<String, String> properties) { | ||
this.properties = properties; | ||
} | ||
|
||
@Override | ||
public String getStringProperty(String name, String defaultValue) { | ||
return getApplicationOrSystemProperty(name, defaultValue, | ||
Function.identity()); | ||
} | ||
|
||
@Override | ||
public boolean getBooleanProperty(String name, boolean defaultValue) { | ||
/* | ||
* Considers {@code ""} to be equal {@code true} in order to treat | ||
* params like {@code -Dtest.param} as enabled ({@code test.param == | ||
* true}). | ||
*/ | ||
String booleanString = getStringProperty(name, null); | ||
if (booleanString == null) { | ||
return defaultValue; | ||
} else if (booleanString.isEmpty()) { | ||
return true; | ||
} else { | ||
boolean parsedBoolean = Boolean.parseBoolean(booleanString); | ||
if (Boolean.toString(parsedBoolean) | ||
.equalsIgnoreCase(booleanString)) { | ||
return parsedBoolean; | ||
} else { | ||
throw new IllegalArgumentException(String.format( | ||
"Property named '%s' is boolean, but contains incorrect value '%s' that is not boolean '%s'", | ||
name, booleanString, parsedBoolean)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Gets an application property value. | ||
* | ||
* @param parameterName | ||
* the Name or the parameter. | ||
* @return String value or null if not found | ||
*/ | ||
public String getApplicationProperty(String parameterName) { | ||
|
||
String val = properties.get(parameterName); | ||
if (val != null) { | ||
return val; | ||
} | ||
|
||
// Try lower case application properties for backward compatibility | ||
// with 3.0.2 and earlier | ||
val = properties.get(parameterName.toLowerCase()); | ||
|
||
return val; | ||
} | ||
|
||
/** | ||
* Gets unmodifiable underlying properties. | ||
* | ||
* @return the properties map | ||
*/ | ||
protected Map<String, String> getProperties() { | ||
return Collections.unmodifiableMap(properties); | ||
} | ||
|
||
/** | ||
* Gets a configured property. The properties are typically read from e.g. | ||
* web.xml or from system properties of the JVM. | ||
* | ||
* @param propertyName | ||
* The simple of the property, in some contexts, lookup might be | ||
* performed using variations of the provided name. | ||
* @param defaultValue | ||
* the default value that should be used if no value has been | ||
* defined | ||
* @param converter | ||
* the way string should be converted into the required property | ||
* @param <T> | ||
* type of a property | ||
* @return the property value, or the passed default value if no property | ||
* value is found | ||
*/ | ||
public <T> T getApplicationOrSystemProperty(String propertyName, | ||
T defaultValue, Function<String, T> converter) { | ||
// Try system properties | ||
String val = getSystemProperty(propertyName); | ||
if (val != null) { | ||
return converter.apply(val); | ||
} | ||
|
||
// Try application properties | ||
val = getApplicationProperty(propertyName); | ||
if (val != null) { | ||
return converter.apply(val); | ||
} | ||
|
||
return defaultValue; | ||
} | ||
|
||
/** | ||
* Gets an system property value. | ||
* | ||
* @param parameterName | ||
* the Name or the parameter. | ||
* @return String value or null if not found | ||
*/ | ||
protected String getSystemProperty(String parameterName) { | ||
// version prefixed with just "vaadin." | ||
return System.getProperty(VAADIN_PREFIX + parameterName); | ||
} | ||
|
||
} |
Oops, something went wrong.