changeset 855:fa6158f29c45

remove BufferDateCache and DateCache
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 20 Sep 2016 21:38:44 -0600
parents 359012f4e797
children ea1768c00d03
files src/org/eclipse/jetty/http/HttpFields.java src/org/eclipse/jetty/io/BufferDateCache.java src/org/eclipse/jetty/server/NCSARequestLog.java src/org/eclipse/jetty/server/Request.java src/org/eclipse/jetty/util/DateCache.java
diffstat 5 files changed, 35 insertions(+), 419 deletions(-) [+]
line wrap: on
line diff
diff -r 359012f4e797 -r fa6158f29c45 src/org/eclipse/jetty/http/HttpFields.java
--- a/src/org/eclipse/jetty/http/HttpFields.java	Tue Sep 20 01:05:37 2016 -0600
+++ b/src/org/eclipse/jetty/http/HttpFields.java	Tue Sep 20 21:38:44 2016 -0600
@@ -42,7 +42,6 @@
 import org.eclipse.jetty.io.Buffer;
 import org.eclipse.jetty.io.BufferCache;
 import org.eclipse.jetty.io.BufferCache.CachedBuffer;
-import org.eclipse.jetty.io.BufferDateCache;
 import org.eclipse.jetty.io.BufferUtil;
 import org.eclipse.jetty.io.ByteArrayBuffer;
 import org.eclipse.jetty.util.LazyList;
@@ -67,14 +66,6 @@
 	/* ------------------------------------------------------------ */
 	public static final String __COOKIE_DELIM="\"\\\n\r\t\f\b%+ ;=";
 	public static final TimeZone __GMT = TimeZone.getTimeZone("GMT");
-	public static final BufferDateCache __dateCache = new BufferDateCache("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
-
-	/* -------------------------------------------------------------- */
-	static
-	{
-		__GMT.setID("GMT");
-		__dateCache.setTimeZone(__GMT);
-	}
 	
 	/* ------------------------------------------------------------ */
 	private static final String[] DAYS =
diff -r 359012f4e797 -r fa6158f29c45 src/org/eclipse/jetty/io/BufferDateCache.java
--- a/src/org/eclipse/jetty/io/BufferDateCache.java	Tue Sep 20 01:05:37 2016 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-//
-//  ========================================================================
-//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
-//  ------------------------------------------------------------------------
-//  All rights reserved. This program and the accompanying materials
-//  are made available under the terms of the Eclipse Public License v1.0
-//  and Apache License v2.0 which accompanies this distribution.
-//
-//      The Eclipse Public License is available at
-//      http://www.eclipse.org/legal/epl-v10.html
-//
-//      The Apache License v2.0 is available at
-//      http://www.opensource.org/licenses/apache2.0.php
-//
-//  You may elect to redistribute this code under either of these licenses.
-//  ========================================================================
-//
-
-package org.eclipse.jetty.io;
-
-import java.text.DateFormatSymbols;
-import java.util.Locale;
-
-import org.eclipse.jetty.util.DateCache;
-
-public class BufferDateCache extends DateCache
-{
-    Buffer _buffer;
-    String _last;
-    
-    public BufferDateCache()
-    {
-        super();
-    }
-
-    public BufferDateCache(String format, DateFormatSymbols s)
-    {
-        super(format,s);
-    }
-
-    public BufferDateCache(String format, Locale l)
-    {
-        super(format,l);
-    }
-
-    public BufferDateCache(String format)
-    {
-        super(format);
-    }
-
-    public synchronized Buffer formatBuffer(long date)
-    {
-        String d = super.format(date);
-        if (d==_last)
-            return _buffer;
-        _last=d;
-        _buffer=new ByteArrayBuffer(d);
-        
-        return _buffer;
-    }
-}
diff -r 359012f4e797 -r fa6158f29c45 src/org/eclipse/jetty/server/NCSARequestLog.java
--- a/src/org/eclipse/jetty/server/NCSARequestLog.java	Tue Sep 20 01:05:37 2016 -0600
+++ b/src/org/eclipse/jetty/server/NCSARequestLog.java	Tue Sep 20 21:38:44 2016 -0600
@@ -24,13 +24,14 @@
 import java.io.Writer;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.Locale;
 import java.util.TimeZone;
 
 import javax.servlet.http.Cookie;
 
 import org.eclipse.jetty.http.HttpHeaders;
-import org.eclipse.jetty.util.DateCache;
 import org.eclipse.jetty.util.component.AbstractLifeCycle;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -60,14 +61,14 @@
 	private boolean _preferProxiedForAddress;
 	private String _logDateFormat = "dd/MMM/yyyy:HH:mm:ss Z";
 	private Locale _logLocale = Locale.getDefault();
-	private String _logTimeZone = "GMT";
+	public TimeZone timeZone = TimeZone.getTimeZone("GMT");
 	private boolean _logLatency = false;
 	private boolean _logCookies = false;
 	private boolean _logServer = false;
 	private boolean _logDispatch = false;
 
 	private transient OutputStream _out;
-	private transient DateCache _logDateCache;
+	private transient DateFormat dateFormat;
 	private transient Writer _writer;
 
 	/* ------------------------------------------------------------ */
@@ -171,28 +172,6 @@
 
 	/* ------------------------------------------------------------ */
 	/**
-	 * Set the timezone of the request log.
-	 * 
-	 * @param tz timezone string
-	 */
-	public void setLogTimeZone(String tz)
-	{
-		_logTimeZone = tz;
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 * Retrieve the timezone of the request log.
-	 * 
-	 * @return timezone string
-	 */
-	public String getLogTimeZone()
-	{
-		return _logTimeZone;
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
 	 * Set the extended request log format flag.
 	 * 
 	 * @param extended true - log the extended request information,
@@ -361,13 +340,11 @@
 				addr = request.getRemoteAddr();
 
 			buf.append(addr);
-			buf.append(" - ");
+			buf.append(" - [");
 
-			buf.append(" [");
-			if (_logDateCache != null)
-				buf.append(_logDateCache.format(request.getTimeStamp()));
-			else
-				buf.append(request.getTimeStampBuffer().toString());
+			synchronized(dateFormat) {
+				buf.append(dateFormat.format(request.getTimeStamp()));
+			}
 
 			buf.append("] \"");
 			buf.append(request.getMethod());
@@ -520,11 +497,8 @@
 	@Override
 	protected synchronized void doStart() throws Exception
 	{
-		if (_logDateFormat != null)
-		{
-			_logDateCache = new DateCache(_logDateFormat,_logLocale);
-			_logDateCache.setTimeZoneID(_logTimeZone);
-		}
+		dateFormat = new SimpleDateFormat(_logDateFormat,_logLocale);
+		dateFormat.setTimeZone(timeZone);
 
 		if (_filename != null) {
 			File file = new File(_filename);
@@ -582,7 +556,7 @@
 
 			_out = null;
 			_closeOut = false;
-			_logDateCache = null;
+			dateFormat = null;
 			_writer = null;
 		}
 	}
diff -r 359012f4e797 -r fa6158f29c45 src/org/eclipse/jetty/server/Request.java
--- a/src/org/eclipse/jetty/server/Request.java	Tue Sep 20 01:05:37 2016 -0600
+++ b/src/org/eclipse/jetty/server/Request.java	Tue Sep 20 21:38:44 2016 -0600
@@ -28,6 +28,9 @@
 import java.net.InetAddress;
 import java.nio.ByteBuffer;
 import java.security.Principal;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.TimeZone;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
@@ -1226,7 +1229,7 @@
 	public Buffer getTimeStampBuffer()
 	{
 		if (_timeStampBuffer == null && _timeStamp > 0)
-			_timeStampBuffer = HttpFields.__dateCache.formatBuffer(_timeStamp);
+			_timeStampBuffer = formatBuffer(_timeStamp);
 		return _timeStampBuffer;
 	}
 
@@ -1981,4 +1984,24 @@
 		setParameters(parameters);
 		setQueryString(query);
 	}
+
+
+	private static Buffer _buffer;
+	private static String _last;
+	private static final DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
+	static {
+		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+	}
+
+	private static Buffer formatBuffer(long date) {
+		synchronized(dateFormat) {
+			String d = dateFormat.format(date);
+			if (d==_last)
+				return _buffer;
+			_last=d;
+			_buffer=new ByteArrayBuffer(d);
+			return _buffer;
+		}
+	}
+
 }
diff -r 359012f4e797 -r fa6158f29c45 src/org/eclipse/jetty/util/DateCache.java
--- a/src/org/eclipse/jetty/util/DateCache.java	Tue Sep 20 01:05:37 2016 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,311 +0,0 @@
-//
-//  ========================================================================
-//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
-//  ------------------------------------------------------------------------
-//  All rights reserved. This program and the accompanying materials
-//  are made available under the terms of the Eclipse Public License v1.0
-//  and Apache License v2.0 which accompanies this distribution.
-//
-//      The Eclipse Public License is available at
-//      http://www.eclipse.org/legal/epl-v10.html
-//
-//      The Apache License v2.0 is available at
-//      http://www.opensource.org/licenses/apache2.0.php
-//
-//  You may elect to redistribute this code under either of these licenses.
-//  ========================================================================
-//
-
-package org.eclipse.jetty.util;
-
-import java.text.DateFormatSymbols;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Locale;
-import java.util.TimeZone;
-
-/* ------------------------------------------------------------ */
-/**  Date Format Cache.
- * Computes String representations of Dates and caches
- * the results so that subsequent requests within the same minute
- * will be fast.
- *
- * Only format strings that contain either "ss" or "ss.SSS" are
- * handled.
- *
- * The timezone of the date may be included as an ID with the "zzz"
- * format string or as an offset with the "ZZZ" format string.
- *
- * If consecutive calls are frequently very different, then this
- * may be a little slower than a normal DateFormat.
- *
- * 
- * 
- */
-
-public class DateCache  
-{
-    public static String DEFAULT_FORMAT="EEE MMM dd HH:mm:ss zzz yyyy";
-    private static long __hitWindow=60*60;
-    
-    private String _formatString;
-    private String _tzFormatString;
-    private SimpleDateFormat _tzFormat;
-    
-    private String _minFormatString;
-    private SimpleDateFormat _minFormat;
-
-    private String _secFormatString;
-    private String _secFormatString0;
-    private String _secFormatString1;
-
-    private long _lastMinutes = -1;
-    private long _lastSeconds = -1;
-    private int _lastMs = -1;
-    private String _lastResult = null;
-
-    private Locale _locale	= null;
-    private DateFormatSymbols	_dfs	= null;
-
-    /* ------------------------------------------------------------ */
-    /** Constructor.
-     * Make a DateCache that will use a default format. The default format
-     * generates the same results as Date.toString().
-     */
-    public DateCache()
-    {
-        this(DEFAULT_FORMAT);
-        getFormat().setTimeZone(TimeZone.getDefault());
-    }
-    
-    /* ------------------------------------------------------------ */
-    /** Constructor.
-     * Make a DateCache that will use the given format
-     */
-    public DateCache(String format)
-    {
-        _formatString=format;
-        setTimeZone(TimeZone.getDefault());
-        
-    }
-    
-    /* ------------------------------------------------------------ */
-    public DateCache(String format,Locale l)
-    {
-        _formatString=format;
-        _locale = l;
-        setTimeZone(TimeZone.getDefault());       
-    }
-    
-    /* ------------------------------------------------------------ */
-    public DateCache(String format,DateFormatSymbols s)
-    {
-        _formatString=format;
-        _dfs = s;
-        setTimeZone(TimeZone.getDefault());
-    }
-
-    /* ------------------------------------------------------------ */
-    /** Set the timezone.
-     * @param tz TimeZone
-     */
-    public synchronized void setTimeZone(TimeZone tz)
-    {
-        setTzFormatString(tz);        
-        if( _locale != null ) 
-        {
-            _tzFormat=new SimpleDateFormat(_tzFormatString,_locale);
-            _minFormat=new SimpleDateFormat(_minFormatString,_locale);
-        }
-        else if( _dfs != null ) 
-        {
-            _tzFormat=new SimpleDateFormat(_tzFormatString,_dfs);
-            _minFormat=new SimpleDateFormat(_minFormatString,_dfs);
-        }
-        else 
-        {
-            _tzFormat=new SimpleDateFormat(_tzFormatString);
-            _minFormat=new SimpleDateFormat(_minFormatString);
-        }
-        _tzFormat.setTimeZone(tz);
-        _minFormat.setTimeZone(tz);
-        _lastSeconds=-1;
-        _lastMinutes=-1;        
-    }
-
-    /* ------------------------------------------------------------ */
-    public TimeZone getTimeZone()
-    {
-        return _tzFormat.getTimeZone();
-    }
-    
-    /* ------------------------------------------------------------ */
-    /** Set the timezone.
-     * @param timeZoneId TimeZoneId the ID of the zone as used by
-     * TimeZone.getTimeZone(id)
-     */
-    public void setTimeZoneID(String timeZoneId)
-    {
-        setTimeZone(TimeZone.getTimeZone(timeZoneId));
-    }
-    
-    /* ------------------------------------------------------------ */
-    private synchronized void setTzFormatString(final  TimeZone tz )
-    {
-        int zIndex = _formatString.indexOf( "ZZZ" );
-        if( zIndex >= 0 )
-        {
-            String ss1 = _formatString.substring( 0, zIndex );
-            String ss2 = _formatString.substring( zIndex+3 );
-            int tzOffset = tz.getRawOffset();
-            
-            StringBuilder sb = new StringBuilder(_formatString.length()+10);
-            sb.append(ss1);
-            sb.append("'");
-            if( tzOffset >= 0 )
-                sb.append( '+' );
-            else
-            {
-                tzOffset = -tzOffset;
-                sb.append( '-' );
-            }
-            
-            int raw = tzOffset / (1000*60);		// Convert to seconds
-            int hr = raw / 60;
-            int min = raw % 60;
-            
-            if( hr < 10 )
-                sb.append( '0' );
-            sb.append( hr );
-            if( min < 10 )
-                sb.append( '0' );
-            sb.append( min );
-            sb.append( '\'' );
-            
-            sb.append(ss2);
-            _tzFormatString=sb.toString();            
-        }
-        else
-            _tzFormatString=_formatString;
-        setMinFormatString();
-    }
-
-    
-    /* ------------------------------------------------------------ */
-    private void setMinFormatString()
-    {
-        int i = _tzFormatString.indexOf("ss.SSS");
-        int l = 6;
-        if (i>=0)
-            throw new IllegalStateException("ms not supported");
-        i = _tzFormatString.indexOf("ss");
-        l=2;
-        
-        // Build a formatter that formats a second format string
-        String ss1=_tzFormatString.substring(0,i);
-        String ss2=_tzFormatString.substring(i+l);
-        _minFormatString =ss1+"'ss'"+ss2;
-    }
-
-    /* ------------------------------------------------------------ */
-    /** Format a date according to our stored formatter.
-     * @param inDate 
-     * @return Formatted date
-     */
-    public synchronized String format(Date inDate)
-    {
-        return format(inDate.getTime());
-    }
-    
-    /* ------------------------------------------------------------ */
-    /** Format a date according to our stored formatter.
-     * @param inDate 
-     * @return Formatted date
-     */
-    public synchronized String format(long inDate)
-    {
-        long seconds = inDate / 1000;
-
-        // Is it not suitable to cache?
-        if (seconds<_lastSeconds ||
-            _lastSeconds>0 && seconds>_lastSeconds+__hitWindow)
-        {
-            // It's a cache miss
-            Date d = new Date(inDate);
-            return _tzFormat.format(d);
-            
-        }
-                                          
-        // Check if we are in the same second
-        // and don't care about millis
-        if (_lastSeconds==seconds )
-            return _lastResult;
-
-        Date d = new Date(inDate);
-        
-        // Check if we need a new format string
-        long minutes = seconds/60;
-        if (_lastMinutes != minutes)
-        {
-            _lastMinutes = minutes;
-            _secFormatString=_minFormat.format(d);
-
-            int i=_secFormatString.indexOf("ss");
-            int l=2;
-            _secFormatString0=_secFormatString.substring(0,i);
-            _secFormatString1=_secFormatString.substring(i+l);
-        }
-
-        // Always format if we get here
-        _lastSeconds = seconds;
-        StringBuilder sb=new StringBuilder(_secFormatString.length());
-        sb.append(_secFormatString0);
-        int s=(int)(seconds%60);
-        if (s<10)
-            sb.append('0');
-        sb.append(s);
-        sb.append(_secFormatString1);
-        _lastResult=sb.toString();
-
-                
-        return _lastResult;
-    }
-
-    /* ------------------------------------------------------------ */
-    /** Format to string buffer. 
-     * @param inDate Date the format
-     * @param buffer StringBuilder
-     */
-    public void format(long inDate, StringBuilder buffer)
-    {
-        buffer.append(format(inDate));
-    }
-    
-    /* ------------------------------------------------------------ */
-    /** Get the format.
-     */
-    public SimpleDateFormat getFormat()
-    {
-        return _minFormat;
-    }
-
-    /* ------------------------------------------------------------ */
-    public String getFormatString()
-    {
-        return _formatString;
-    }    
-
-    /* ------------------------------------------------------------ */
-    public String now()
-    {
-        long now=System.currentTimeMillis();
-        _lastMs=(int)(now%1000);
-        return format(now);
-    }
-
-    /* ------------------------------------------------------------ */
-    public int lastMs()
-    {
-        return _lastMs;
-    }
-}