diff options
-rw-r--r-- | WebContent/WEB-INF/web.xml | 4 | ||||
-rw-r--r-- | server/src/com/vaadin/data/util/AbstractProperty.java | 37 | ||||
-rw-r--r-- | server/src/com/vaadin/data/util/IndexedContainer.java | 38 | ||||
-rw-r--r-- | server/src/com/vaadin/data/util/LegacyPropertyHelper.java | 102 | ||||
-rw-r--r-- | server/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java | 37 | ||||
-rw-r--r-- | server/src/com/vaadin/server/Constants.java | 14 | ||||
-rw-r--r-- | server/src/com/vaadin/server/DefaultDeploymentConfiguration.java | 29 | ||||
-rw-r--r-- | server/src/com/vaadin/server/DeploymentConfiguration.java | 27 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/AbstractField.java | 38 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/Label.java | 33 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java | 19 |
11 files changed, 325 insertions, 53 deletions
diff --git a/WebContent/WEB-INF/web.xml b/WebContent/WEB-INF/web.xml index 3915898293..fa1f40248b 100644 --- a/WebContent/WEB-INF/web.xml +++ b/WebContent/WEB-INF/web.xml @@ -50,6 +50,10 @@ <servlet-class>com.vaadin.launcher.ApplicationRunnerServlet</servlet-class> <!-- Non-default values for testing purposes --> <init-param> + <param-name>legacyPropertyToString</param-name> + <param-value>false</param-value> + </init-param> + <init-param> <param-name>heartbeatInterval</param-name> <param-value>301</param-value> </init-param> diff --git a/server/src/com/vaadin/data/util/AbstractProperty.java b/server/src/com/vaadin/data/util/AbstractProperty.java index 499421a8b4..903f2f50f2 100644 --- a/server/src/com/vaadin/data/util/AbstractProperty.java +++ b/server/src/com/vaadin/data/util/AbstractProperty.java @@ -18,7 +18,6 @@ package com.vaadin.data.util; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; -import java.util.logging.Level; import java.util.logging.Logger; import com.vaadin.data.Property; @@ -71,27 +70,33 @@ public abstract class AbstractProperty<T> implements Property<T>, } /** - * Returns the value of the <code>Property</code> in human readable textual - * format. + * Returns a string representation of this object. The returned string + * representation depends on if the legacy Property toString mode is enabled + * or disabled. + * <p> + * If legacy Property toString mode is enabled, returns the value of the + * <code>Property</code> converted to a String. + * </p> + * <p> + * If legacy Property toString mode is disabled, the string representation + * has no special meaning + * </p> * - * @return String representation of the value stored in the Property - * @deprecated As of 7.0, use {@link #getValue()} instead and possibly - * toString on that + * @see LegacyPropertyHelper#isLegacyToStringEnabled() + * + * @return A string representation of the value value stored in the Property + * or a string representation of the Property object. + * @deprecated As of 7.0. To get the property value, use {@link #getValue()} + * instead (and possibly toString on that) */ @Deprecated @Override public String toString() { - getLogger() - .log(Level.WARNING, - "You are using Property.toString() instead of getValue() to get the value for a {0}." - + "This will not be supported starting from Vaadin 7.1 " - + "(your debugger might call toString() and cause this message to appear).", - getClass().getSimpleName()); - T v = getValue(); - if (v == null) { - return null; + if (!LegacyPropertyHelper.isLegacyToStringEnabled()) { + return super.toString(); + } else { + return LegacyPropertyHelper.legacyPropertyToString(this); } - return v.toString(); } /* Events */ diff --git a/server/src/com/vaadin/data/util/IndexedContainer.java b/server/src/com/vaadin/data/util/IndexedContainer.java index a54bf26e52..31f37ac277 100644 --- a/server/src/com/vaadin/data/util/IndexedContainer.java +++ b/server/src/com/vaadin/data/util/IndexedContainer.java @@ -28,7 +28,6 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.logging.Level; import java.util.logging.Logger; import com.vaadin.data.Container; @@ -954,29 +953,32 @@ public class IndexedContainer extends } /** - * Returns the value of the Property in human readable textual format. - * The return value should be assignable to the <code>setValue</code> - * method if the Property is not in read-only mode. + * Returns a string representation of this object. The returned string + * representation depends on if the legacy Property toString mode is + * enabled or disabled. + * <p> + * If legacy Property toString mode is enabled, returns the value of the + * <code>Property</code> converted to a String. + * </p> + * <p> + * If legacy Property toString mode is disabled, the string + * representation has no special meaning + * </p> * - * @return <code>String</code> representation of the value stored in the - * Property - * @deprecated As of 7.0, use {@link #getValue()} instead and possibly - * toString on that + * @return A string representation of the value value stored in the + * Property or a string representation of the Property object. + * @deprecated As of 7.0. To get the property value, use + * {@link #getValue()} instead (and possibly toString on + * that) */ @Deprecated @Override public String toString() { - getLogger() - .log(Level.WARNING, - "You are using IndexedContainerProperty.toString() instead of getValue() to get the value for a {0}." - + " This will not be supported starting from Vaadin 7.1 " - + "(your debugger might call toString() and cause this message to appear).", - getClass().getSimpleName()); - Object v = getValue(); - if (v == null) { - return null; + if (!LegacyPropertyHelper.isLegacyToStringEnabled()) { + return super.toString(); + } else { + return LegacyPropertyHelper.legacyPropertyToString(this); } - return v.toString(); } private Logger getLogger() { diff --git a/server/src/com/vaadin/data/util/LegacyPropertyHelper.java b/server/src/com/vaadin/data/util/LegacyPropertyHelper.java new file mode 100644 index 0000000000..0276e35dbf --- /dev/null +++ b/server/src/com/vaadin/data/util/LegacyPropertyHelper.java @@ -0,0 +1,102 @@ +/* + * Copyright 2000-2013 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.data.util; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.vaadin.data.Property; +import com.vaadin.server.Constants; +import com.vaadin.server.DeploymentConfiguration.LegacyProperyToStringMode; +import com.vaadin.server.VaadinService; + +/** + * Helper class which provides methods for handling Property.toString in a + * Vaadin 6 compatible way + * + * @author Vaadin Ltd + * @since 7.1 + * @deprecated This is only used internally for backwards compatibility + */ +@Deprecated +public class LegacyPropertyHelper { + + /** + * Returns the property value converted to a String. + * + * @param p + * The property + * @return A string representation of the property value, compatible with + * how Property implementations in Vaadin 6 do it + */ + public static String legacyPropertyToString(Property p) { + maybeLogLegacyPropertyToStringWarning(p); + Object value = p.getValue(); + if (value == null) { + return null; + } + return value.toString(); + } + + public static void maybeLogLegacyPropertyToStringWarning(Property p) { + if (!logLegacyToStringWarning()) { + return; + } + + getLogger().log(Level.WARNING, + Constants.WARNING_LEGACY_PROPERTY_TOSTRING, + p.getClass().getName()); + } + + /** + * Checks if legacy Property.toString() implementation is enabled. The + * legacy Property.toString() will return the value of the property somehow + * converted to a String. If the legacy mode is disabled, toString() will + * return super.toString(). + * <p> + * The legacy toString mode can be toggled using the + * "legacyPropertyToString" init parameter + * </p> + * + * @return true if legacy Property.toString() mode is enabled, false + * otherwise + */ + public static boolean isLegacyToStringEnabled() { + if (VaadinService.getCurrent() == null) { + // This should really not happen but we need to handle it somehow. + // IF it happens it seems more safe to use the legacy mode and log. + return true; + } + return VaadinService.getCurrent().getDeploymentConfiguration() + .getLegacyPropertyToStringMode().useLegacyMode(); + } + + private static boolean logLegacyToStringWarning() { + if (VaadinService.getCurrent() == null) { + // This should really not happen but we need to handle it somehow. + // IF it happens it seems more safe to use the legacy mode and log. + return true; + } + return VaadinService.getCurrent().getDeploymentConfiguration() + .getLegacyPropertyToStringMode() == LegacyProperyToStringMode.WARNING; + + } + + private static Logger getLogger() { + return Logger.getLogger(LegacyPropertyHelper.class.getName()); + } + +} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java b/server/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java index 378372d044..d8448a2b50 100644 --- a/server/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java +++ b/server/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java @@ -18,10 +18,10 @@ package com.vaadin.data.util.sqlcontainer; import java.sql.Date; import java.sql.Time; import java.sql.Timestamp; -import java.util.logging.Level; import java.util.logging.Logger; import com.vaadin.data.Property; +import com.vaadin.data.util.LegacyPropertyHelper; import com.vaadin.data.util.converter.Converter.ConversionException; /** @@ -255,26 +255,33 @@ final public class ColumnProperty implements Property { } /** - * Returns the value of the Property in human readable textual format. + * Returns a string representation of this object. The returned string + * representation depends on if the legacy Property toString mode is enabled + * or disabled. + * <p> + * If legacy Property toString mode is enabled, returns the value of this + * <code>Property</code> converted to a String. + * </p> + * <p> + * If legacy Property toString mode is disabled, the string representation + * has no special meaning + * </p> * - * @see java.lang.Object#toString() - * @deprecated As of 7.0, use {@link #getValue()} instead and possibly - * toString on that + * @see LegacyPropertyHelper#isLegacyToStringEnabled() + * + * @return A string representation of the value value stored in the Property + * or a string representation of the Property object. + * @deprecated As of 7.0. To get the property value, use {@link #getValue()} + * instead (and possibly toString on that) */ @Deprecated @Override public String toString() { - getLogger() - .log(Level.WARNING, - "You are using ColumnProperty.toString() instead of getValue() to get the value for a {0}. " - + "This will not be supported starting from Vaadin 7.1 (your debugger might call toString() " - + "and cause this message to appear).", - getClass().getSimpleName()); - Object v = getValue(); - if (v == null) { - return null; + if (!LegacyPropertyHelper.isLegacyToStringEnabled()) { + return super.toString(); + } else { + return LegacyPropertyHelper.legacyPropertyToString(this); } - return v.toString(); } private static Logger getLogger() { diff --git a/server/src/com/vaadin/server/Constants.java b/server/src/com/vaadin/server/Constants.java index 7636c43c57..f8d8105286 100644 --- a/server/src/com/vaadin/server/Constants.java +++ b/server/src/com/vaadin/server/Constants.java @@ -99,6 +99,19 @@ public interface Constants { + ".\n" + "================================================================="; + public static final String WARNING_LEGACY_PROPERTY_TOSTRING = "You are using toString() instead of getValue() to get the value for a Property of type {0}" + + ". This is strongly discouraged and only provided for backwards compatibility with Vaadin 6. " + + "To disable this warning message and retain the behavior, set the init parameter \"" + + Constants.SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING + + "\" to \"true\". To disable the legacy functionality, set \"" + + Constants.SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING + + "\" to false." + + " (Note that your debugger might call toString() and trigger this message)."; + + static final String WARNING_UNKNOWN_LEGACY_PROPERTY_TOSTRING_VALUE = "Unknown value '{0}' for parameter " + + Constants.SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING + + ". Supported values are 'false','warning','true'"; + static final String URL_PARAMETER_THEME = "theme"; static final String SERVLET_PARAMETER_PRODUCTION_MODE = "productionMode"; @@ -108,6 +121,7 @@ public interface Constants { static final String SERVLET_PARAMETER_CLOSE_IDLE_SESSIONS = "closeIdleSessions"; static final String SERVLET_PARAMETER_PUSH_MODE = "pushMode"; static final String SERVLET_PARAMETER_UI_PROVIDER = "UIProvider"; + static final String SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING = "legacyPropertyToString"; // Configurable parameter names static final String PARAMETER_VAADIN_RESOURCES = "Resources"; diff --git a/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java b/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java index d11bd69997..80c3644d77 100644 --- a/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java +++ b/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java @@ -17,6 +17,7 @@ package com.vaadin.server; import java.util.Properties; +import java.util.logging.Level; import java.util.logging.Logger; import com.vaadin.shared.communication.PushMode; @@ -37,6 +38,7 @@ public class DefaultDeploymentConfiguration implements DeploymentConfiguration { private boolean closeIdleSessions; private PushMode pushMode; private final Class<?> systemPropertyBaseClass; + private LegacyProperyToStringMode legacyPropertyToStringMode; /** * Create a new deployment configuration instance. @@ -59,6 +61,26 @@ public class DefaultDeploymentConfiguration implements DeploymentConfiguration { checkHeartbeatInterval(); checkCloseIdleSessions(); checkPushMode(); + checkLegacyPropertyToString(); + } + + private void checkLegacyPropertyToString() { + String param = getApplicationOrSystemProperty( + Constants.SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING, "warning"); + if ("true".equals(param)) { + legacyPropertyToStringMode = LegacyProperyToStringMode.ENABLED; + } else if ("false".equals(param)) { + legacyPropertyToStringMode = LegacyProperyToStringMode.DISABLED; + } else { + if (!"warning".equals(param)) { + getLogger() + .log(Level.WARNING, + Constants.WARNING_UNKNOWN_LEGACY_PROPERTY_TOSTRING_VALUE, + param); + } + legacyPropertyToStringMode = LegacyProperyToStringMode.WARNING; + + } } @Override @@ -270,4 +292,11 @@ public class DefaultDeploymentConfiguration implements DeploymentConfiguration { private Logger getLogger() { return Logger.getLogger(getClass().getName()); } + + @Override + @Deprecated + public LegacyProperyToStringMode getLegacyPropertyToStringMode() { + return legacyPropertyToStringMode; + } + } diff --git a/server/src/com/vaadin/server/DeploymentConfiguration.java b/server/src/com/vaadin/server/DeploymentConfiguration.java index 23edf8052a..bf9c019b6d 100644 --- a/server/src/com/vaadin/server/DeploymentConfiguration.java +++ b/server/src/com/vaadin/server/DeploymentConfiguration.java @@ -19,6 +19,7 @@ package com.vaadin.server; import java.io.Serializable; import java.util.Properties; +import com.vaadin.data.util.AbstractProperty; import com.vaadin.shared.communication.PushMode; /** @@ -30,6 +31,23 @@ import com.vaadin.shared.communication.PushMode; * @since 7.0.0 */ public interface DeploymentConfiguration extends Serializable { + + /** + * Determines the mode of the "legacyPropertyToString" parameter. + * + * @author Vaadin Ltd + * @since 7.1 + */ + @Deprecated + public enum LegacyProperyToStringMode { + DISABLED, WARNING, ENABLED; + + public boolean useLegacyMode() { + return this == WARNING || this == ENABLED; + } + + } + /** * Returns whether Vaadin is in production mode. * @@ -111,4 +129,13 @@ public interface DeploymentConfiguration extends Serializable { public String getApplicationOrSystemProperty(String propertyName, String defaultValue); + /** + * Returns to legacy Property.toString() mode used. See + * {@link AbstractProperty#isLegacyToStringEnabled()} for more information. + * + * @return The Property.toString() mode in use. + */ + @Deprecated + public LegacyProperyToStringMode getLegacyPropertyToStringMode(); + } diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java index eb8fc30a45..6648f69ff9 100644 --- a/server/src/com/vaadin/ui/AbstractField.java +++ b/server/src/com/vaadin/ui/AbstractField.java @@ -25,13 +25,13 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Locale; -import java.util.logging.Logger; import com.vaadin.data.Buffered; import com.vaadin.data.Property; import com.vaadin.data.Validatable; import com.vaadin.data.Validator; import com.vaadin.data.Validator.InvalidValueException; +import com.vaadin.data.util.LegacyPropertyHelper; import com.vaadin.data.util.converter.Converter; import com.vaadin.data.util.converter.Converter.ConversionException; import com.vaadin.data.util.converter.ConverterUtil; @@ -75,9 +75,6 @@ public abstract class AbstractField<T> extends AbstractComponent implements /* Private members */ - private static final Logger logger = Logger.getLogger(AbstractField.class - .getName()); - /** * Value of the abstract field. */ @@ -371,6 +368,39 @@ public abstract class AbstractField<T> extends AbstractComponent implements return buffered; } + /** + * Returns a string representation of this object. The returned string + * representation depends on if the legacy Property toString mode is enabled + * or disabled. + * <p> + * If legacy Property toString mode is enabled, returns the value of this + * <code>Field</code> converted to a String. + * </p> + * <p> + * If legacy Property toString mode is disabled, the string representation + * has no special meaning + * </p> + * + * @see LegacyPropertyHelper#isLegacyToStringEnabled() + * + * @return A string representation of the value value stored in the Property + * or a string representation of the Property object. + * @deprecated As of 7.0. Use {@link #getValue()} to get the value of the + * field, {@link #getConvertedValue()} to get the field value + * converted to the data model type or + * {@link #getPropertyDataSource()} .getValue() to get the value + * of the data source. + */ + @Deprecated + @Override + public String toString() { + if (!LegacyPropertyHelper.isLegacyToStringEnabled()) { + return super.toString(); + } else { + return LegacyPropertyHelper.legacyPropertyToString(this); + } + } + /* Property interface implementation */ /** diff --git a/server/src/com/vaadin/ui/Label.java b/server/src/com/vaadin/ui/Label.java index 72f556ee5b..d037652a09 100644 --- a/server/src/com/vaadin/ui/Label.java +++ b/server/src/com/vaadin/ui/Label.java @@ -21,6 +21,8 @@ import java.util.Locale; import java.util.logging.Logger; import com.vaadin.data.Property; +import com.vaadin.data.util.AbstractProperty; +import com.vaadin.data.util.LegacyPropertyHelper; import com.vaadin.data.util.converter.Converter; import com.vaadin.data.util.converter.ConverterUtil; import com.vaadin.shared.ui.label.ContentMode; @@ -525,4 +527,35 @@ public class Label extends AbstractComponent implements Property<String>, markAsDirty(); } + /** + * Returns a string representation of this object. The returned string + * representation depends on if the legacy Property toString mode is enabled + * or disabled. + * <p> + * If legacy Property toString mode is enabled, returns the value displayed + * by this label. + * </p> + * <p> + * If legacy Property toString mode is disabled, the string representation + * has no special meaning + * </p> + * + * @see AbstractProperty#isLegacyToStringEnabled() + * + * @return The value displayed by this label or a string representation of + * this Label object. + * + * @deprecated As of 7.0, use {@link #getValue()} to get the value of the + * label or {@link #getPropertyDataSource()}.getValue() to get + * the value of the data source. + */ + @Deprecated + @Override + public String toString() { + if (!LegacyPropertyHelper.isLegacyToStringEnabled()) { + return super.toString(); + } else { + return LegacyPropertyHelper.legacyPropertyToString(this); + } + } } diff --git a/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java b/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java index cd53a564d2..d113efdfaf 100644 --- a/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java +++ b/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java @@ -18,7 +18,9 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration { private PushMode pushMode = PushMode.DISABLED; private Properties initParameters = new Properties(); private Map<String, String> applicationOrSystemProperty = new HashMap<String, String>(); + private LegacyProperyToStringMode legacyPropertyToStringMode = LegacyProperyToStringMode.DISABLED; + @Override public boolean isProductionMode() { return productionMode; } @@ -27,6 +29,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration { this.productionMode = productionMode; } + @Override public boolean isXsrfProtectionEnabled() { return xsrfProtectionEnabled; } @@ -35,6 +38,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration { this.xsrfProtectionEnabled = xsrfProtectionEnabled; } + @Override public int getResourceCacheTime() { return resourceCacheTime; } @@ -43,6 +47,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration { this.resourceCacheTime = resourceCacheTime; } + @Override public int getHeartbeatInterval() { return heartbeatInterval; } @@ -51,6 +56,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration { this.heartbeatInterval = heartbeatInterval; } + @Override public boolean isCloseIdleSessions() { return closeIdleSessions; } @@ -59,6 +65,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration { this.closeIdleSessions = closeIdleSessions; } + @Override public PushMode getPushMode() { return pushMode; } @@ -67,6 +74,7 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration { this.pushMode = pushMode; } + @Override public Properties getInitParameters() { return initParameters; } @@ -89,4 +97,15 @@ public class MockDeploymentConfiguration implements DeploymentConfiguration { } } + @Override + @Deprecated + public LegacyProperyToStringMode getLegacyPropertyToStringMode() { + return legacyPropertyToStringMode; + } + + public void setLegacyPropertyToStringMode( + LegacyProperyToStringMode legacyPropertyToStringMode) { + this.legacyPropertyToStringMode = legacyPropertyToStringMode; + } + } |