1 17 18 package org.apache.tomcat.util.http; 19 20 import java.text.DateFormat ; 21 import java.text.ParseException ; 22 import java.text.SimpleDateFormat ; 23 import java.util.Date ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 import java.util.TimeZone ; 27 import java.util.concurrent.ConcurrentHashMap ; 28 29 34 public final class FastHttpDateFormat { 35 36 37 39 40 protected static final int CACHE_SIZE = 41 Integer.parseInt(System.getProperty("org.apache.tomcat.util.http.FastHttpDateFormat.CACHE_SIZE", "1000")); 42 43 44 47 protected static final SimpleDateFormat format = 48 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 49 50 51 54 protected static final SimpleDateFormat formats[] = { 55 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US), 56 new SimpleDateFormat ("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US), 57 new SimpleDateFormat ("EEE MMMM d HH:mm:ss yyyy", Locale.US) 58 }; 59 60 61 protected final static TimeZone gmtZone = TimeZone.getTimeZone("GMT"); 62 63 64 67 static { 68 69 format.setTimeZone(gmtZone); 70 71 formats[0].setTimeZone(gmtZone); 72 formats[1].setTimeZone(gmtZone); 73 formats[2].setTimeZone(gmtZone); 74 75 } 76 77 78 81 protected static long currentDateGenerated = 0L; 82 83 84 87 protected static String currentDate = null; 88 89 90 93 protected static final ConcurrentHashMap <Long , String > formatCache = 94 new ConcurrentHashMap <Long , String >(CACHE_SIZE); 95 96 97 100 protected static final ConcurrentHashMap <String , Long > parseCache = 101 new ConcurrentHashMap <String , Long >(CACHE_SIZE); 102 103 104 106 107 110 public static final String getCurrentDate() { 111 112 long now = System.currentTimeMillis(); 113 if ((now - currentDateGenerated) > 1000) { 114 synchronized (format) { 115 if ((now - currentDateGenerated) > 1000) { 116 currentDateGenerated = now; 117 currentDate = format.format(new Date (now)); 118 } 119 } 120 } 121 return currentDate; 122 123 } 124 125 126 129 public static final String formatDate 130 (long value, DateFormat threadLocalformat) { 131 132 Long longValue = new Long (value); 133 String cachedDate = formatCache.get(longValue); 134 if (cachedDate != null) 135 return cachedDate; 136 137 String newDate = null; 138 Date dateValue = new Date (value); 139 if (threadLocalformat != null) { 140 newDate = threadLocalformat.format(dateValue); 141 updateFormatCache(longValue, newDate); 142 } else { 143 synchronized (formatCache) { 144 synchronized (format) { 145 newDate = format.format(dateValue); 146 } 147 updateFormatCache(longValue, newDate); 148 } 149 } 150 return newDate; 151 152 } 153 154 155 158 public static final long parseDate(String value, 159 DateFormat [] threadLocalformats) { 160 161 Long cachedDate = parseCache.get(value); 162 if (cachedDate != null) 163 return cachedDate.longValue(); 164 165 Long date = null; 166 if (threadLocalformats != null) { 167 date = internalParseDate(value, threadLocalformats); 168 updateParseCache(value, date); 169 } else { 170 synchronized (parseCache) { 171 date = internalParseDate(value, formats); 172 updateParseCache(value, date); 173 } 174 } 175 if (date == null) { 176 return (-1L); 177 } else { 178 return date.longValue(); 179 } 180 181 } 182 183 184 187 private static final Long internalParseDate 188 (String value, DateFormat [] formats) { 189 Date date = null; 190 for (int i = 0; (date == null) && (i < formats.length); i++) { 191 try { 192 date = formats[i].parse(value); 193 } catch (ParseException e) { 194 ; 195 } 196 } 197 if (date == null) { 198 return null; 199 } 200 return new Long (date.getTime()); 201 } 202 203 204 207 private static void updateFormatCache(Long key, String value) { 208 if (value == null) { 209 return; 210 } 211 if (formatCache.size() > CACHE_SIZE) { 212 formatCache.clear(); 213 } 214 formatCache.put(key, value); 215 } 216 217 218 221 private static void updateParseCache(String key, Long value) { 222 if (value == null) { 223 return; 224 } 225 if (parseCache.size() > CACHE_SIZE) { 226 parseCache.clear(); 227 } 228 parseCache.put(key, value); 229 } 230 231 232 } 233 | Popular Tags |