Author: markt
Date: Sun Jul 15 21:03:04 2012
New Revision: 1361796

URL: http://svn.apache.org/viewvc?rev=1361796&view=rev
Log:
Fix Find Bugs warnings - thread safety of data formatting

Added:
    tomcat/trunk/java/org/apache/catalina/util/ConcurrentDateFormat.java   
(with props)
Modified:
    tomcat/trunk/java/org/apache/catalina/servlets/WebdavServlet.java

Modified: tomcat/trunk/java/org/apache/catalina/servlets/WebdavServlet.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/servlets/WebdavServlet.java?rev=1361796&r1=1361795&r2=1361796&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/servlets/WebdavServlet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/servlets/WebdavServlet.java Sun Jul 
15 21:03:04 2012
@@ -26,7 +26,6 @@ import java.io.StringWriter;
 import java.io.Writer;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
-import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Enumeration;
 import java.util.Hashtable;
@@ -49,6 +48,7 @@ import javax.xml.parsers.DocumentBuilder
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
+import org.apache.catalina.util.ConcurrentDateFormat;
 import org.apache.catalina.util.DOMWriter;
 import org.apache.catalina.util.MD5Encoder;
 import org.apache.catalina.util.XMLWriter;
@@ -199,8 +199,9 @@ public class WebdavServlet
     /**
      * Simple date format for the creation date ISO representation (partial).
      */
-    protected static final SimpleDateFormat creationDateFormat =
-        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
+    protected static final ConcurrentDateFormat creationDateFormat =
+        new ConcurrentDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US,
+                TimeZone.getTimeZone("GMT"));
 
 
      /**
@@ -215,15 +216,8 @@ public class WebdavServlet
     protected static final MD5Encoder md5Encoder = new MD5Encoder();
 
 
-
-    static {
-        creationDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
-    }
-
-
     // ----------------------------------------------------- Instance Variables
 
-
     /**
      * Repository of the locks put on single resources.
      * <p>
@@ -2606,27 +2600,7 @@ public class WebdavServlet
      * Get creation date in ISO format.
      */
     private String getISOCreationDate(long creationDate) {
-        StringBuilder creationDateValue = new StringBuilder
-            (creationDateFormat.format
-             (new Date(creationDate)));
-        /*
-        int offset = Calendar.getInstance().getTimeZone().getRawOffset()
-            / 3600000; // FIXME ?
-        if (offset < 0) {
-            creationDateValue.append("-");
-            offset = -offset;
-        } else if (offset > 0) {
-            creationDateValue.append("+");
-        }
-        if (offset != 0) {
-            if (offset < 10)
-                creationDateValue.append("0");
-            creationDateValue.append(offset + ":00");
-        } else {
-            creationDateValue.append("Z");
-        }
-        */
-        return creationDateValue.toString();
+        return creationDateFormat.format(new Date(creationDate));
     }
 
     /**

Added: tomcat/trunk/java/org/apache/catalina/util/ConcurrentDateFormat.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/util/ConcurrentDateFormat.java?rev=1361796&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/util/ConcurrentDateFormat.java (added)
+++ tomcat/trunk/java/org/apache/catalina/util/ConcurrentDateFormat.java Sun 
Jul 15 21:03:04 2012
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.util;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.Queue;
+import java.util.TimeZone;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+/**
+ * A thread safe wrapper around {@link SimpleDateFormat} that does not make use
+ * of ThreadLocal and - broadly - only creates enough SimpleDateFormat objects
+ * to satisfy the concurrency requirements.
+ */
+public class ConcurrentDateFormat {
+
+    private final String format;
+    private final Locale locale;
+    private final TimeZone timezone;
+    private final Queue<SimpleDateFormat> queue = new 
ConcurrentLinkedQueue<>();
+
+    public ConcurrentDateFormat(String format, Locale locale,
+            TimeZone timezone) {
+        this.format = format;
+        this.locale = locale;
+        this.timezone = timezone;
+        SimpleDateFormat initial = createInstance();
+        queue.add(initial);
+    }
+
+    public String format(Date date) {
+        SimpleDateFormat sdf = queue.poll();
+        if (sdf == null) {
+            sdf = createInstance();
+        }
+        String result = sdf.format(date);
+        queue.add(sdf);
+        return result;
+    }
+
+    private SimpleDateFormat createInstance() {
+        SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
+        sdf.setTimeZone(timezone);
+        return sdf;
+    }
+}

Propchange: tomcat/trunk/java/org/apache/catalina/util/ConcurrentDateFormat.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to