|
70 | 70 | * RESERVED FOR INTERNAL USE. A class which provides utility methods. |
71 | 71 | */ |
72 | 72 | public final class Utility { |
| 73 | + |
| 74 | + /** |
| 75 | + * Thread local for storing GMT date format. |
| 76 | + */ |
| 77 | + private static ThreadLocal<DateFormat> |
| 78 | + RFC1123_GMT_DATE_TIME_FORMATTER = new ThreadLocal<DateFormat>() { |
| 79 | + @Override |
| 80 | + protected DateFormat initialValue() { |
| 81 | + final DateFormat formatter = new SimpleDateFormat(RFC1123_PATTERN, LOCALE_US); |
| 82 | + formatter.setTimeZone(GMT_ZONE); |
| 83 | + return formatter; |
| 84 | + } |
| 85 | + }; |
| 86 | + |
73 | 87 | /** |
74 | 88 | * Stores a reference to the GMT time zone. |
75 | 89 | */ |
@@ -116,13 +130,22 @@ public final class Utility { |
116 | 130 | * Used to create Json parsers and generators. |
117 | 131 | */ |
118 | 132 | private static final JsonFactory jsonFactory = new JsonFactory(); |
119 | | - |
| 133 | + |
120 | 134 | /** |
121 | | - * A factory to create SAXParser instances. |
| 135 | + * Thread local for SAXParser. |
122 | 136 | */ |
123 | | - private static final ThreadLocal<SAXParserFactory> saxParserFactory = new ThreadLocal<SAXParserFactory>() { |
124 | | - @Override public SAXParserFactory initialValue() { |
125 | | - return SAXParserFactory.newInstance(); |
| 137 | + private static final ThreadLocal<SAXParser> saxParserThreadLocal = new ThreadLocal<SAXParser>() { |
| 138 | + SAXParserFactory factory; |
| 139 | + @Override public SAXParser initialValue() { |
| 140 | + factory = SAXParserFactory.newInstance(); |
| 141 | + factory.setNamespaceAware(true); |
| 142 | + try { |
| 143 | + return factory.newSAXParser(); |
| 144 | + } catch (SAXException e) { |
| 145 | + throw new RuntimeException("Unable to create SAXParser", e); |
| 146 | + } catch (ParserConfigurationException e) { |
| 147 | + throw new RuntimeException("Check parser configuration", e); |
| 148 | + } |
126 | 149 | } |
127 | 150 | }; |
128 | 151 |
|
@@ -567,21 +590,18 @@ public static String getGMTTime() { |
567 | 590 |
|
568 | 591 | /** |
569 | 592 | * Returns the GTM date/time String for the specified value using the RFC1123 pattern. |
570 | | - * |
| 593 | + * |
571 | 594 | * @param date |
572 | 595 | * A <code>Date</code> object that represents the date to convert to GMT date/time in the RFC1123 |
573 | 596 | * pattern. |
574 | | - * |
| 597 | + * |
575 | 598 | * @return A <code>String</code> that represents the GMT date/time for the specified value using the RFC1123 |
576 | 599 | * pattern. |
577 | 600 | */ |
578 | 601 | public static String getGMTTime(final Date date) { |
579 | | - final DateFormat formatter = new SimpleDateFormat(RFC1123_PATTERN, LOCALE_US); |
580 | | - formatter.setTimeZone(GMT_ZONE); |
581 | | - return formatter.format(date); |
| 602 | + return RFC1123_GMT_DATE_TIME_FORMATTER.get().format(date); |
582 | 603 | } |
583 | 604 |
|
584 | | - |
585 | 605 | /** |
586 | 606 | * Returns the UTC date/time String for the specified value using Java's version of the ISO8601 pattern, |
587 | 607 | * which is limited to millisecond precision. |
@@ -668,8 +688,9 @@ public static JsonParser getJsonParser(final InputStream inStream) throws JsonPa |
668 | 688 | * @throws SAXException |
669 | 689 | */ |
670 | 690 | public static SAXParser getSAXParser() throws ParserConfigurationException, SAXException { |
671 | | - saxParserFactory.get().setNamespaceAware(true); |
672 | | - return saxParserFactory.get().newSAXParser(); |
| 691 | + SAXParser parser = saxParserThreadLocal.get(); |
| 692 | + parser.reset(); //reset to original config |
| 693 | + return parser; |
673 | 694 | } |
674 | 695 |
|
675 | 696 | /** |
@@ -811,9 +832,7 @@ public static HashMap<String, String> parseAccountString(final String parseStrin |
811 | 832 | * If the specified string is invalid. |
812 | 833 | */ |
813 | 834 | public static Date parseRFC1123DateFromStringInGMT(final String value) throws ParseException { |
814 | | - final DateFormat format = new SimpleDateFormat(RFC1123_PATTERN, Utility.LOCALE_US); |
815 | | - format.setTimeZone(GMT_ZONE); |
816 | | - return format.parse(value); |
| 835 | + return RFC1123_GMT_DATE_TIME_FORMATTER.get().parse(value); |
817 | 836 | } |
818 | 837 |
|
819 | 838 | /** |
|
0 commit comments