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 package org.apache.catalina.util;
18
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21 import java.util.Locale;
22 import java.util.Queue;
23 import java.util.TimeZone;
24 import java.util.concurrent.ConcurrentLinkedQueue;
25
26 /**
27  * A thread safe wrapper around {@link SimpleDateFormat} that does not make use
28  * of ThreadLocal and - broadly - only creates enough SimpleDateFormat objects
29  * to satisfy the concurrency requirements.
30  *
31  * @deprecated Unused. This will be removed in Tomcat 10.
32  *             Use {@link org.apache.tomcat.util.http.ConcurrentDateFormat}
33  */

34 @Deprecated
35 public class ConcurrentDateFormat {
36
37     private final String format;
38     private final Locale locale;
39     private final TimeZone timezone;
40     private final Queue<SimpleDateFormat> queue = new ConcurrentLinkedQueue<>();
41
42     public static final String RFC1123_DATE = "EEE, dd MMM yyyy HH:mm:ss zzz";
43     public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
44
45     private static final ConcurrentDateFormat FORMAT_RFC1123;
46
47     static {
48         FORMAT_RFC1123 = new ConcurrentDateFormat(RFC1123_DATE, Locale.US, GMT);
49     }
50
51     public static String formatRfc1123(Date date) {
52         return FORMAT_RFC1123.format(date);
53     }
54
55     public ConcurrentDateFormat(String format, Locale locale,
56             TimeZone timezone) {
57         this.format = format;
58         this.locale = locale;
59         this.timezone = timezone;
60         SimpleDateFormat initial = createInstance();
61         queue.add(initial);
62     }
63
64     public String format(Date date) {
65         SimpleDateFormat sdf = queue.poll();
66         if (sdf == null) {
67             sdf = createInstance();
68         }
69         String result = sdf.format(date);
70         queue.add(sdf);
71         return result;
72     }
73
74     private SimpleDateFormat createInstance() {
75         SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
76         sdf.setTimeZone(timezone);
77         return sdf;
78     }
79 }
80