Author: dims
Date: Mon Feb 25 07:44:33 2008
New Revision: 630882

URL: http://svn.apache.org/viewvc?rev=630882&view=rev
Log:
[PERF] Contribution from David Strite. The pool of XMLInputFactory and 
XMLOutputFactorys in StAXUtils is a source of contention. the factories 
in Woodstox are threadsafe, there can be a single instance of the 
factory. With a single instance of the factory, no synchronization is 
needed in StAXUtils. So let's get rid of the pool. If anyone needs
to support a parser which is not thread safe, they can wrap a pool
and use the standard plugin mechanism for plugging in a new stax impl
to plug it in.
 


Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java?rev=630882&r1=630881&r2=630882&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java
 Mon Feb 25 07:44:33 2008
@@ -41,133 +41,40 @@
 
 
 public class StAXUtils {
-
-    private static interface ObjectCreator {
-        Object newObject();
-    }
-
-    private static class Pool {
-        private final int MAX_POOL_SIZE = 100;
-        private final List objects = new ArrayList();
-        private final ObjectCreator objectCreator;
-
-        Pool(ObjectCreator[] creators) {
-            ObjectCreator oc = null;
-            for (int i = 0; i < creators.length; i++) {
-                try {
-                    creators[i].newObject();
-                    oc = creators[i];
-                    break;
-                } catch (Throwable t) {
-                    // Ignore me
-                }
-            }
-            if (oc == null) {
-                throw new IllegalStateException("No valid ObjectCreator 
found.");
-            }
-            objectCreator = oc;
-        }
-
-        synchronized Object getInstance() {
-            final int size = objects.size();
-            if (size > 0) {
-                return objects.remove(size - 1);
-            }
-            return objectCreator.newObject();
-        }
-
-        synchronized void releaseInstance(Object object) {
-            if (objects.size() < MAX_POOL_SIZE) {
-                objects.add(object);
-            }
-        }
-
-        synchronized void clear() {
-            objects.clear();
-        }
-    }
-
-    private static final Pool xmlInputFactoryPool = new Pool(new 
ObjectCreator[] {
-            new ObjectCreator() {
-                public Object newObject() {
-                    return AccessController.doPrivileged(
-                            new PrivilegedAction() {
-                                public Object run() {
-                                    Thread currentThread = 
Thread.currentThread();
-                                    ClassLoader savedClassLoader = 
currentThread.getContextClassLoader();
-                                    XMLInputFactory factory = null;
-                                    try {
-                                        
currentThread.setContextClassLoader(StAXUtils.class.getClassLoader());
-                                        factory = 
XMLInputFactory.newInstance();
-                                    }
-                                    finally {
-                                        
currentThread.setContextClassLoader(savedClassLoader);
-                                    }
-                                    return factory; 
-                                }
-                            });
-                }
-            },
-            new ObjectCreator() {
-                public Object newObject() {
-                    return XMLInputFactory.newInstance();
-                }
-            }
-    });
-
-    private static final Pool xmlOutputFactoryPool = new Pool(new 
ObjectCreator[] {
-            new ObjectCreator() {
-                public Object newObject() {
-                    return AccessController.doPrivileged(
-                            new PrivilegedAction() {
-                                public Object run() {
-                                                                       
-                                    Thread currentThread = 
Thread.currentThread();
-                                    ClassLoader savedClassLoader = 
currentThread.getContextClassLoader();
-                                    XMLOutputFactory factory = null;
-                                    try {
-                                        
currentThread.setContextClassLoader(StAXUtils.class.getClassLoader());
-                                        factory = 
XMLOutputFactory.newInstance();
-                                        
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
-                                    }
-                                    finally {
-                                        
currentThread.setContextClassLoader(savedClassLoader);
-                                    }
-                                    return factory;
-                                }
-                            });
-                }
-            },
-            new ObjectCreator() {
-                public Object newObject() {
-                    XMLOutputFactory factory = XMLOutputFactory.newInstance();
-                    
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
-                    return factory;
-                }
-            }
-    });
-
-
     private static Log log = LogFactory.getLog(StAXUtils.class);
     private static boolean isDebugEnabled = log.isDebugEnabled();
 
-
     /**
      * Gets an XMLInputFactory instance from pool.
      *
      * @return an XMLInputFactory instance.
      */
     public static XMLInputFactory getXMLInputFactory() {
-        return (XMLInputFactory) xmlInputFactoryPool.getInstance();
+        return (XMLInputFactory) AccessController.doPrivileged(
+                new PrivilegedAction() {
+                    public Object run() {
+                        Thread currentThread = Thread.currentThread();
+                        ClassLoader savedClassLoader = 
currentThread.getContextClassLoader();
+                        XMLInputFactory factory = null;
+                        try {
+                            
currentThread.setContextClassLoader(StAXUtils.class.getClassLoader());
+                            factory = XMLInputFactory.newInstance();
+                        }
+                        finally {
+                            
currentThread.setContextClassLoader(savedClassLoader);
+                        }
+                        return factory;
+                    }
+                });
     }
 
     /**
+     * @deprecated
      * Returns an XMLInputFactory instance for reuse.
      *
      * @param factory An XMLInputFactory instance that is available for reuse
      */
     public static void releaseXMLInputFactory(XMLInputFactory factory) {
-        xmlInputFactoryPool.releaseInstance(factory);
     }
 
     public static XMLStreamReader createXMLStreamReader(final InputStream in, 
final String encoding)
@@ -246,16 +153,33 @@
      * @return an XMLOutputFactory instance.
      */
     public static XMLOutputFactory getXMLOutputFactory() {
-        return (XMLOutputFactory) xmlOutputFactoryPool.getInstance();
+        return (XMLOutputFactory) AccessController.doPrivileged(
+                new PrivilegedAction() {
+                    public Object run() {
+
+                        Thread currentThread = Thread.currentThread();
+                        ClassLoader savedClassLoader = 
currentThread.getContextClassLoader();
+                        XMLOutputFactory factory = null;
+                        try {
+                            
currentThread.setContextClassLoader(StAXUtils.class.getClassLoader());
+                            factory = XMLOutputFactory.newInstance();
+                            
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
+                        }
+                        finally {
+                            
currentThread.setContextClassLoader(savedClassLoader);
+                        }
+                        return factory;
+                    }
+                });
     }
 
     /**
+     * @deprecated
      * Returns an XMLOutputFactory instance for reuse.
      *
      * @param factory An XMLOutputFactory instance that is available for reuse.
      */
     public static void releaseXMLOutputFactory(XMLOutputFactory factory) {
-        xmlOutputFactoryPool.releaseInstance(factory);
     }
 
     public static XMLStreamWriter createXMLStreamWriter(final OutputStream out)
@@ -277,8 +201,6 @@
             return writer;
         } catch (PrivilegedActionException pae) {
             throw (XMLStreamException) pae.getException();
-        } finally {
-            releaseXMLOutputFactory(outputFactory);
         }
     }
 
@@ -301,8 +223,6 @@
             return writer;
         } catch (PrivilegedActionException pae) {
             throw (XMLStreamException) pae.getException();
-        } finally {
-            releaseXMLOutputFactory(outputFactory);
         }
     }
 
@@ -324,13 +244,12 @@
             return writer;
         } catch (PrivilegedActionException pae) {
             throw (XMLStreamException) pae.getException();
-        } finally {
-            releaseXMLOutputFactory(outputFactory);
         }
     }
 
+    /**
+     * @deprecated
+     */
     public static void reset() {
-        xmlOutputFactoryPool.clear();
-        xmlInputFactoryPool.clear();
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to