You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2013/05/16 13:22:10 UTC
[3/3] git commit: Protected the DateFormat against concurrent usage
by storing the formatter in a ThreadLocal storage
Protected the DateFormat against concurrent usage by storing the
formatter in a ThreadLocal storage
Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/9e8b4773
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/9e8b4773
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/9e8b4773
Branch: refs/heads/trunk
Commit: 9e8b477397dee280ba78eb58d19c399fdd66044f
Parents: bf20617
Author: Emmanuel Lécharny <el...@apache.org>
Authored: Thu May 16 13:15:06 2013 +0200
Committer: Emmanuel Lécharny <el...@apache.org>
Committed: Thu May 16 13:15:06 2013 +0200
----------------------------------------------------------------------
.../main/java/org/apache/mina/http/DateUtil.java | 55 ++++++++++-----
1 files changed, 38 insertions(+), 17 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina/blob/9e8b4773/http/src/main/java/org/apache/mina/http/DateUtil.java
----------------------------------------------------------------------
diff --git a/http/src/main/java/org/apache/mina/http/DateUtil.java b/http/src/main/java/org/apache/mina/http/DateUtil.java
index 1fd24c6..e64eaff 100644
--- a/http/src/main/java/org/apache/mina/http/DateUtil.java
+++ b/http/src/main/java/org/apache/mina/http/DateUtil.java
@@ -33,31 +33,54 @@ import java.util.regex.Pattern;
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class DateUtil {
+ /** A thread local is used to protect the DateFormat against concurrent access */
+ private static ThreadLocal<DateFormat> RFC_1123_FORMAT = new ThreadLocal<DateFormat>() {
+ /**
+ * Gets the formatter
+ */
+ public DateFormat get() {
+ return super.get();
+ }
- private final static Locale LOCALE = Locale.US;
+ /**
+ * Get rid of the formatter
+ */
+ public void remove() {
+ super.remove();
+ }
- private final static TimeZone GMT_ZONE;
+ /**
+ * Set a new formatter
+ */
+ public void set(DateFormat value) {
+ super.set(value);
+ }
- private final static String RFC_1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
+ /**
+ * Initialize the formatter
+ */
+ protected DateFormat initialValue() {
+ DateFormat dateFormat = new SimpleDateFormat(RFC_1123_PATTERN, Locale.US);
+ TimeZone gmtZone = TimeZone.getTimeZone("GMT");
+ dateFormat.setTimeZone(gmtZone);
- private final static DateFormat RFC_1123_FORMAT;
+ return dateFormat;
+ }
+ };
+
+ /** The format of an HTTP date */
+ private final static String RFC_1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
/** Pattern to find digits only. */
private final static Pattern DIGIT_PATTERN = Pattern.compile("^\\d+$");
- static {
- RFC_1123_FORMAT = new SimpleDateFormat(DateUtil.RFC_1123_PATTERN, DateUtil.LOCALE);
- GMT_ZONE = TimeZone.getTimeZone("GMT");
- DateUtil.RFC_1123_FORMAT.setTimeZone(DateUtil.GMT_ZONE);
- }
-
/**
* Returns the current date as String
*
* @return Current Date as String, in the format <i>EEE, dd MMM yyyy HH:mm:ss zzz</i>
*/
public static String getCurrentAsString() {
- return DateUtil.RFC_1123_FORMAT.format(new Date());
+ return RFC_1123_FORMAT.get().format(new Date());
}
/**
@@ -72,7 +95,7 @@ public class DateUtil {
private static long parseDateStringToMilliseconds(final String dateString) {
try {
- return DateUtil.RFC_1123_FORMAT.parse(dateString).getTime();
+ return RFC_1123_FORMAT.get().parse(dateString).getTime();
} catch (final ParseException e) {
return 0;
}
@@ -92,7 +115,7 @@ public class DateUtil {
long ms = 0;
- if (DateUtil.DIGIT_PATTERN.matcher(dateValue).matches()) {
+ if (DIGIT_PATTERN.matcher(dateValue).matches()) {
ms = Long.parseLong(dateValue);
} else {
ms = parseDateStringToMilliseconds(dateValue);
@@ -109,11 +132,10 @@ public class DateUtil {
* @return a <code>String</code> representation of the date.
*/
public static String parseToRFC1123(final long dateValue) {
-
final Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(dateValue);
- return DateUtil.RFC_1123_FORMAT.format(calendar.getTime());
+ return RFC_1123_FORMAT.get().format(calendar.getTime());
}
/**
@@ -124,7 +146,6 @@ public class DateUtil {
* @return a <code>String</code> representation of the date.
*/
public static String getDateAsString(Date date) {
- return RFC_1123_FORMAT.format(date);
+ return RFC_1123_FORMAT.get().format(date);
}
-
}