KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > util > core > FastHttpDateFormat


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

16 package com.blandware.atleap.webapp.util.core;
17
18 import java.util.Date JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.TimeZone JavaDoc;
22 import java.text.DateFormat JavaDoc;
23 import java.text.ParseException JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25
26 /**
27  * <p>Utility class to generate HTTP dates.</p>
28  * <p><a HREF="FastHttpDateFormat.java.htm"><i>View Source</i></a></p>
29  * <p>This source code is taken from Tomcat Apache</p>
30  *
31  * @author Remy Maucherat
32  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
33  * @version $Revision: 1.2 $ $Date: 2005/08/02 14:53:46 $
34  */

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

39
40     /**
41      * HTTP date format.
42      */

43     protected static final SimpleDateFormat JavaDoc format =
44         new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
45
46
47     /**
48      * The set of SimpleDateFormat formats to use in <code>getDateHeader()</code>.
49      */

50     protected static final SimpleDateFormat JavaDoc formats[] = {
51         new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US),
52         new SimpleDateFormat JavaDoc("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US),
53         new SimpleDateFormat JavaDoc("EEE MMMM d HH:mm:ss yyyy", Locale.US)
54     };
55
56
57     /**
58      * GMT timezone - all HTTP dates are on GMT
59      */

60     protected final static TimeZone JavaDoc gmtZone = TimeZone.getTimeZone("GMT");
61
62
63     static {
64
65         format.setTimeZone(gmtZone);
66
67         formats[0].setTimeZone(gmtZone);
68         formats[1].setTimeZone(gmtZone);
69         formats[2].setTimeZone(gmtZone);
70
71     }
72
73
74     /**
75      * Instant on which the currentDate object was generated.
76      */

77     protected static long currentDateGenerated = 0L;
78
79
80     /**
81      * Current formatted date.
82      */

83     protected static String JavaDoc currentDate = null;
84
85
86     /**
87      * Formatter cache.
88      */

89     protected static final HashMap JavaDoc formatCache = new HashMap JavaDoc();
90
91
92     /**
93      * Parser cache.
94      */

95     protected static final HashMap JavaDoc parseCache = new HashMap JavaDoc();
96
97
98     // --------------------------------------------------------- Public Methods
99

100
101     /**
102      * Gets the current date in HTTP format.
103      *
104      * @return Current date in HTTP format
105      */

106     public static final String JavaDoc getCurrentDate() {
107
108         long now = System.currentTimeMillis();
109         if ((now - currentDateGenerated) > 1000) {
110             synchronized (format) {
111                 if ((now - currentDateGenerated) > 1000) {
112                     currentDateGenerated = now;
113                     currentDate = format.format(new Date JavaDoc(now));
114                 }
115             }
116         }
117         return currentDate;
118
119     }
120
121
122     /**
123      * Formats a specified date to HTTP format. If local format is not
124      * <code>null</code>, it's used instead.
125      *
126      * @param value Date value to format
127      * @param threadLocalformat The format to use (or <code>null</code> -- then
128      * HTTP format will be used)
129      * @return Formatted date
130      */

131     public static final String JavaDoc formatDate(long value,
132                                           DateFormat JavaDoc threadLocalformat) {
133
134         String JavaDoc cachedDate = null;
135         Long JavaDoc longValue = new Long JavaDoc(value);
136         try {
137             cachedDate = (String JavaDoc) formatCache.get(longValue);
138         } catch (Exception JavaDoc e) {
139         }
140         if (cachedDate != null)
141             return cachedDate;
142
143         String JavaDoc newDate = null;
144         Date JavaDoc dateValue = new Date JavaDoc(value);
145         if (threadLocalformat != null) {
146             newDate = threadLocalformat.format(dateValue);
147             synchronized (formatCache) {
148                 updateCache(formatCache, longValue, newDate);
149             }
150         } else {
151             synchronized (formatCache) {
152                 newDate = format.format(dateValue);
153                 updateCache(formatCache, longValue, newDate);
154             }
155         }
156         return newDate;
157
158     }
159
160
161     /**
162      * Tries to parse the given date as an HTTP date. If local format list is not
163      * <code>null</code>, it's used instead.
164      *
165      * @param value The string to parse
166      * @param threadLocalformats Array of formats to use for parsing.
167      * If <code>null</code>, HTTP formats are used.
168      * @return Parsed date (or -1 if error occured)
169      */

170     public static final long parseDate(String JavaDoc value,
171                                        DateFormat JavaDoc[] threadLocalformats) {
172
173         Long JavaDoc cachedDate = null;
174         try {
175             cachedDate = (Long JavaDoc) parseCache.get(value);
176         } catch (Exception JavaDoc e) {
177         }
178         if (cachedDate != null)
179             return cachedDate.longValue();
180
181         Long JavaDoc date = null;
182         if (threadLocalformats != null) {
183             date = internalParseDate(value, threadLocalformats);
184             synchronized (parseCache) {
185                 updateCache(parseCache, value, date);
186             }
187         } else {
188             synchronized (parseCache) {
189                 date = internalParseDate(value, formats);
190                 updateCache(parseCache, value, date);
191             }
192         }
193         if (date == null) {
194             return (-1L);
195         } else {
196             return date.longValue();
197         }
198
199     }
200
201
202     /**
203      * Parses date with given formatters.
204      *
205      * @param value The string to parse
206      * @param formats Array of formats to use
207      * @return Parsed date (or <code>null</code> if no formatter mached)
208      */

209     private static final Long JavaDoc internalParseDate
210         (String JavaDoc value, DateFormat JavaDoc[] formats) {
211         Date JavaDoc date = null;
212         for (int i = 0; (date == null) && (i < formats.length); i++) {
213             try {
214                 date = formats[i].parse(value);
215             } catch (ParseException JavaDoc e) {
216                 ;
217             }
218         }
219         if (date == null) {
220             return null;
221         }
222         return new Long JavaDoc(date.getTime());
223     }
224
225
226     /**
227      * Updates cache.
228      *
229      * @param cache Cache to be updated
230      * @param key Key to be updated
231      * @param value New value
232      */

233     private static final void updateCache(HashMap JavaDoc cache, Object JavaDoc key,
234                                           Object JavaDoc value) {
235         if (value == null) {
236             return;
237         }
238         if (cache.size() > 1000) {
239             cache.clear();
240         }
241         cache.put(key, value);
242     }
243 }
244
Popular Tags