Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit

Permalink
Fix Find Bugs warnings - thread safety of data formatting
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1361796 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Jul 15, 2012
1 parent ecdf8e9 commit e568423
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 31 deletions.
36 changes: 5 additions & 31 deletions java/org/apache/catalina/servlets/WebdavServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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;
Expand All @@ -49,6 +48,7 @@
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;
Expand Down Expand Up @@ -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"));


/**
Expand All @@ -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>
Expand Down Expand Up @@ -2606,27 +2600,7 @@ private void parseLockNullProperties(HttpServletRequest req,
* 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));
}

/**
Expand Down
62 changes: 62 additions & 0 deletions java/org/apache/catalina/util/ConcurrentDateFormat.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit e568423

Please sign in to comment.