You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LegacyPropertyHelper.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.util;
  17. import java.io.Serializable;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.vaadin.data.Property;
  21. import com.vaadin.server.Constants;
  22. import com.vaadin.server.DeploymentConfiguration.LegacyProperyToStringMode;
  23. import com.vaadin.server.VaadinService;
  24. /**
  25. * Helper class which provides methods for handling Property.toString in a
  26. * Vaadin 6 compatible way
  27. *
  28. * @author Vaadin Ltd
  29. * @since 7.1
  30. * @deprecated This is only used internally for backwards compatibility
  31. */
  32. @Deprecated
  33. public class LegacyPropertyHelper implements Serializable {
  34. /**
  35. * Returns the property value converted to a String.
  36. *
  37. * @param p
  38. * The property
  39. * @return A string representation of the property value, compatible with
  40. * how Property implementations in Vaadin 6 do it
  41. */
  42. public static String legacyPropertyToString(Property p) {
  43. maybeLogLegacyPropertyToStringWarning(p);
  44. Object value = p.getValue();
  45. if (value == null) {
  46. return null;
  47. }
  48. return value.toString();
  49. }
  50. public static void maybeLogLegacyPropertyToStringWarning(Property p) {
  51. if (!logLegacyToStringWarning()) {
  52. return;
  53. }
  54. getLogger().log(Level.WARNING,
  55. Constants.WARNING_LEGACY_PROPERTY_TOSTRING,
  56. p.getClass().getName());
  57. if (getLogger().isLoggable(Level.FINE)) {
  58. getLogger().log(Level.FINE,
  59. "Strack trace for legacy toString to ease debugging",
  60. new Throwable());
  61. }
  62. }
  63. /**
  64. * Checks if legacy Property.toString() implementation is enabled. The
  65. * legacy Property.toString() will return the value of the property somehow
  66. * converted to a String. If the legacy mode is disabled, toString() will
  67. * return super.toString().
  68. * <p>
  69. * The legacy toString mode can be toggled using the
  70. * "legacyPropertyToString" init parameter
  71. * </p>
  72. *
  73. * @return true if legacy Property.toString() mode is enabled, false
  74. * otherwise
  75. */
  76. public static boolean isLegacyToStringEnabled() {
  77. if (VaadinService.getCurrent() == null) {
  78. // This will happen at least in JUnit tests. We do not what the real
  79. // value should be but it seems more safe to use the legacy mode.
  80. return true;
  81. }
  82. return VaadinService.getCurrent().getDeploymentConfiguration()
  83. .getLegacyPropertyToStringMode().useLegacyMode();
  84. }
  85. private static boolean logLegacyToStringWarning() {
  86. if (VaadinService.getCurrent() == null) {
  87. // This will happen at least in JUnit tests. We do not want to spam
  88. // the log with these messages in this case.
  89. return false;
  90. }
  91. return VaadinService.getCurrent().getDeploymentConfiguration()
  92. .getLegacyPropertyToStringMode() == LegacyProperyToStringMode.WARNING;
  93. }
  94. private static Logger getLogger() {
  95. return Logger.getLogger(LegacyPropertyHelper.class.getName());
  96. }
  97. }