Skip to content

Commit

Permalink
refactor: extract common config functionality and introduce add config
Browse files Browse the repository at this point in the history
part of #9417
  • Loading branch information
Denis Anisimov committed Dec 11, 2020
1 parent 93a0be2 commit 01eb234
Show file tree
Hide file tree
Showing 9 changed files with 810 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import com.vaadin.flow.server.AbstractConfiguration;
import com.vaadin.flow.server.Constants;
import com.vaadin.flow.server.InitParameters;
import com.vaadin.flow.server.WrappedSession;
import com.vaadin.flow.shared.communication.PushMode;

import static com.vaadin.flow.server.Constants.POLYFILLS_DEFAULT_VALUE;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_POLYFILLS;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP;

/**
* A collection of properties configured at deploy time as well as a way of
Expand All @@ -39,23 +39,8 @@
* @author Vaadin Ltd
* @since 1.0
*/
public interface DeploymentConfiguration extends Serializable {

/**
* Returns whether Vaadin is in production mode.
*
* @return true if in production mode, false otherwise.
*/
boolean isProductionMode();

/**
* 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);
}
public interface DeploymentConfiguration
extends AbstractConfiguration, Serializable {

/**
* Returns whether the server provides timing info to the client.
Expand Down Expand Up @@ -195,6 +180,7 @@ <T> T getApplicationOrSystemProperty(String propertyName, T defaultValue,
* @return the property value, or the passed default value if no property
* value is found
*/
@Override
default String getStringProperty(String propertyName, String defaultValue) {
return getApplicationOrSystemProperty(propertyName, defaultValue,
Function.identity());
Expand Down Expand Up @@ -224,6 +210,7 @@ default String getStringProperty(String propertyName, String defaultValue) {
* @throws IllegalArgumentException
* if property value string is not a boolean value
*/
@Override
default boolean getBooleanProperty(String propertyName,
boolean defaultValue) throws IllegalArgumentException {
String booleanString = getStringProperty(propertyName, null);
Expand Down Expand Up @@ -283,7 +270,8 @@ default boolean disableAutomaticServletRegistration() {
* <code>false</code> to not serve Brotli files.
*/
default boolean isBrotli() {
return getBooleanProperty(InitParameters.SERVLET_PARAMETER_BROTLI, false);
return getBooleanProperty(InitParameters.SERVLET_PARAMETER_BROTLI,
false);
}

default String getCompiledWebComponentsPath() {
Expand All @@ -295,8 +283,8 @@ default String getCompiledWebComponentsPath() {
* Returns an array with polyfills to be loaded when the app is loaded.
*
* The default value is empty, but it can be changed by setting the
* {@link InitParameters#SERVLET_PARAMETER_POLYFILLS} as a comma separated list
* of JS files to load.
* {@link InitParameters#SERVLET_PARAMETER_POLYFILLS} as a comma separated
* list of JS files to load.
*
* @return polyfills to load
*/
Expand All @@ -313,9 +301,10 @@ default List<String> getPolyfills() {
*
* @return true if dev server should be used
*/
@Override
default boolean enableDevServer() {
return getBooleanProperty(InitParameters.SERVLET_PARAMETER_ENABLE_DEV_SERVER,
true);
return getBooleanProperty(
InitParameters.SERVLET_PARAMETER_ENABLE_DEV_SERVER, true);
}

/**
Expand All @@ -326,8 +315,8 @@ default boolean enableDevServer() {
* @return true if dev server should be reused
*/
default boolean reuseDevServer() {
return getBooleanProperty(InitParameters.SERVLET_PARAMETER_REUSE_DEV_SERVER,
true);
return getBooleanProperty(
InitParameters.SERVLET_PARAMETER_REUSE_DEV_SERVER, true);
}

/**
Expand Down Expand Up @@ -377,7 +366,8 @@ default boolean isEagerServerLoad() {
/**
* Checks if dev mode live reload is enabled or not.
*
* @return {@code true} if dev mode live reload is enabled, {@code false} otherwise
* @return {@code true} if dev mode live reload is enabled, {@code false}
* otherwise
*/
boolean isDevModeLiveReloadEnabled();

Expand All @@ -387,6 +377,7 @@ default boolean isEagerServerLoad() {
* @return {@code true} if enabled, {@code false} if not
* @since 2.2
*/
@Override
default boolean isPnpmEnabled() {
return getBooleanProperty(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM,
Boolean.valueOf(Constants.ENABLE_PNPM_DEFAULT_STRING));
Expand Down
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.vaadin.flow.server;

import java.util.Map;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.function.DeploymentConfiguration;

Expand All @@ -26,8 +28,18 @@
* @author Vaadin Ltd
* @since 1.0
*/
public abstract class AbstractDeploymentConfiguration
implements DeploymentConfiguration {
public abstract class AbstractDeploymentConfiguration extends
AbstractPropertyConfiguration implements DeploymentConfiguration {

/**
* Creates a new configuration based on {@code properties}.
*
* @param properties
* configuration properties
*/
protected AbstractDeploymentConfiguration(Map<String, String> properties) {
super(properties);
}

@Override
public String getUIClassName() {
Expand Down
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);
}

}
Loading

0 comments on commit 01eb234

Please sign in to comment.