roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agillil...@apache.org
Subject svn commit: r446711 - in /incubator/roller/trunk/src/org/apache/roller: business/hibernate/ ui/authoring/struts/formbeans/ util/
Date Fri, 15 Sep 2006 20:02:33 GMT
Author: agilliland
Date: Fri Sep 15 13:02:33 2006
New Revision: 446711

URL: http://svn.apache.org/viewvc?view=rev&rev=446711
Log:
a bunch of work to fix bad MT use of SimpleDateFormat class throughout Roller as described
in JIRA issue ROL-1221

http://opensource.atlassian.com/projects/roller/browse/ROL-1221

- moved some shared instances of SimpleDateFormat from global scope to method scope in places
where that seemed easiest.

- completely refactored DateUtil to only maintain shared references of the string representation
of a format, not the actual instance of the format.  methods for access a format new return
a new instance of SimpleDateFormat.

- added a synchronization block to the DateUtil.parse() method to ensure synchronization happens
on format instances passed in for use.



Modified:
    incubator/roller/trunk/src/org/apache/roller/business/hibernate/HibernateRefererManagerImpl.java
    incubator/roller/trunk/src/org/apache/roller/business/hibernate/HibernateWeblogManagerImpl.java
    incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/UserFormEx.java
    incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/WeblogEntryFormEx.java
    incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/WebsiteFormEx.java
    incubator/roller/trunk/src/org/apache/roller/util/DateUtil.java
    incubator/roller/trunk/src/org/apache/roller/util/ISO8601DateParser.java

Modified: incubator/roller/trunk/src/org/apache/roller/business/hibernate/HibernateRefererManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/business/hibernate/HibernateRefererManagerImpl.java?view=diff&rev=446711&r1=446710&r2=446711
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/business/hibernate/HibernateRefererManagerImpl.java
(original)
+++ incubator/roller/trunk/src/org/apache/roller/business/hibernate/HibernateRefererManagerImpl.java
Fri Sep 15 13:02:33 2006
@@ -1,26 +1,23 @@
 /*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-*  contributor license agreements.  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.  For additional information regarding
-* copyright in this work, please see the NOTICE file in the top level
-* directory of this distribution.
-*/
-/*
- * Created on Feb 23, 2003
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
  */
+
 package org.apache.roller.business.hibernate;
 
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
@@ -73,7 +70,7 @@
     
     private HibernatePersistenceStrategy strategy = null;
     private Date mRefDate = new Date();
-    private SimpleDateFormat mDateFormat = DateUtil.get8charDateFormat();
+    
         
     public HibernateRefererManagerImpl(HibernatePersistenceStrategy strat) {
         

Modified: incubator/roller/trunk/src/org/apache/roller/business/hibernate/HibernateWeblogManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/business/hibernate/HibernateWeblogManagerImpl.java?view=diff&rev=446711&r1=446710&r2=446711
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/business/hibernate/HibernateWeblogManagerImpl.java
(original)
+++ incubator/roller/trunk/src/org/apache/roller/business/hibernate/HibernateWeblogManagerImpl.java
Fri Sep 15 13:02:33 2006
@@ -76,8 +76,6 @@
     /* inline creation of reverse comparator, anonymous inner class */
     private Comparator reverseComparator = new ReverseComparator();
     
-    private SimpleDateFormat formatter = DateUtil.get8charDateFormat();
-    
     
     public HibernateWeblogManagerImpl(HibernatePersistenceStrategy strat) {
         log.debug("Instantiating Hibernate Weblog Manager");
@@ -1023,6 +1021,7 @@
             cal.setTimeZone(website.getTimeZoneInstance());
         }
         
+        SimpleDateFormat formatter = DateUtil.get8charDateFormat();
         for (Iterator wbItr = entries.iterator(); wbItr.hasNext();) {
             WeblogEntryData entry = (WeblogEntryData) wbItr.next();
             Date sDate = DateUtil.getNoonOfDay(entry.getPubTime(), cal);

Modified: incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/UserFormEx.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/UserFormEx.java?view=diff&rev=446711&r1=446710&r2=446711
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/UserFormEx.java
(original)
+++ incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/UserFormEx.java
Fri Sep 15 13:02:33 2006
@@ -101,9 +101,8 @@
             }
             catch (java.text.ParseException pe)
             {
-                // wasn't proper format, try others
-                Date pubDate = DateUtil.parseFromFormats(value);
-                this.setDateCreated( new Timestamp(pubDate.getTime()) );
+                // wasn't properly formatted
+                throw new RuntimeException("improperly formatted date", pe);
             }
         }
     }

Modified: incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/WeblogEntryFormEx.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/WeblogEntryFormEx.java?view=diff&rev=446711&r1=446710&r2=446711
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/WeblogEntryFormEx.java
(original)
+++ incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/WeblogEntryFormEx.java
Fri Sep 15 13:02:33 2006
@@ -19,7 +19,6 @@
 
 import java.sql.Timestamp;
 import java.text.DateFormat;
-import java.text.SimpleDateFormat;
 import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Calendar;

Modified: incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/WebsiteFormEx.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/WebsiteFormEx.java?view=diff&rev=446711&r1=446710&r2=446711
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/WebsiteFormEx.java
(original)
+++ incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/formbeans/WebsiteFormEx.java
Fri Sep 15 13:02:33 2006
@@ -118,9 +118,8 @@
                         value, DateUtil.friendlyTimestampFormat());
                 this.setDateCreated(new Timestamp(pubDate.getTime()));
             } catch (java.text.ParseException pe) {
-                // wasn't proper format, try others
-                Date pubDate = DateUtil.parseFromFormats(value);
-                this.setDateCreated( new Timestamp(pubDate.getTime()) );
+                // wasn't properly formatted
+                throw new RuntimeException("improperly formatted date", pe);
             }
         }
     }

Modified: incubator/roller/trunk/src/org/apache/roller/util/DateUtil.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/util/DateUtil.java?view=diff&rev=446711&r1=446710&r2=446711
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/util/DateUtil.java (original)
+++ incubator/roller/trunk/src/org/apache/roller/util/DateUtil.java Fri Sep 15 13:02:33 2006
@@ -31,65 +31,24 @@
 /**
  * General purpose date utilities.
  */
-public abstract class DateUtil extends Object {
+public abstract class DateUtil {
     
     public static final long millisInDay = 86400000;
     
-    // some static date formats
-    private static SimpleDateFormat[] mDateFormats = loadDateFormats();
+    // a bunch of date formats
+    private static final String formatDefaultDate = "dd.MM.yyyy";
+    private static final String formatDefaultDateMinimal = "d.M.yy";
+    private static final String formatDefaultTimestamp = "yyyy-MM-dd HH:mm:ss.SSS";
     
-    private static final SimpleDateFormat mFormat8chars =
-            new SimpleDateFormat("yyyyMMdd");
+    private static final String formatFriendlyTimestamp = "dd.MM.yyyy HH:mm:ss";
     
-    private static final SimpleDateFormat mFormat6chars =
-            new SimpleDateFormat("yyyyMM");
+    private static final String format6chars = "yyyyMM";
+    private static final String format8chars = "yyyyMMdd";
     
-    private static final SimpleDateFormat mFormatIso8601Day =
-            new SimpleDateFormat("yyyy-MM-dd");
-    
-    private static final SimpleDateFormat mFormatIso8601 =
-            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
-    
-    // http://www.w3.org/Protocols/rfc822/Overview.html#z28
-    // Using Locale.US to fix ROL-725 and ROL-628
-    private static final SimpleDateFormat mFormatRfc822 =
-            new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
-    
-    
-    private static SimpleDateFormat[] loadDateFormats() {
-        SimpleDateFormat[] temp = {
-            //new SimpleDateFormat("MM/dd/yyyy hh:mm:ss.SSS a"),
-            new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy"), // standard Date.toString()
results
-            new SimpleDateFormat("M/d/yy hh:mm:ss"),
-            new SimpleDateFormat("M/d/yyyy hh:mm:ss"),
-            new SimpleDateFormat("M/d/yy hh:mm a"),
-            new SimpleDateFormat("M/d/yyyy hh:mm a"),
-            new SimpleDateFormat("M/d/yy HH:mm"),
-            new SimpleDateFormat("M/d/yyyy HH:mm"),
-            new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"),
-            new SimpleDateFormat("yy-MM-dd HH:mm:ss.SSS"),
-            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"), // standard Timestamp.toString()
results
-            new SimpleDateFormat("M-d-yy HH:mm"),
-            new SimpleDateFormat("M-d-yyyy HH:mm"),
-            new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS"),
-            new SimpleDateFormat("M/d/yy"),
-            new SimpleDateFormat("M/d/yyyy"),
-            new SimpleDateFormat("M-d-yy"),
-            new SimpleDateFormat("M-d-yyyy"),
-            new SimpleDateFormat("MMMM d, yyyyy"),
-            new SimpleDateFormat("MMM d, yyyyy")
-        };
-        
-        return temp;
-    }
+    private static final String formatIso8601 = "yyyy-MM-dd'T'HH:mm:ssZ";
+    private static final String formatIso8601Day = "yyyy-MM-dd";
     
-    
-    /**
-     * Gets the array of SimpleDateFormats that DateUtil knows about.
-     **/
-    private static SimpleDateFormat[] getFormats() {
-        return mDateFormats;
-    }
+    private static final String formatRfc822 = "EEE, d MMM yyyy HH:mm:ss Z";
     
     
     /**
@@ -215,40 +174,6 @@
     }
     
     
-    public static Date parseFromFormats(String aValue) {
-        if (StringUtils.isEmpty(aValue)) return null;
-        
-        // get DateUtil's formats
-        SimpleDateFormat formats[] = DateUtil.getFormats();
-        if (formats == null) return null;
-        
-        // iterate over the array and parse
-        Date myDate = null;
-        for (int i = 0; i <formats.length; i++) {
-            try {
-                myDate = DateUtil.parse(aValue, formats[i]);
-                //if (myDate instanceof Date)
-                return myDate;
-            } catch (Exception e) {
-                // do nothing because we want to try the next
-                // format if current one fails
-            }
-        }
-        // haven't returned so couldn't parse
-        return null;
-    }
-    
-    
-    public static java.sql.Timestamp parseTimestampFromFormats(String aValue) {
-        if (StringUtils.isEmpty(aValue)) return null;
-        
-        // call the regular Date formatter
-        Date myDate = DateUtil.parseFromFormats(aValue);
-        if (myDate != null) return new java.sql.Timestamp(myDate.getTime());
-        return null;
-    }
-    
-    
     /**
      * Returns a java.sql.Timestamp equal to the current time
      **/
@@ -271,24 +196,6 @@
     
     
     /**
-     * Tries to take the passed-in String and format it as a date string in the
-     * the passed-in format.
-     **/
-    public static String formatDateString(String aString, SimpleDateFormat aFormat) {
-        if (StringUtils.isEmpty(aString) || aFormat == null)  return "";
-        try {
-            java.sql.Timestamp aDate = parseTimestampFromFormats(aString);
-            if (aDate != null) {
-                return DateUtil.format(aDate, aFormat);
-            }
-        } catch (Exception e) {
-            // Could not parse aString.
-        }
-        return "";
-    }
-    
-    
-    /**
      * Returns a Date using the passed-in string and format.  Returns null if the string
      * is null or empty or if the format is null.  The string must match the format.
      **/
@@ -296,8 +203,9 @@
         if (StringUtils.isEmpty(aValue) || aFormat == null) {
             return null;
         }
-        
-        return aFormat.parse(aValue);
+        synchronized(aFormat) {
+            return aFormat.parse(aValue);
+        }
     }
     
     
@@ -331,74 +239,145 @@
     }
     
     
+    // convenience method returns minimal date format
+    public static SimpleDateFormat defaultDateFormat() {
+        return DateUtil.friendlyDateFormat(true);
+    }
+    
+    
+    // convenience method returns minimal date format
+    public static java.text.SimpleDateFormat minimalDateFormat() {
+        return friendlyDateFormat(true);
+    }
+    
+    
+    // convenience method that returns friendly data format
+    // using full month, day, year digits.
+    public static SimpleDateFormat fullDateFormat() {
+        return friendlyDateFormat(false);
+    }
+    
+    
+    /** 
+     * Returns a "friendly" date format.
+     * @param mimimalFormat Should the date format allow single digits.
+     **/
+    public static SimpleDateFormat friendlyDateFormat(boolean minimalFormat) {
+        if (minimalFormat) {
+            return new SimpleDateFormat(formatDefaultDateMinimal);
+        }
+        
+        return new SimpleDateFormat(formatDefaultDate);
+    }
+    
+    
     // returns full timestamp format
-    public static java.text.SimpleDateFormat defaultTimestampFormat() {
-        return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+    public static SimpleDateFormat defaultTimestampFormat() {
+        return new SimpleDateFormat(formatDefaultTimestamp);
+    }
+    
+    
+    // convenience method returns long friendly timestamp format
+    public static SimpleDateFormat friendlyTimestampFormat() {
+        return new SimpleDateFormat(formatFriendlyTimestamp);
     }
     
     
     // convenience method returns minimal date format
-    public static java.text.SimpleDateFormat get8charDateFormat() {
-        return DateUtil.mFormat8chars;
+    public static SimpleDateFormat get8charDateFormat() {
+        return new SimpleDateFormat(format8chars);
     }
     
     
     // convenience method returns minimal date format
-    public static java.text.SimpleDateFormat get6charDateFormat() {
-        return DateUtil.mFormat6chars;
+    public static SimpleDateFormat get6charDateFormat() {
+        return new SimpleDateFormat(format6chars);
     }
     
     
     // convenience method returns minimal date format
-    public static java.text.SimpleDateFormat defaultDateFormat() {
-        return DateUtil.friendlyDateFormat(true);
+    public static SimpleDateFormat getIso8601DateFormat() {
+        return new SimpleDateFormat(formatIso8601);
     }
     
     
-    // convenience method
-    public static String defaultTimestamp(Date date) {
-        return DateUtil.format(date, DateUtil.defaultTimestampFormat());
+    // convenience method returns minimal date format
+    public static SimpleDateFormat getIso8601DayDateFormat() {
+        return new SimpleDateFormat(formatIso8601Day);
+    }
+    
+    
+    // convenience method returns minimal date format
+    public static SimpleDateFormat getRfc822DateFormat() {
+        // http://www.w3.org/Protocols/rfc822/Overview.html#z28
+        // Using Locale.US to fix ROL-725 and ROL-628
+        return new SimpleDateFormat(formatRfc822, Locale.US);
     }
     
     
     // convenience method
     public static String defaultDate(Date date) {
-        return DateUtil.format(date, DateUtil.defaultDateFormat());
+        return format(date, defaultDateFormat());
     }
     
     
-    // convenience method returns long friendly timestamp format
-    public static java.text.SimpleDateFormat friendlyTimestampFormat() {
-        return new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
+    // convenience method using minimal date format
+    public static String minimalDate(Date date) {
+        return format(date, DateUtil.minimalDateFormat());
+    }
+    
+    
+    public static String fullDate(Date date) {
+        return format(date, DateUtil.fullDateFormat());
+    }
+    
+    
+    /**
+     * Format the date using the "friendly" date format.
+     */
+    public static String friendlyDate(Date date, boolean minimalFormat) {
+        return format(date, friendlyDateFormat(minimalFormat));
+    }
+    
+    
+    // convenience method
+    public static String friendlyDate(Date date) {
+        return format(date, friendlyDateFormat(true));
+    }
+    
+    
+    // convenience method
+    public static String defaultTimestamp(Date date) {
+        return format(date, defaultTimestampFormat());
     }
     
     
     // convenience method returns long friendly formatted timestamp
     public static String friendlyTimestamp(Date date) {
-        return DateUtil.format(date, DateUtil.friendlyTimestampFormat());
+        return format(date, friendlyTimestampFormat());
     }
     
     
     // convenience method returns 8 char day stamp YYYYMMDD
     public static String format8chars(Date date) {
-        return DateUtil.format(date, mFormat8chars);
+        return format(date, get8charDateFormat());
     }
     
     
     // convenience method returns 6 char month stamp YYYYMM
     public static String format6chars(Date date) {
-        return DateUtil.format(date, mFormat6chars);
+        return format(date, get6charDateFormat());
     }
     
     
     // convenience method returns long friendly formatted timestamp
     public static String formatIso8601Day(Date date) {
-        return DateUtil.format(date, mFormatIso8601Day);
+        return format(date, getIso8601DayDateFormat());
     }
     
     
     public static String formatRfc822(Date date) {
-        return DateUtil.format(date,mFormatRfc822);
+        return format(date, getRfc822DateFormat());
     }
     
     
@@ -409,62 +388,12 @@
         // Add a colon 2 chars before the end of the string
         // to make it a valid ISO-8601 date.
         
-        String str = DateUtil.format(date,mFormatIso8601);
+        String str = format(date, getIso8601DateFormat());
         StringBuffer sb = new StringBuffer();
         sb.append( str.substring(0,str.length()-2) );
         sb.append( ":" );
         sb.append( str.substring(str.length()-2) );
         return sb.toString();
-    }
-    
-    
-    // convenience method returns minimal date format
-    public static java.text.SimpleDateFormat minimalDateFormat() {
-        return DateUtil.friendlyDateFormat(true);
-    }
-    
-    
-    // convenience method using minimal date format
-    public static String minimalDate(Date date) {
-        return DateUtil.format(date, DateUtil.minimalDateFormat());
-    }
-    
-    
-    // convenience method that returns friendly data format
-    // using full month, day, year digits.
-    public static java.text.SimpleDateFormat fullDateFormat() {
-        return DateUtil.friendlyDateFormat(false);
-    }
-    
-    
-    public static String fullDate(Date date) {
-        return DateUtil.format(date, DateUtil.fullDateFormat());
-    }
-    
-    
-    /** Returns a "friendly" date format.
-     *  @param mimimalFormat Should the date format allow single digits.
-     **/
-    public static java.text.SimpleDateFormat friendlyDateFormat(boolean minimalFormat) {
-        if (minimalFormat) {
-            return new java.text.SimpleDateFormat("d.M.yy");
-        }
-        
-        return new java.text.SimpleDateFormat("dd.MM.yyyy");
-    }
-    
-    
-    /**
-     * Format the date using the "friendly" date format.
-     */
-    public static String friendlyDate(Date date, boolean minimalFormat) {
-        return DateUtil.format(date, DateUtil.friendlyDateFormat(minimalFormat));
-    }
-    
-    
-    // convenience method
-    public static String friendlyDate(Date date) {
-        return DateUtil.format(date, DateUtil.friendlyDateFormat(true));
     }
     
     

Modified: incubator/roller/trunk/src/org/apache/roller/util/ISO8601DateParser.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/util/ISO8601DateParser.java?view=diff&rev=446711&r1=446710&r2=446711
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/util/ISO8601DateParser.java (original)
+++ incubator/roller/trunk/src/org/apache/roller/util/ISO8601DateParser.java Fri Sep 15 13:02:33
2006
@@ -31,9 +31,6 @@
     // 2004-06-14T19:GMT20:30Z
     // 2004-06-20T06:GMT22:01Z
 
-    private static SimpleDateFormat df
-        = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );
-
     // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
     //    
     // http://www.intertwingly.net/wiki/pie/DateTime
@@ -80,7 +77,8 @@
 
         //NOTE: SimpleDateFormat uses GMT[-+]hh:mm for the TZ which breaks
         //things a bit.  Before we go on we have to repair this.
-
+        SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );
+        
         //this is zero time so we need to add that TZ indicator for 
         if ( input.endsWith( "Z" ) ) {
             input = input.substring( 0, input.length() - 1) + "GMT-00:00";
@@ -98,7 +96,9 @@
     }
 
     public static String toString( Date date ) {
-
+        
+        SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );
+        
         TimeZone tz = TimeZone.getTimeZone( "UTC" );
         
         df.setTimeZone( tz );



Mime
View raw message