|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.context.support;
|
18 | 18 |
|
19 | 19 | import java.text.MessageFormat;
|
20 |
| -import java.util.HashMap; |
21 | 20 | import java.util.Locale;
|
22 | 21 | import java.util.Map;
|
| 22 | +import java.util.concurrent.ConcurrentHashMap; |
23 | 23 |
|
24 | 24 | import org.apache.commons.logging.Log;
|
25 | 25 | import org.apache.commons.logging.LogFactory;
|
@@ -53,7 +53,7 @@ public abstract class MessageSourceSupport {
|
53 | 53 | * Used for passed-in default messages. MessageFormats for resolved
|
54 | 54 | * codes are cached on a specific basis in subclasses.
|
55 | 55 | */
|
56 |
| - private final Map<String, Map<Locale, MessageFormat>> messageFormatsPerMessage = new HashMap<>(); |
| 56 | + private final Map<String, Map<Locale, MessageFormat>> messageFormatsPerMessage = new ConcurrentHashMap<>(); |
57 | 57 |
|
58 | 58 |
|
59 | 59 | /**
|
@@ -116,32 +116,22 @@ protected String formatMessage(String msg, @Nullable Object[] args, Locale local
|
116 | 116 | if (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args)) {
|
117 | 117 | return msg;
|
118 | 118 | }
|
119 |
| - MessageFormat messageFormat = null; |
120 |
| - synchronized (this.messageFormatsPerMessage) { |
121 |
| - Map<Locale, MessageFormat> messageFormatsPerLocale = this.messageFormatsPerMessage.get(msg); |
122 |
| - if (messageFormatsPerLocale != null) { |
123 |
| - messageFormat = messageFormatsPerLocale.get(locale); |
| 119 | + Map<Locale, MessageFormat> messageFormatsPerLocale = this.messageFormatsPerMessage |
| 120 | + .computeIfAbsent(msg, key -> new ConcurrentHashMap<>()); |
| 121 | + MessageFormat messageFormat = messageFormatsPerLocale.computeIfAbsent(locale, key -> { |
| 122 | + try { |
| 123 | + return createMessageFormat(msg, locale); |
124 | 124 | }
|
125 |
| - else { |
126 |
| - messageFormatsPerLocale = new HashMap<>(); |
127 |
| - this.messageFormatsPerMessage.put(msg, messageFormatsPerLocale); |
128 |
| - } |
129 |
| - if (messageFormat == null) { |
130 |
| - try { |
131 |
| - messageFormat = createMessageFormat(msg, locale); |
132 |
| - } |
133 |
| - catch (IllegalArgumentException ex) { |
134 |
| - // Invalid message format - probably not intended for formatting, |
135 |
| - // rather using a message structure with no arguments involved... |
136 |
| - if (isAlwaysUseMessageFormat()) { |
137 |
| - throw ex; |
138 |
| - } |
139 |
| - // Silently proceed with raw message if format not enforced... |
140 |
| - messageFormat = INVALID_MESSAGE_FORMAT; |
| 125 | + catch (IllegalArgumentException ex) { |
| 126 | + // Invalid message format - probably not intended for formatting, |
| 127 | + // rather using a message structure with no arguments involved... |
| 128 | + if (isAlwaysUseMessageFormat()) { |
| 129 | + throw ex; |
141 | 130 | }
|
142 |
| - messageFormatsPerLocale.put(locale, messageFormat); |
| 131 | + // Silently proceed with raw message if format not enforced... |
| 132 | + return INVALID_MESSAGE_FORMAT; |
143 | 133 | }
|
144 |
| - } |
| 134 | + }); |
145 | 135 | if (messageFormat == INVALID_MESSAGE_FORMAT) {
|
146 | 136 | return msg;
|
147 | 137 | }
|
|
0 commit comments