INT-3715: Fix Thread Safety in SyslogTransformer
JIRA: https://jira.spring.io/browse/INT-3715 `SimpleDateFormat` is not thread-safe.
This commit is contained in:
committed by
Artem Bilan
parent
c1a6041da3
commit
696ed10aa2
@@ -22,6 +22,8 @@ import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -40,6 +42,8 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, Map<String, ?>> {
|
||||
|
||||
private static final BlockingQueue<SimpleDateFormat> dateFormats = new LinkedBlockingQueue<SimpleDateFormat>();
|
||||
|
||||
public static final String FACILITY = "FACILITY";
|
||||
|
||||
public static final String SEVERITY = "SEVERITY";
|
||||
@@ -56,8 +60,6 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, M
|
||||
|
||||
private final Pattern pattern = Pattern.compile("<([^>]+)>(.{15}) ([^ ]+) (?:([^:]+): )?(.*)", Pattern.DOTALL);
|
||||
|
||||
private final SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd HH:mm:ss");
|
||||
|
||||
@Override
|
||||
protected Map<String, ?> transformPayload(Object payload) throws Exception {
|
||||
boolean isByteArray = payload instanceof byte[];
|
||||
@@ -86,6 +88,10 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, M
|
||||
private Map<String, ?> transform(String payload) {
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
Matcher matcher = pattern.matcher(payload);
|
||||
SimpleDateFormat dateFormat = dateFormats.poll();
|
||||
if (dateFormat == null) {
|
||||
dateFormat = new SimpleDateFormat("MMM dd HH:mm:ss");
|
||||
}
|
||||
if (matcher.matches()) {
|
||||
try {
|
||||
String facilityString = matcher.group(1);
|
||||
@@ -97,7 +103,8 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, M
|
||||
String timestamp = matcher.group(2);
|
||||
Date date;
|
||||
try {
|
||||
date = this.dateFormat.parse(timestamp);
|
||||
date = dateFormat.parse(timestamp);
|
||||
dateFormats.offer(dateFormat);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
int year = calendar.get(Calendar.YEAR);
|
||||
int month = calendar.get(Calendar.MONTH);
|
||||
|
||||
Reference in New Issue
Block a user