Tom So created CAMEL-8118:
-----------------------------

             Summary: BigDecimalPatternFormat overwrites Locale setting
                 Key: CAMEL-8118
                 URL: https://issues.apache.org/jira/browse/CAMEL-8118
             Project: Camel
          Issue Type: Bug
          Components: camel-bindy
    Affects Versions: 2.14.0
            Reporter: Tom So


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: 
https://fisheye6.atlassian.com/browse/camel-git/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/BigDecimalPatternFormat.java?r=2854e18ff985ffb9abaca305038801f6c508f0c4

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:
{quote}
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;
}
{quote}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to