Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-8118

BigDecimalPatternFormat overwrites Locale setting

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.14.0
    • Fix Version/s: 2.14.1, 2.15.0
    • Component/s: camel-bindy
    • Labels:
      None
    • Estimated Complexity:
      Novice

      Description

      Because of this change request (https://issues.apache.org/jira/browse/CAMEL-7742) was a new feature implemented that allows pattern annotations for BigDecimal fields in CSV model classes for the Camel Bindy component.

      The problem with that is, that the usage of this feature overwrites the current Locale setting of the environment. For example, if the current Locale was set to "German" and the provided pattern for the BigDecimal field requires "US" to unmarshal the numbers in the CSV file, then the method BigDecimalPatternFormat#parse(String) overwrites the Locale, but doesn't restore the former setting. This can cause problems for other software components that depends on the Locale setting.

      The cause of the problem can be found here on line 21: Source of BigDecimalPatternFormat

      A possible workaround would be storing the current locale temporarily, overwrite that setting, perform the formatting task and then restore the former locale setting.
      Like in this code example:

      if (getNumberFormat() != null) {
          final Locale currentLocale = Locale.getDefault();
          Locale.setDefault(super.getLocale());
          DecimalFormat df = (DecimalFormat)getNumberFormat();
          df.setParseBigDecimal(true);
          BigDecimal bd = (BigDecimal)df.parse(string.trim());
          if(super.getPrecision() != -1) {
              bd = bd.setScale(super.getPrecision(), RoundingMode.valueOf(super.getRounding()));
          }
          Locale.getDefault(); // what is the purpose of this line?
          Locale.setDefault(currentLocale); // restore the Locale setting
          return bd;
      }
      

        Attachments

          Activity

            People

            • Assignee:
              davsclaus Claus Ibsen
              Reporter:
              TomSO Tom So
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: