You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by ba...@apache.org on 2007/10/16 07:40:17 UTC

svn commit: r585044 - in /jakarta/taglibs/proper/standard/trunk: src/org/apache/taglibs/standard/tag/common/fmt/ test/org/apache/taglibs/standard/tag/el/fmt/ test/web/org/apache/taglibs/standard/tag/el/fmt/

Author: bayard
Date: Mon Oct 15 22:40:16 2007
New Revision: 585044

URL: http://svn.apache.org/viewvc?rev=585044&view=rev
Log:
Applying the fix and the tests from #32311 - LRUMap used as a caching system to improve on performance

Added:
    jakarta/taglibs/proper/standard/trunk/test/org/apache/taglibs/standard/tag/el/fmt/
    jakarta/taglibs/proper/standard/trunk/test/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.java   (with props)
    jakarta/taglibs/proper/standard/trunk/test/web/org/apache/taglibs/standard/tag/el/fmt/
    jakarta/taglibs/proper/standard/trunk/test/web/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.jsp   (with props)
Modified:
    jakarta/taglibs/proper/standard/trunk/src/org/apache/taglibs/standard/tag/common/fmt/FormatDateSupport.java

Modified: jakarta/taglibs/proper/standard/trunk/src/org/apache/taglibs/standard/tag/common/fmt/FormatDateSupport.java
URL: http://svn.apache.org/viewvc/jakarta/taglibs/proper/standard/trunk/src/org/apache/taglibs/standard/tag/common/fmt/FormatDateSupport.java?rev=585044&r1=585043&r2=585044&view=diff
==============================================================================
--- jakarta/taglibs/proper/standard/trunk/src/org/apache/taglibs/standard/tag/common/fmt/FormatDateSupport.java (original)
+++ jakarta/taglibs/proper/standard/trunk/src/org/apache/taglibs/standard/tag/common/fmt/FormatDateSupport.java Mon Oct 15 22:40:16 2007
@@ -22,6 +22,8 @@
 import java.util.Date;
 import java.util.Locale;
 import java.util.TimeZone;
+import java.util.Collections;
+import java.util.Map;
 
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.JspTagException;
@@ -31,6 +33,8 @@
 import org.apache.taglibs.standard.resources.Resources;
 import org.apache.taglibs.standard.tag.common.core.Util;
 
+import org.apache.taglibs.standard.extra.commons.collections.map.LRUMap;
+
 /**
  * Support for tag handlers for <formatDate>, the date and time
  * formatting tag in JSTL 1.0.
@@ -43,10 +47,24 @@
     //*********************************************************************
     // Private constants
 
+	/**
+	 * Name of configuration setting for maximum number of entries in the
+	 * cached dateformat map
+	 */
+	private static final String DATE_CACHE_PARAM = 
+		"org.apache.taglibs.standard.tag.common.fmt.dateFormatCacheSize";
+
     private static final String DATE = "date";
     private static final String TIME = "time";
     private static final String DATETIME = "both";
 
+    private static Map dateFormatCache = null;
+
+	/**
+	 * Default maximum  cache size
+	 */
+	private static final int MAX_SIZE = 100;
+
 
     //*********************************************************************
     // Protected state
@@ -120,16 +138,7 @@
 	    DateFormat.getAvailableLocales());
 
 	if (locale != null) {
-	    DateFormat formatter = createFormatter(locale);
-
-	    // Apply pattern, if present
-	    if (pattern != null) {
-		try {
-		    ((SimpleDateFormat) formatter).applyPattern(pattern);
-		} catch (ClassCastException cce) {
-		    formatter = new SimpleDateFormat(pattern, locale);
-		}
-	    }
+	    DateFormat formatter = createFormatter(locale, pattern);
 
 	    // Set time zone
 	    TimeZone tz = null;
@@ -180,22 +189,75 @@
     //*********************************************************************
     // Private utility methods
 
-    private DateFormat createFormatter(Locale loc) throws JspException {
+    private DateFormat createFormatter(Locale loc, String pattern) throws JspException {
 	DateFormat formatter = null;
 
+	// lazy initialization of cache
+	if (dateFormatCache == null) {
+		String value = pageContext.getServletContext().getInitParameter(DATE_CACHE_PARAM);
+		if (value != null) {
+			dateFormatCache = Collections.synchronizedMap(new LRUMap(Integer.parseInt(value)));
+		} else {
+			dateFormatCache = Collections.synchronizedMap(new LRUMap(MAX_SIZE));
+		}
+	}
+
+	// Apply pattern, if present
+	if (pattern != null) {
+		if ((type == null) || DATE.equalsIgnoreCase(type)) {
+		    String key = DATE + pattern + loc;
+            formatter = (DateFormat) dateFormatCache.get(key);
+            if(formatter == null) {
+                formatter = new SimpleDateFormat(pattern, loc);
+                dateFormatCache.put(key, formatter);
+            }
+		} else if (TIME.equalsIgnoreCase(type)) {
+		    String key = TIME + pattern + loc;
+            formatter = (DateFormat) dateFormatCache.get(key);
+            if(formatter == null) {
+                formatter = new SimpleDateFormat(pattern, loc);
+                dateFormatCache.put(key, formatter);
+            }
+		} else if (DATETIME.equalsIgnoreCase(type)) {
+		    String key = DATETIME + pattern + loc;
+            formatter = (DateFormat) dateFormatCache.get(key);
+            if(formatter == null) {
+                formatter = new SimpleDateFormat(pattern, loc);
+                dateFormatCache.put(key, formatter);
+            }
+		} else {
+		    throw new JspException(
+  	                  Resources.getMessage("FORMAT_DATE_INVALID_TYPE", 
+						 type));
+		}
+        return formatter;
+	}
+
 	if ((type == null) || DATE.equalsIgnoreCase(type)) {
-	    formatter = DateFormat.getDateInstance(
-	        Util.getStyle(dateStyle, "FORMAT_DATE_INVALID_DATE_STYLE"),
-		loc);
+		int style = Util.getStyle(dateStyle, "FORMAT_DATE_INVALID_DATE_STYLE");
+		String key = DATE + style + loc;
+		formatter = (DateFormat) dateFormatCache.get(key);
+		if(formatter == null) {
+			formatter = DateFormat.getDateInstance(style, loc);
+			dateFormatCache.put(key, formatter);
+		}
 	} else if (TIME.equalsIgnoreCase(type)) {
-	    formatter = DateFormat.getTimeInstance(
-	        Util.getStyle(timeStyle, "FORMAT_DATE_INVALID_TIME_STYLE"),
-		loc);
+		int style = Util.getStyle(timeStyle, "FORMAT_DATE_INVALID_TIME_STYLE");
+		String key = TIME + style + loc;
+		formatter = (DateFormat) dateFormatCache.get(key);
+		if(formatter == null) {
+			formatter = DateFormat.getTimeInstance(style, loc);
+			dateFormatCache.put(key, formatter);
+		}
 	} else if (DATETIME.equalsIgnoreCase(type)) {
-	    formatter = DateFormat.getDateTimeInstance(
-	        Util.getStyle(dateStyle, "FORMAT_DATE_INVALID_DATE_STYLE"),
-		Util.getStyle(timeStyle, "FORMAT_DATE_INVALID_TIME_STYLE"),
-		loc);
+		int style1 = Util.getStyle(dateStyle, "FORMAT_DATE_INVALID_DATE_STYLE");
+		int style2 = Util.getStyle(timeStyle, "FORMAT_DATE_INVALID_TIME_STYLE");
+		String key = DATETIME + style1 + loc + style2;
+		formatter = (DateFormat) dateFormatCache.get(key);
+		if(formatter == null) {
+			formatter = DateFormat.getDateTimeInstance(style1, style2, loc);
+			dateFormatCache.put(key, formatter);
+		}
 	} else {
 	    throw new JspException(
                     Resources.getMessage("FORMAT_DATE_INVALID_TYPE", 

Added: jakarta/taglibs/proper/standard/trunk/test/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.java
URL: http://svn.apache.org/viewvc/jakarta/taglibs/proper/standard/trunk/test/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.java?rev=585044&view=auto
==============================================================================
--- jakarta/taglibs/proper/standard/trunk/test/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.java (added)
+++ jakarta/taglibs/proper/standard/trunk/test/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.java Mon Oct 15 22:40:16 2007
@@ -0,0 +1,49 @@
+/*
+ * Copyright 1999,2004 The Apache Software Foundation.
+ *
+ * Licensed 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.taglibs.standard.tag.el.fmt;
+
+import javax.servlet.jsp.*;
+import org.apache.cactus.*;
+import org.apache.taglibs.standard.testutil.TestUtil;
+
+public class TestDateTag extends JspTestCase {
+
+    public TestDateTag(String name) {
+        super(name);
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testDateTag() throws Exception {
+        String var = "varDate";
+        String toInclude = TestUtil.getTestJsp(this);
+        pageContext.include(toInclude);
+        String actual = (String) pageContext.getAttribute(var, PageContext.APPLICATION_SCOPE);
+
+        // This isn't desired; Cactus doesn't set things up right
+        assertEquals(new java.util.Date().toString().length(), actual.length());
+        
+        // Ideally we would run this, or something like it
+//        assertEquals("yyyy-MM-dd".length(), actual.length());
+    }
+}

Propchange: jakarta/taglibs/proper/standard/trunk/test/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jakarta/taglibs/proper/standard/trunk/test/web/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.jsp
URL: http://svn.apache.org/viewvc/jakarta/taglibs/proper/standard/trunk/test/web/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.jsp?rev=585044&view=auto
==============================================================================
--- jakarta/taglibs/proper/standard/trunk/test/web/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.jsp (added)
+++ jakarta/taglibs/proper/standard/trunk/test/web/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.jsp Mon Oct 15 22:40:16 2007
@@ -0,0 +1,18 @@
+<!-- Use this via the web browser to show that things are fine             -->
+<!-- Unfortunately with Cactus the server isn't getting the correct locale -->
+<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
+
+<jsp:useBean id="date" class="java.util.Date" />
+
+<fmt:formatDate value="${date}" var="varDate" scope="application" pattern="yyyy-MM-dd"/>
+<c:out value="${varDate}"/>
+
+<fmt:formatDate value="${date}" var="varTime" scope="application" pattern="HH:mm:ss" type="time"/>
+<c:out value="${varTime}"/>
+
+<fmt:formatDate value="${date}" dateStyle="short" var="varDate2" scope="application"/>
+<c:out value="${varDate2}"/>
+
+<fmt:formatDate value="${date}" timeStyle="short" var="varTime2" scope="application" type="time"/>
+<c:out value="${varTime2}"/>

Propchange: jakarta/taglibs/proper/standard/trunk/test/web/org/apache/taglibs/standard/tag/el/fmt/TestDateTag.jsp
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org