From fc18848319f680b664979cca472ada0e0857a79c Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Sun, 21 Sep 2014 13:43:28 +0300 Subject: [PATCH] Dedicated methods for init params in DeploymentConfiguration (#12087). Change-Id: I3610814509f38ed4c8789de52cc53e7b19a4c4a2 --- .../AbstractDeploymentConfiguration.java | 54 ++++++ .../DefaultDeploymentConfiguration.java | 3 +- .../com/vaadin/server/DefaultUIProvider.java | 12 +- .../server/DeploymentConfiguration.java | 32 ++++ .../vaadin/server/ServletPortletHelper.java | 6 +- .../vaadin/server/VaadinPortletService.java | 4 +- .../src/com/vaadin/server/VaadinService.java | 2 +- .../vaadin/server/VaadinServletService.java | 7 +- .../AbstractDeploymentConfigurationTest.java | 157 ++++++++++++++++++ .../server/VaadinPortletServiceTests.java | 5 +- .../util/MockDeploymentConfiguration.java | 5 +- 11 files changed, 258 insertions(+), 29 deletions(-) create mode 100644 server/src/com/vaadin/server/AbstractDeploymentConfiguration.java create mode 100644 server/tests/src/com/vaadin/server/AbstractDeploymentConfigurationTest.java diff --git a/server/src/com/vaadin/server/AbstractDeploymentConfiguration.java b/server/src/com/vaadin/server/AbstractDeploymentConfiguration.java new file mode 100644 index 0000000000..43d4570d90 --- /dev/null +++ b/server/src/com/vaadin/server/AbstractDeploymentConfiguration.java @@ -0,0 +1,54 @@ +/* + * Copyright 2000-2014 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.server; + +/** + * An abstract base class for DeploymentConfiguration implementations. This + * class provides default implementation for common config properties. + * + * @author Vaadin Ltd + */ +public abstract class AbstractDeploymentConfiguration implements + DeploymentConfiguration { + + @Override + public String getUIClassName() { + return getApplicationOrSystemProperty(VaadinSession.UI_PARAMETER, null); + } + + @Override + public String getUIProviderClassName() { + return getApplicationOrSystemProperty( + Constants.SERVLET_PARAMETER_UI_PROVIDER, null); + } + + @Override + public String getWidgetset(String defaultValue) { + return getApplicationOrSystemProperty(Constants.PARAMETER_WIDGETSET, + defaultValue); + } + + @Override + public String getResourcesPath() { + return getApplicationOrSystemProperty( + Constants.PARAMETER_VAADIN_RESOURCES, null); + } + + @Override + public String getClassLoaderName() { + return getApplicationOrSystemProperty("ClassLoader", null); + } +} diff --git a/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java b/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java index fd14c3cd3f..22d5210eaa 100644 --- a/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java +++ b/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java @@ -29,7 +29,8 @@ import com.vaadin.shared.communication.PushMode; * @author Vaadin Ltd * @since 7.0.0 */ -public class DefaultDeploymentConfiguration implements DeploymentConfiguration { +public class DefaultDeploymentConfiguration extends + AbstractDeploymentConfiguration { /** * Default value for {@link #getResourceCacheTime()} = {@value} . */ diff --git a/server/src/com/vaadin/server/DefaultUIProvider.java b/server/src/com/vaadin/server/DefaultUIProvider.java index 2a1a59dbe6..38525fc020 100644 --- a/server/src/com/vaadin/server/DefaultUIProvider.java +++ b/server/src/com/vaadin/server/DefaultUIProvider.java @@ -24,15 +24,9 @@ public class DefaultUIProvider extends UIProvider { public Class getUIClass(UIClassSelectionEvent event) { VaadinRequest request = event.getRequest(); - Object uiClassNameObj = request - .getService() - .getDeploymentConfiguration() - .getApplicationOrSystemProperty(VaadinSession.UI_PARAMETER, - null); - - if (uiClassNameObj instanceof String) { - String uiClassName = uiClassNameObj.toString(); - + String uiClassName = request.getService().getDeploymentConfiguration() + .getUIClassName(); + if (uiClassName != null) { ClassLoader classLoader = request.getService().getClassLoader(); try { Class uiClass = Class.forName(uiClassName, true, diff --git a/server/src/com/vaadin/server/DeploymentConfiguration.java b/server/src/com/vaadin/server/DeploymentConfiguration.java index fcfeecc31f..3124729773 100644 --- a/server/src/com/vaadin/server/DeploymentConfiguration.java +++ b/server/src/com/vaadin/server/DeploymentConfiguration.java @@ -162,6 +162,38 @@ public interface DeploymentConfiguration extends Serializable { public String getApplicationOrSystemProperty(String propertyName, String defaultValue); + /** + * Gets UI class configuration option value. + * + * @return UI class name + */ + public String getUIClassName(); + + /** + * Gets UI provider class configuration option value. + * + * @return UI class name + */ + public String getUIProviderClassName(); + + /** + * Gets Widgetset configuration option value. {@code defaultValue} is + * returned if widgetset parameter is not configured. + * + * @return UI class name + */ + public String getWidgetset(String defaultValue); + + /** + * Gets resources path configuration option value. + */ + public String getResourcesPath(); + + /** + * Gets class loader configuration option value. + */ + public String getClassLoaderName(); + /** * Returns to legacy Property.toString() mode used. See * {@link AbstractProperty#isLegacyToStringEnabled()} for more information. diff --git a/server/src/com/vaadin/server/ServletPortletHelper.java b/server/src/com/vaadin/server/ServletPortletHelper.java index 2ec747ba3a..197d9fe416 100644 --- a/server/src/com/vaadin/server/ServletPortletHelper.java +++ b/server/src/com/vaadin/server/ServletPortletHelper.java @@ -130,8 +130,7 @@ public class ServletPortletHelper implements Serializable { public static void initDefaultUIProvider(VaadinSession session, VaadinService vaadinService) throws ServiceException { String uiProperty = vaadinService.getDeploymentConfiguration() - .getApplicationOrSystemProperty(VaadinSession.UI_PARAMETER, - null); + .getUIClassName(); // Add provider for UI parameter first to give it lower priority // (providers are FILO) @@ -141,8 +140,7 @@ public class ServletPortletHelper implements Serializable { } String uiProviderProperty = vaadinService.getDeploymentConfiguration() - .getApplicationOrSystemProperty( - Constants.SERVLET_PARAMETER_UI_PROVIDER, null); + .getUIProviderClassName(); // Then add custom UI provider if defined if (uiProviderProperty != null) { UIProvider uiProvider = getUIProvider(uiProviderProperty, diff --git a/server/src/com/vaadin/server/VaadinPortletService.java b/server/src/com/vaadin/server/VaadinPortletService.java index c6d9b8e46a..cff024672c 100644 --- a/server/src/com/vaadin/server/VaadinPortletService.java +++ b/server/src/com/vaadin/server/VaadinPortletService.java @@ -124,9 +124,7 @@ public class VaadinPortletService extends VaadinService { @Override public String getConfiguredWidgetset(VaadinRequest request) { - String widgetset = getDeploymentConfiguration() - .getApplicationOrSystemProperty( - VaadinPortlet.PARAMETER_WIDGETSET, null); + String widgetset = getDeploymentConfiguration().getWidgetset(null); if (widgetset == null) { widgetset = getParameter(request, diff --git a/server/src/com/vaadin/server/VaadinService.java b/server/src/com/vaadin/server/VaadinService.java index 8fd6da8dee..008ba9c1c8 100644 --- a/server/src/com/vaadin/server/VaadinService.java +++ b/server/src/com/vaadin/server/VaadinService.java @@ -155,7 +155,7 @@ public abstract class VaadinService implements Serializable { this.deploymentConfiguration = deploymentConfiguration; final String classLoaderName = getDeploymentConfiguration() - .getApplicationOrSystemProperty("ClassLoader", null); + .getClassLoaderName(); if (classLoaderName != null) { try { final Class classLoaderClass = getClass().getClassLoader() diff --git a/server/src/com/vaadin/server/VaadinServletService.java b/server/src/com/vaadin/server/VaadinServletService.java index a4ff3943c9..8946ac4fae 100644 --- a/server/src/com/vaadin/server/VaadinServletService.java +++ b/server/src/com/vaadin/server/VaadinServletService.java @@ -118,9 +118,7 @@ public class VaadinServletService extends VaadinService { VaadinServletRequest servletRequest = (VaadinServletRequest) request; String staticFileLocation; // if property is defined in configurations, use that - staticFileLocation = getDeploymentConfiguration() - .getApplicationOrSystemProperty( - VaadinServlet.PARAMETER_VAADIN_RESOURCES, null); + staticFileLocation = getDeploymentConfiguration().getResourcesPath(); if (staticFileLocation != null) { return staticFileLocation; } @@ -159,8 +157,7 @@ public class VaadinServletService extends VaadinService { @Override public String getConfiguredWidgetset(VaadinRequest request) { - return getDeploymentConfiguration().getApplicationOrSystemProperty( - VaadinServlet.PARAMETER_WIDGETSET, + return getDeploymentConfiguration().getWidgetset( VaadinServlet.DEFAULT_WIDGETSET); } diff --git a/server/tests/src/com/vaadin/server/AbstractDeploymentConfigurationTest.java b/server/tests/src/com/vaadin/server/AbstractDeploymentConfigurationTest.java new file mode 100644 index 0000000000..7370bd3fac --- /dev/null +++ b/server/tests/src/com/vaadin/server/AbstractDeploymentConfigurationTest.java @@ -0,0 +1,157 @@ +/* + * Copyright 2000-2014 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.server; + +import java.util.Properties; +import java.util.UUID; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.shared.communication.PushMode; + +/** + * Test for {@link AbstractDeploymentConfiguration} + * + * @author Vaadin Ltd + */ +public class AbstractDeploymentConfigurationTest { + + @Test + public void getUIClass_returnsUIParameterPropertyValue() { + String ui = UUID.randomUUID().toString(); + DeploymentConfiguration config = getConfig(VaadinSession.UI_PARAMETER, + ui); + Assert.assertEquals("Unexpected UI class configuration option value", + ui, config.getUIClassName()); + } + + @Test + public void getUIProviderClass_returnsUIProviderPropertyValue() { + String uiProvider = UUID.randomUUID().toString(); + DeploymentConfiguration config = getConfig( + Constants.SERVLET_PARAMETER_UI_PROVIDER, uiProvider); + Assert.assertEquals( + "Unexpected UI providerclass configuration option value", + uiProvider, config.getUIProviderClassName()); + } + + @Test + public void getWidgetset_returnsWidgetsetProviderPropertyValue() { + String widgetset = UUID.randomUUID().toString(); + DeploymentConfiguration config = getConfig( + Constants.PARAMETER_WIDGETSET, widgetset); + Assert.assertEquals("Unexpected widgetset configuration option value", + widgetset, config.getWidgetset(null)); + } + + @Test + public void getWidgetset_noWidgetsetPropertyValue_returnsProvidedDefaultValue() { + DeploymentConfiguration config = getConfig(null, null); + String widgetset = UUID.randomUUID().toString(); + Assert.assertEquals("Unexpected widgetset configuration option value", + widgetset, config.getWidgetset(widgetset)); + } + + @Test + public void getResourcesPath_returnsResourcesPathPropertyValue() { + String resources = UUID.randomUUID().toString(); + DeploymentConfiguration config = getConfig( + Constants.PARAMETER_VAADIN_RESOURCES, resources); + Assert.assertEquals( + "Unexpected resources path configuration option value", + resources, config.getResourcesPath()); + } + + @Test + public void getClassLoader_returnsClassloaderPropertyValue() { + String classLoader = UUID.randomUUID().toString(); + DeploymentConfiguration config = getConfig("ClassLoader", classLoader); + Assert.assertEquals( + "Unexpected classLoader configuration option value", + classLoader, config.getClassLoaderName()); + } + + private DeploymentConfiguration getConfig(String property, String value) { + Properties props = new Properties(); + if (property != null) { + props.put(property, value); + } + return new DeploymentConfigImpl(props); + } + + private static class DeploymentConfigImpl extends + AbstractDeploymentConfiguration { + + private Properties properties; + + DeploymentConfigImpl(Properties props) { + properties = props; + } + + @Override + public boolean isProductionMode() { + return false; + } + + @Override + public boolean isXsrfProtectionEnabled() { + return false; + } + + @Override + public boolean isSyncIdCheckEnabled() { + return false; + } + + @Override + public int getResourceCacheTime() { + return 0; + } + + @Override + public int getHeartbeatInterval() { + return 0; + } + + @Override + public boolean isCloseIdleSessions() { + return false; + } + + @Override + public PushMode getPushMode() { + return null; + } + + @Override + public Properties getInitParameters() { + return null; + } + + @Override + public String getApplicationOrSystemProperty(String propertyName, + String defaultValue) { + return properties.getProperty(propertyName, defaultValue); + } + + @Override + public LegacyProperyToStringMode getLegacyPropertyToStringMode() { + return null; + } + + } +} diff --git a/server/tests/src/com/vaadin/server/VaadinPortletServiceTests.java b/server/tests/src/com/vaadin/server/VaadinPortletServiceTests.java index f7a69c2edb..0e094bfabb 100644 --- a/server/tests/src/com/vaadin/server/VaadinPortletServiceTests.java +++ b/server/tests/src/com/vaadin/server/VaadinPortletServiceTests.java @@ -86,10 +86,7 @@ public class VaadinPortletServiceTests { } private void mockWidgetsetConfiguration(String widgetset) { - when( - conf.getApplicationOrSystemProperty( - Constants.PARAMETER_WIDGETSET, null)).thenReturn( - widgetset); + when(conf.getWidgetset(null)).thenReturn(widgetset); } @Test diff --git a/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java b/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java index fe7f9ba03e..8eceaea53f 100644 --- a/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java +++ b/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java @@ -4,10 +4,11 @@ import java.util.HashMap; import java.util.Map; import java.util.Properties; -import com.vaadin.server.DeploymentConfiguration; +import com.vaadin.server.AbstractDeploymentConfiguration; import com.vaadin.shared.communication.PushMode; -public class MockDeploymentConfiguration implements DeploymentConfiguration { +public class MockDeploymentConfiguration extends + AbstractDeploymentConfiguration { private boolean productionMode = false; private boolean xsrfProtectionEnabled = true; -- 2.39.5