jacks recommended change for thread safety

This commit is contained in:
Jeremy Long
2016-12-05 22:41:15 -05:00
parent bd3af45db9
commit f47c6b07f4
5 changed files with 58 additions and 18 deletions

View File

@@ -261,6 +261,10 @@ Copyright (c) 2012 Jeremy Long. All Rights Reserved.
</reporting> </reporting>
<dependencies> <dependencies>
<!-- Note, to stay compatible with Jenkins installations only JARs compiled to 1.6 can be used --> <!-- Note, to stay compatible with Jenkins installations only JARs compiled to 1.6 can be used -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.google.code.findbugs</groupId> <groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>

View File

@@ -17,13 +17,13 @@
*/ */
package org.owasp.dependencycheck.data.nvdcve; package org.owasp.dependencycheck.data.nvdcve;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Properties; import java.util.Properties;
import java.util.TreeMap; import java.util.TreeMap;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.owasp.dependencycheck.data.update.nvd.NvdCveInfo; import org.owasp.dependencycheck.data.update.nvd.NvdCveInfo;
import org.owasp.dependencycheck.data.update.exception.UpdateException; import org.owasp.dependencycheck.data.update.exception.UpdateException;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -170,12 +170,13 @@ public class DatabaseProperties {
if (key.startsWith("NVD CVE ")) { if (key.startsWith("NVD CVE ")) {
try { try {
final long epoch = Long.parseLong((String) entry.getValue()); final long epoch = Long.parseLong((String) entry.getValue());
final Date date = new Date(epoch); final DateTime date = new DateTime(epoch);
synchronized (date) { DateTimeFormatter format = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
final DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); String formatted = format.print(date);
final String formatted = format.format(date); // final Date date = new Date(epoch);
map.put(key, formatted); // final DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
} // final String formatted = format.format(date);
map.put(key, formatted);
} catch (Throwable ex) { //deliberately being broad in this catch clause } catch (Throwable ex) { //deliberately being broad in this catch clause
LOGGER.debug("Unable to parse timestamp from DB", ex); LOGGER.debug("Unable to parse timestamp from DB", ex);
map.put(key, (String) entry.getValue()); map.put(key, (String) entry.getValue());

View File

@@ -35,6 +35,9 @@ import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context; import org.apache.velocity.context.Context;
import org.apache.velocity.runtime.RuntimeConstants; import org.apache.velocity.runtime.RuntimeConstants;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.owasp.dependencycheck.analyzer.Analyzer; import org.owasp.dependencycheck.analyzer.Analyzer;
import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties; import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
import org.owasp.dependencycheck.dependency.Dependency; import org.owasp.dependencycheck.dependency.Dependency;
@@ -104,15 +107,18 @@ public class ReportGenerator {
velocityEngine.init(); velocityEngine.init();
final EscapeTool enc = new EscapeTool(); final EscapeTool enc = new EscapeTool();
final Date d = new Date();
String scanDate; final DateTime dt = DateTime.now();
String scanDateXML; DateTimeFormatter dateFormat = DateTimeFormat.forPattern("MMM d, yyyy 'at' HH:mm:ss z");
synchronized (d) { DateTimeFormatter dateFormatXML = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy 'at' HH:mm:ss z");
final DateFormat dateFormatXML = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); // final Date d = new Date();
scanDate = dateFormat.format(d); // final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy 'at' HH:mm:ss z");
scanDateXML = dateFormatXML.format(d); // final DateFormat dateFormatXML = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
}
final String scanDate = dateFormat.print(dt);
final String scanDateXML = dateFormatXML.print(dt);
context.put("applicationName", applicationName); context.put("applicationName", applicationName);
context.put("dependencies", dependencies); context.put("dependencies", dependencies);
context.put("analyzers", analyzers); context.put("analyzers", analyzers);

View File

@@ -17,8 +17,14 @@
*/ */
package org.owasp.dependencycheck.data.nvdcve; package org.owasp.dependencycheck.data.nvdcve;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.owasp.dependencycheck.BaseDBTestCase; import org.owasp.dependencycheck.BaseDBTestCase;
import java.util.Properties; import java.util.Properties;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
@@ -143,4 +149,22 @@ public class DatabasePropertiesIntegrationTest extends BaseDBTestCase {
} }
} }
} }
@Test
public void testTest() {
final Date now = new Date();
final DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
final String formatted = format.format(now);
final DateTime dt = new DateTime(now.getTime());
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String jodaFormatted = fmt.print(dt);
System.out.println(formatted);
System.out.println(jodaFormatted);
assertTrue(jodaFormatted.equals(formatted));
}
} }

View File

@@ -543,6 +543,11 @@ Copyright (c) 2012 - Jeremy Long
</reporting> </reporting>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.6</version>
</dependency>
<dependency> <dependency>
<groupId>com.google.code.findbugs</groupId> <groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>