Skip to content

Commit

Permalink
Ensure SimpleDateFormat not accessed by multiple threads
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Jan 12, 2017
1 parent b37bb86 commit f8bc7bc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/jenkins/plugins/openstack/compute/ServerScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ protected boolean _equals(ServerScope o) {
}

public static final class Time extends ServerScope {
private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// Track the time using master clock so we do not have to sync with openstack time difference
private final long aliveUntil;
Expand All @@ -194,14 +193,19 @@ public Time(int duration, TimeUnit tu) {
}

private Time(long millis) {
super("time", FORMAT.format(new Date(millis)));
super("time", format().format(new Date(millis)));
aliveUntil = millis;
}

// The instance is not type safe so create a new one every time
private static SimpleDateFormat format() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}

public Time(String specifier) {
super("time", specifier);
try {
aliveUntil = FORMAT.parse(specifier).getTime();
aliveUntil = format().parse(specifier).getTime();
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
Expand All @@ -214,7 +218,7 @@ public boolean isOutOfScope() {

@Override
public String getValue() {
return name + ":" + FORMAT.format(new Date(aliveUntil));
return name + ":" + format().format(new Date(aliveUntil));
}

@Override
Expand Down

0 comments on commit f8bc7bc

Please sign in to comment.