KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > http > FastHttpDateFormat


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.tomcat.util.http;
19
20 import java.text.DateFormat JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.TimeZone JavaDoc;
27 import java.util.concurrent.ConcurrentHashMap JavaDoc;
28
29 /**
30  * Utility class to generate HTTP dates.
31  *
32  * @author Remy Maucherat
33  */

34 public final class FastHttpDateFormat {
35
36
37     // -------------------------------------------------------------- Variables
38

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     /**
45      * HTTP date format.
46      */

47     protected static final SimpleDateFormat JavaDoc format =
48         new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
49
50
51     /**
52      * The set of SimpleDateFormat formats to use in getDateHeader().
53      */

54     protected static final SimpleDateFormat JavaDoc formats[] = {
55         new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US),
56         new SimpleDateFormat JavaDoc("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US),
57         new SimpleDateFormat JavaDoc("EEE MMMM d HH:mm:ss yyyy", Locale.US)
58     };
59
60
61     protected final static TimeZone JavaDoc gmtZone = TimeZone.getTimeZone("GMT");
62
63
64     /**
65      * GMT timezone - all HTTP dates are on GMT
66      */

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     /**
79      * Instant on which the currentDate object was generated.
80      */

81     protected static long currentDateGenerated = 0L;
82
83
84     /**
85      * Current formatted date.
86      */

87     protected static String JavaDoc currentDate = null;
88
89
90     /**
91      * Formatter cache.
92      */

93     protected static final ConcurrentHashMap JavaDoc<Long JavaDoc, String JavaDoc> formatCache =
94         new ConcurrentHashMap JavaDoc<Long JavaDoc, String JavaDoc>(CACHE_SIZE);
95
96
97     /**
98      * Parser cache.
99      */

100     protected static final ConcurrentHashMap JavaDoc<String JavaDoc, Long JavaDoc> parseCache =
101         new ConcurrentHashMap JavaDoc<String JavaDoc, Long JavaDoc>(CACHE_SIZE);
102
103
104     // --------------------------------------------------------- Public Methods
105

106
107     /**
108      * Get the current date in HTTP format.
109      */

110     public static final String JavaDoc 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 JavaDoc(now));
118                 }
119             }
120         }
121         return currentDate;
122
123     }
124
125
126     /**
127      * Get the HTTP format of the specified date.
128      */

129     public static final String JavaDoc formatDate
130         (long value, DateFormat JavaDoc threadLocalformat) {
131
132         Long JavaDoc longValue = new Long JavaDoc(value);
133         String JavaDoc cachedDate = formatCache.get(longValue);
134         if (cachedDate != null)
135             return cachedDate;
136
137         String JavaDoc newDate = null;
138         Date JavaDoc dateValue = new Date JavaDoc(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     /**
156      * Try to parse the given date as a HTTP date.
157      */

158     public static final long parseDate(String JavaDoc value,
159                                        DateFormat JavaDoc[] threadLocalformats) {
160
161         Long JavaDoc cachedDate = parseCache.get(value);
162         if (cachedDate != null)
163             return cachedDate.longValue();
164
165         Long JavaDoc 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     /**
185      * Parse date with given formatters.
186      */

187     private static final Long JavaDoc internalParseDate
188         (String JavaDoc value, DateFormat JavaDoc[] formats) {
189         Date JavaDoc date = null;
190         for (int i = 0; (date == null) && (i < formats.length); i++) {
191             try {
192                 date = formats[i].parse(value);
193             } catch (ParseException JavaDoc e) {
194                 ;
195             }
196         }
197         if (date == null) {
198             return null;
199         }
200         return new Long JavaDoc(date.getTime());
201     }
202
203
204     /**
205      * Update cache.
206      */

207     private static void updateFormatCache(Long JavaDoc key, String JavaDoc 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     /**
219      * Update cache.
220      */

221     private static void updateParseCache(String JavaDoc key, Long JavaDoc 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