craigmcc 01/05/02 16:42:43
Modified: digester/src/java/org/apache/commons/digester Digester.java
Log:
Save the SAXParserFactory that is created the first time we create a parser in
this application. Empirical evidence indicates that creating the parser
factory can be very time consuming, so this should accelerate startup time.
To avoid race conditions on setting the properties of the actual parser
instance being created, the code that does this is now synchronized.
(Wanted to get this change in before moving to jakarta-commons, which is
coming up shortly.)
Revision Changes Path
1.4 +51 -14
jakarta-commons-sandbox/digester/src/java/org/apache/commons/digester/Digester.java
Index: Digester.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/digester/src/java/org/apache/commons/digester/Digester.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Digester.java 2001/04/16 21:41:41 1.3
+++ Digester.java 2001/05/02 23:42:42 1.4
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons-sandbox/digester/src/java/org/apache/commons/digester/Digester.java,v
1.3 2001/04/16 21:41:41 geirm Exp $
- * $Revision: 1.3 $
- * $Date: 2001/04/16 21:41:41 $
+ * $Header:
/home/cvs/jakarta-commons-sandbox/digester/src/java/org/apache/commons/digester/Digester.java,v
1.4 2001/05/02 23:42:42 craigmcc Exp $
+ * $Revision: 1.4 $
+ * $Date: 2001/05/02 23:42:42 $
*
* ====================================================================
*
@@ -103,7 +103,7 @@
* even from the same thread.</p>
*
* @author Craig McClanahan
- * @version $Revision: 1.3 $ $Date: 2001/04/16 21:41:41 $
+ * @version $Revision: 1.4 $ $Date: 2001/05/02 23:42:42 $
*/
public class Digester extends HandlerBase {
@@ -158,6 +158,12 @@
/**
+ * The SAXParserFactory that is created the first time we need it.
+ */
+ protected static SAXParserFactory factory = null;
+
+
+ /**
* The Locator associated with our parser.
*/
protected Locator locator = null;
@@ -170,6 +176,12 @@
/**
+ * Do we want a "namespace aware" parser?
+ */
+ protected boolean namespaceAware = false;
+
+
+ /**
* The SAXParser we will use to parse the input stream.
*/
protected SAXParser parser = null;
@@ -261,6 +273,28 @@
/**
+ * Return the "namespace aware" flag for parsers we create.
+ */
+ public boolean getNamespaceAware() {
+
+ return (this.namespaceAware);
+
+ }
+
+
+ /**
+ * Set the "namespace aware" flag for parsers we create.
+ *
+ * @param namespaceAware The new "namespace aware" flag
+ */
+ public void setNamespaceAware(boolean namespaceAware) {
+
+ this.namespaceAware = namespaceAware;
+
+ }
+
+
+ /**
* Return the SAXParser we will use to parse the input stream. If there
* is a problem creating the parser, return <code>null</code>.
*/
@@ -271,16 +305,19 @@
return (parser);
// Create and return a new parser
- try {
- SAXParserFactory factory = SAXParserFactory.newInstance();
- factory.setNamespaceAware(false);
- factory.setValidating(validating);
- parser = factory.newSAXParser();
- return (parser);
- } catch (Exception e) {
- log("Digester.getParser: ", e);
- return (null);
- }
+ synchronized (this) {
+ try {
+ if (factory == null)
+ factory = SAXParserFactory.newInstance();
+ factory.setNamespaceAware(namespaceAware);
+ factory.setValidating(validating);
+ parser = factory.newSAXParser();
+ return (parser);
+ } catch (Exception e) {
+ log("Digester.getParser: ", e);
+ return (null);
+ }
+ }
}