1818import java .io .FileReader ;
1919import java .io .IOException ;
2020import java .io .Reader ;
21+ import java .lang .ref .WeakReference ;
2122import java .util .ArrayList ;
2223import java .util .HashMap ;
2324import java .util .List ;
@@ -55,6 +56,30 @@ private enum State {
5556 summary , hierarchy , intf , methods , method , flow , clear , gaps , gap , constraints , key , index
5657 }
5758
59+ /**
60+ * It takes quite a while to create a new XML Input Factory
61+ */
62+ private static class CachedFactory {
63+ WeakReference <Thread > thread ;
64+ XMLInputFactory factory ;
65+
66+ public CachedFactory () {
67+
68+ XMLInputFactory factory = XMLInputFactory .newInstance ();
69+ factory .setProperty (XMLInputFactory .IS_SUPPORTING_EXTERNAL_ENTITIES , false );
70+ factory .setProperty (XMLInputFactory .SUPPORT_DTD , false );
71+ this .factory = factory ;
72+ this .thread = new WeakReference <Thread >(Thread .currentThread ());
73+ }
74+
75+ public boolean isValidForThisThread () {
76+ Thread thr = thread .get ();
77+ return Thread .currentThread () == thr ;
78+ }
79+ }
80+
81+ private CachedFactory cachedFactory ;
82+
5883 /**
5984 * Reads a summary xml and places the new summaries into the given data object.
6085 * This method closes the reader.
@@ -68,10 +93,13 @@ public void read(Reader reader, ClassMethodSummaries summaries)
6893 throws XMLStreamException , SummaryXMLException , IOException {
6994 XMLStreamReader xmlreader = null ;
7095 try {
71- XMLInputFactory factory = XMLInputFactory .newInstance ();
72- factory .setProperty (XMLInputFactory .IS_SUPPORTING_EXTERNAL_ENTITIES , false );
73- factory .setProperty (XMLInputFactory .SUPPORT_DTD , false );
74- xmlreader = factory .createXMLStreamReader (reader );
96+ //Sadly, the XML Input Factory is not thread safe :/
97+ CachedFactory cachedFact = cachedFactory ;
98+ if (cachedFact == null || !cachedFact .isValidForThisThread ()) {
99+ cachedFact = new CachedFactory ();
100+ this .cachedFactory = cachedFact ;
101+ }
102+ xmlreader = cachedFact .factory .createXMLStreamReader (reader );
75103 final MethodSummaries summary = summaries .getMethodSummaries ();
76104
77105 Map <String , String > sourceAttributes = new HashMap <String , String >();
0 commit comments