Skip to content

Commit fe6c99a

Browse files
tulinkryVladimir Kotal
authored andcommitted
refactoring date formats to be synchronized per repository (#2570)
fixes #2534
1 parent 5fc870e commit fe6c99a

19 files changed

+71
-73
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/AccuRevHistoryParser.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@
2727
import java.io.IOException;
2828
import java.io.InputStream;
2929
import java.io.InputStreamReader;
30-
import java.text.DateFormat;
3130
import java.text.ParseException;
3231
import java.util.ArrayList;
3332
import java.util.Date;
3433
import java.util.List;
35-
3634
import org.opengrok.indexer.util.Executor;
3735

3836
/**
@@ -99,7 +97,6 @@ public void processStream(InputStream input) throws IOException {
9997

10098
BufferedReader in = new BufferedReader(new InputStreamReader(input));
10199
List<HistoryEntry> entries = new ArrayList<HistoryEntry>();
102-
DateFormat df = repository.getDateFormat();
103100
String line;
104101
String user;
105102
Date date;
@@ -143,7 +140,7 @@ public void processStream(InputStream input) throws IOException {
143140
entry.setAuthor(user);
144141

145142
try {
146-
date = df.parse(data[2]);
143+
date = repository.parse(data[2]);
147144
entry.setDate(date);
148145
} catch (ParseException pe) {
149146
//

opengrok-indexer/src/main/java/org/opengrok/indexer/history/BazaarHistoryParser.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030
import java.io.InputStream;
3131
import java.io.InputStreamReader;
3232
import java.nio.file.InvalidPathException;
33-
import java.text.DateFormat;
3433
import java.text.ParseException;
3534
import java.util.ArrayList;
3635
import java.util.Date;
3736
import java.util.List;
3837
import java.util.logging.Level;
3938
import java.util.logging.Logger;
40-
4139
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4240
import org.opengrok.indexer.logger.LoggerFactory;
4341
import org.opengrok.indexer.util.Executor;
@@ -93,7 +91,6 @@ History parse(File file, String sinceRevision) throws HistoryException {
9391
*/
9492
@Override
9593
public void processStream(InputStream input) throws IOException {
96-
DateFormat df = repository.getDateFormat();
9794
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
9895

9996
BufferedReader in = new BufferedReader(new InputStreamReader(input));
@@ -132,7 +129,7 @@ public void processStream(InputStream input) throws IOException {
132129
// And then, look for timestamp.
133130
if (s.startsWith("timestamp:")) {
134131
try {
135-
Date date = df.parse(s.substring("timestamp:".length()).trim());
132+
Date date = repository.parse(s.substring("timestamp:".length()).trim());
136133
entry.setDate(date);
137134
} catch (ParseException e) {
138135
//

opengrok-indexer/src/main/java/org/opengrok/indexer/history/CVSHistoryParser.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828
import java.io.IOException;
2929
import java.io.InputStream;
3030
import java.io.InputStreamReader;
31-
import java.text.DateFormat;
3231
import java.text.ParseException;
3332
import java.util.ArrayList;
3433
import java.util.HashMap;
3534
import java.util.List;
3635
import java.util.logging.Logger;
37-
3836
import org.opengrok.indexer.logger.LoggerFactory;
3937
import org.opengrok.indexer.util.Executor;
4038

@@ -61,7 +59,6 @@ private enum ParseState {
6159
*/
6260
@Override
6361
public void processStream(InputStream input) throws IOException {
64-
DateFormat df = cvsrepo.getDateFormat();
6562
ArrayList<HistoryEntry> entries = new ArrayList<HistoryEntry>();
6663

6764
BufferedReader in = new BufferedReader(new InputStreamReader(input));
@@ -124,7 +121,7 @@ public void processStream(InputStream input) throws IOException {
124121
if ("date".equals(key)) {
125122
try {
126123
val = val.replace('/', '-');
127-
entry.setDate(df.parse(val));
124+
entry.setDate(cvsrepo.parse(val));
128125
} catch (ParseException pe) {
129126
//
130127
// Overriding processStream() thus need to comply with the

opengrok-indexer/src/main/java/org/opengrok/indexer/history/ClearCaseHistoryParser.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828
import java.io.IOException;
2929
import java.io.InputStream;
3030
import java.io.InputStreamReader;
31-
import java.text.DateFormat;
3231
import java.text.ParseException;
3332
import java.util.ArrayList;
3433
import java.util.List;
35-
3634
import org.opengrok.indexer.util.Executor;
3735

3836
/**
@@ -70,7 +68,6 @@ History parse(File file, Repository repos) throws HistoryException {
7068
*/
7169
@Override
7270
public void processStream(InputStream input) throws IOException {
73-
DateFormat df = repository.getDateFormat();
7471
BufferedReader in = new BufferedReader(new InputStreamReader(input));
7572
List<HistoryEntry> entries = new ArrayList<HistoryEntry>();
7673
String s;
@@ -89,7 +86,7 @@ public void processStream(InputStream input) throws IOException {
8986
entry = new HistoryEntry();
9087
if ((s = in.readLine()) != null) {
9188
try {
92-
entry.setDate(df.parse(s));
89+
entry.setDate(repository.parse(s));
9390
} catch (ParseException pe) {
9491
//
9592
// Overriding processStream() thus need to comply with the

opengrok-indexer/src/main/java/org/opengrok/indexer/history/GitHistoryParser.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.io.InputStream;
3232
import java.io.InputStreamReader;
3333
import java.nio.file.InvalidPathException;
34-
import java.text.DateFormat;
3534
import java.text.ParseException;
3635
import java.util.ArrayList;
3736
import java.util.List;
@@ -80,7 +79,6 @@ public void processStream(InputStream input) throws IOException {
8079
}
8180

8281
private void process(BufferedReader in) throws IOException {
83-
DateFormat df = repository.getDateFormat();
8482
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
8583
entries = new ArrayList<>();
8684
HistoryEntry entry = null;
@@ -103,7 +101,7 @@ private void process(BufferedReader in) throws IOException {
103101
String dateString =
104102
s.substring("AuthorDate:".length()).trim();
105103
try {
106-
entry.setDate(df.parse(dateString));
104+
entry.setDate(repository.parse(dateString));
107105
} catch (ParseException pe) {
108106
//
109107
// Overriding processStream() thus need to comply with the

opengrok-indexer/src/main/java/org/opengrok/indexer/history/GitRepository.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,8 @@ public String determineCurrentVersion(boolean interactive) throws IOException {
729729
}
730730

731731
try {
732-
Date date = getDateFormat().parse(output.substring(0, indexOf));
733-
return String.format("%s %s",
734-
outputDateFormat.format(date), output.substring(indexOf + 1));
732+
Date date = parse(output.substring(0, indexOf));
733+
return String.format("%s %s", format(date), output.substring(indexOf + 1));
735734
} catch (ParseException ex) {
736735
throw new IOException(ex);
737736
}

opengrok-indexer/src/main/java/org/opengrok/indexer/history/MercurialHistoryParser.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030
import java.io.InputStream;
3131
import java.io.InputStreamReader;
3232
import java.nio.file.InvalidPathException;
33-
import java.text.DateFormat;
3433
import java.text.ParseException;
3534
import java.util.ArrayList;
3635
import java.util.Date;
3736
import java.util.List;
3837
import java.util.logging.Level;
3938
import java.util.logging.Logger;
40-
4139
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4240
import org.opengrok.indexer.logger.LoggerFactory;
4341
import org.opengrok.indexer.util.Executor;
@@ -112,7 +110,6 @@ History parse(File file, String changeset) throws HistoryException {
112110
@Override
113111
public void processStream(InputStream input) throws IOException {
114112
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
115-
DateFormat df = repository.getDateFormat();
116113
BufferedReader in = new BufferedReader(new InputStreamReader(input));
117114
entries = new ArrayList<HistoryEntry>();
118115
String s;
@@ -128,7 +125,7 @@ public void processStream(InputStream input) throws IOException {
128125
} else if (s.startsWith(MercurialRepository.DATE) && entry != null) {
129126
Date date;
130127
try {
131-
date = df.parse(s.substring(MercurialRepository.DATE.length()).trim());
128+
date = repository.parse(s.substring(MercurialRepository.DATE.length()).trim());
132129
} catch (ParseException pe) {
133130
//
134131
// Overriding processStream() thus need to comply with the

opengrok-indexer/src/main/java/org/opengrok/indexer/history/MonotoneHistoryParser.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030
import java.io.InputStream;
3131
import java.io.InputStreamReader;
3232
import java.nio.file.InvalidPathException;
33-
import java.text.DateFormat;
3433
import java.text.ParseException;
3534
import java.util.ArrayList;
3635
import java.util.Date;
3736
import java.util.List;
3837
import java.util.logging.Level;
3938
import java.util.logging.Logger;
40-
4139
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4240
import org.opengrok.indexer.logger.LoggerFactory;
4341
import org.opengrok.indexer.util.Executor;
@@ -99,7 +97,6 @@ History parse(File file, String changeset) throws HistoryException {
9997
@Override
10098
public void processStream(InputStream input) throws IOException {
10199
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
102-
DateFormat df = repository.getDateFormat();
103100
BufferedReader in = new BufferedReader(new InputStreamReader(input));
104101
String s;
105102

@@ -138,7 +135,7 @@ public void processStream(InputStream input) throws IOException {
138135
if (s.startsWith("Date:")) {
139136
Date date = new Date();
140137
try {
141-
date = df.parse(s.substring("date:".length()).trim());
138+
date = repository.parse(s.substring("date:".length()).trim());
142139
} catch (ParseException pe) {
143140
//
144141
// Overriding processStream() thus need to comply with the

opengrok-indexer/src/main/java/org/opengrok/indexer/history/RazorHistoryParser.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.io.File;
2828
import java.io.FileReader;
2929
import java.io.IOException;
30-
import java.text.DateFormat;
3130
import java.text.ParseException;
3231
import java.util.ArrayList;
3332
import java.util.Date;
@@ -85,7 +84,6 @@ private History parseFile(File file, Repository repos)
8584
}
8685

8786
protected History parseContents(BufferedReader contents) throws IOException {
88-
DateFormat df = repository.getDateFormat();
8987
String line;
9088

9189
ArrayList<HistoryEntry> entries = new ArrayList<HistoryEntry>();
@@ -166,7 +164,7 @@ protected History parseContents(BufferedReader contents) throws IOException {
166164
entry.setActive("Active".equals(state));
167165
Date date = null;
168166
try {
169-
date = df.parse(dateTime);
167+
date = repository.parse(dateTime);
170168
} catch (ParseException pe) {
171169
//
172170
// Overriding processStream() thus need to comply with the

opengrok-indexer/src/main/java/org/opengrok/indexer/history/Repository.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public abstract class Repository extends RepositoryInfo {
5252

5353
private static final Logger LOGGER = LoggerFactory.getLogger(Repository.class);
5454

55+
/**
56+
* format used for printing the date in {@code currentVersion}.
57+
* <p>
58+
* NOTE: SimpleDateFormat is not thread-safe, lock must be held when formatting
59+
*/
60+
protected static final SimpleDateFormat OUTPUT_DATE_FORMAT = new SimpleDateFormat("YYYY-MM-dd HH:mm Z");
61+
5562
/**
5663
* The command with which to access the external repository. Can be
5764
* {@code null} if the repository isn't accessed via a CLI, or if it hasn't
@@ -455,10 +462,37 @@ boolean supportsSubRepositories() {
455462
return false;
456463
}
457464

458-
public DateFormat getDateFormat() {
465+
private DateFormat getDateFormat() {
459466
return new RepositoryDateFormat();
460467
}
461468

469+
/**
470+
* Format the given date according to the output format.
471+
*
472+
* @param date the date
473+
* @return the string representing the formatted date
474+
* @see #OUTPUT_DATE_FORMAT
475+
*/
476+
public String format(Date date) {
477+
synchronized (OUTPUT_DATE_FORMAT) {
478+
return OUTPUT_DATE_FORMAT.format(date);
479+
}
480+
}
481+
482+
/**
483+
* Parse the given string as a date object with the repository date formats.
484+
*
485+
* @param dateString the string representing the date
486+
* @return the instance of a date
487+
* @throws ParseException when the string can not be parsed correctly
488+
*/
489+
public Date parse(String dateString) throws ParseException {
490+
final DateFormat format = getDateFormat();
491+
synchronized (format) {
492+
return format.parse(dateString);
493+
}
494+
}
495+
462496
static Boolean checkCmd(String... args) {
463497
Executor exec = new Executor(args);
464498
return exec.exec(false) == 0;
@@ -515,6 +549,7 @@ protected String getRepoRelativePath(final File file)
515549

516550
private class RepositoryDateFormat extends DateFormat {
517551
private final Locale locale = Locale.ENGLISH;
552+
// NOTE: SimpleDateFormat is not thread-safe, lock must be held when used
518553
private final SimpleDateFormat[] formatters = new SimpleDateFormat[datePatterns.length];
519554

520555
{

0 commit comments

Comments
 (0)