You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by sc...@apache.org on 2007/07/17 20:41:47 UTC

svn commit: r557019 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/impl/builder/ axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/ axiom-tests/src/test/java/org/apache/axiom/om/

Author: scheu
Date: Tue Jul 17 11:41:46 2007
New Revision: 557019

URL: http://svn.apache.org/viewvc?view=rev&rev=557019
Log:
WSCOMMONS-220
Contributor: Lizet Ernand & Rich Scheuerle
(1) Add StAX getProperty(String) support to OMStAXWrapper.
(2) Keep track of parser closure state in OMStAXWrapper and StAXBuilder
(3) (If requested) Release the parser when it is closed.

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMWrapperTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java?view=diff&rev=557019&r1=557018&r2=557019
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java Tue Jul 17 11:41:46 2007
@@ -76,6 +76,9 @@
 
     protected String charEncoding = null;
     
+    protected boolean _isClosed = false;              // Indicate if parser is closed
+    protected boolean _releaseParserOnClose = false;  // Defaults to legacy behavior, which is keep the reference
+
     
     /**
      * Constructor StAXBuilder.
@@ -233,12 +236,13 @@
                 OMText text = omfactory.createOMText(dataHandler, true);
                 omContainer.addChild(text);
                 return text;
-            } else {
-                return omfactory.createOMText(omContainer, parser.getText(), textType);
-            }
-        } catch (IllegalArgumentException e) {
-            return omfactory.createOMText(omContainer, parser.getText(), textType);
+            } 
+        } catch (IllegalArgumentException e) { 
+        	//parser.getProperty may throw illegalArgument exception, ignore
+        } catch (IllegalStateException e) {	
+        	//parser.getProperty may throw illegalState exceptions, ignore
         }
+        return omfactory.createOMText(omContainer, parser.getText(), textType);
     }
 
     /**
@@ -513,9 +517,17 @@
 
     public void close() {
         try {
-            parser.close();
+            if (!isClosed()) {
+                parser.close();
+            }
         } catch (XMLStreamException e) {
             throw new RuntimeException(e);
+        } finally {
+            _isClosed = true;
+            // Release the parser so that it can be GC'd or reused.
+            if (_releaseParserOnClose) {
+                parser = null;
+            }
         }
     }
 
@@ -546,6 +558,8 @@
         } catch (IllegalArgumentException e) {
             // according to the parser api, get property will return IllegalArgumentException, when that
             // property is not found.
+        } catch (IllegalStateException e) {
+            // it will also throw illegalStateExceptions if in wrong state, ignore
         }
         return false;
     }
@@ -559,5 +573,27 @@
             return "UTF-8";
         }
         return this.charEncoding;
+    }
+    
+    
+    /**
+     * @return if parser is closed
+     */
+    public boolean isClosed() {
+        return _isClosed;
+    }
+    
+    /**
+     * Indicate if the parser resource should be release when closed.
+     * @param value boolean
+     */
+    public void releaseParserOnClose(boolean value) {
+        
+        // Release parser if already closed
+        if (isClosed() && value) {
+            parser = null;
+        }
+        _releaseParserOnClose = value;
+        
     }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java?view=diff&rev=557019&r1=557018&r2=557019
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java Tue Jul 17 11:41:46 2007
@@ -60,6 +60,9 @@
 
     /** Field parser */
     private XMLStreamReader parser;
+    private boolean _isClosed = false;              // Indicate if parser is closed
+    private boolean _releaseParserOnClose = false;  // Defaults to legacy behavior, which is keep the reference
+
 
     /** Field rootNode */
     private OMNode rootNode;
@@ -771,9 +774,25 @@
      */
     public void close() throws XMLStreamException {
 
-        // this doesnot mean anything with respect to the OM
-        if (parser != null) {
-            parser.close();
+        // If there is a builder, it controls its parser
+        if (builder != null && builder instanceof StAXBuilder) {
+            StAXBuilder staxBuilder = (StAXBuilder) builder;
+            staxBuilder.close();
+            parser = null;
+        } else {
+            if (parser != null) {
+                try {
+                    if (_isClosed) {
+                        parser.close();
+                    }
+                } finally {
+                    _isClosed = true;
+                    // Release the parser so that it can be GC'd or reused.
+                    if (_releaseParserOnClose) {
+                        parser = null;
+                    }
+                }
+            }
         }
     }
 
@@ -939,14 +958,36 @@
                     return text.getDataHandler();
             }
         }
+        // Per spec, throw IllegalArgumentException
+        if (s == null) {
+            throw new IllegalArgumentException();
+        }
+        if (parser != null) {
+            return parser.getProperty(s);       	
+        }
+        // Delegate to the builder's parser.
+        if (builder != null && builder instanceof StAXBuilder) {
+            StAXBuilder staxBuilder = (StAXBuilder) builder;
+            if (!staxBuilder.isClosed()) {
+                // If the parser was closed by something other
+                // than the builder, an IllegalStateException is
+                // thrown.  For now, return null as this is unexpected
+                // by the caller.
+                try {
+                    return ((StAXBuilder) builder).getReaderProperty(s);
+                } catch (IllegalStateException ise) {
+                    return null;
+                }
+            }
+        }
         return null;
     }
+        
 
     /**
-     * This is a very important method. It keeps the navigator one step ahead
-     * and pushes it one event ahead. If the nextNode is null then navigable is
-     * set to false. At the same time the parser and builder are set up for the
-     * upcoming event generation.
+     * This is a very important method. It keeps the navigator one step ahead and pushes it one
+     * event ahead. If the nextNode is null then navigable is set to false. At the same time the
+     * parser and builder are set up for the upcoming event generation.
      * 
      * @throws XMLStreamException
      */
@@ -1323,5 +1364,42 @@
 
     public OMXMLParserWrapper getBuilder() {
         return builder;
+    }
+    
+    /**
+     * @return if parser is closed
+     */
+    public boolean isClosed() {
+        
+        // If there is a builder, the builder owns the parser
+        // and knows the isClosed status
+        if (builder != null && builder instanceof StAXBuilder) {
+           return ((StAXBuilder) builder).isClosed();
+        } else {
+            return _isClosed;
+        }
+    }
+    
+    /**
+     * Indicate if the parser resource should be release when closed.
+     * @param value boolean
+     */
+    public void releaseParserOnClose(boolean value) {
+        // if there is a StAXBuilder, it owns the parser 
+        // and controls the releaseOnClose status
+        if (builder != null && builder instanceof StAXBuilder) {
+            ((StAXBuilder) builder).releaseParserOnClose(value);
+            if (isClosed() && value) {
+                parser = null;
+            }
+            return;
+        } else {
+            // Release parser if already closed
+            if (isClosed() && value) {
+                parser = null;
+            }
+            _releaseParserOnClose = value;
+        }
+        
     }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMWrapperTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMWrapperTest.java?view=diff&rev=557019&r1=557018&r2=557019
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMWrapperTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMWrapperTest.java Tue Jul 17 11:41:46 2007
@@ -21,6 +21,7 @@
 
 import junit.framework.TestCase;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.impl.llom.OMStAXWrapper;
 
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamException;
@@ -59,6 +60,14 @@
 
 
             XMLStreamReader reader = wrap2Element.getXMLStreamReaderWithoutCaching();
+            
+            // Make sure the reader is an OMStAXWrapper
+            if (reader instanceof OMStAXWrapper) {
+                OMStAXWrapper wrapper = (OMStAXWrapper) reader;
+                assertTrue(!wrapper.isClosed());
+                wrapper.releaseParserOnClose(true);
+            }
+            
             int count = 0;
             while (reader.hasNext()) {
                 reader.next();
@@ -66,6 +75,18 @@
             }
 
             assertEquals(3, count);
+            
+            
+            // Make sure that the wrapper can be closed without failing
+            reader.close();
+            reader.close();  // This should be a noop since the parser is closed.
+            
+            // Closing the parser should also close the parser on the builder (since they are the same)
+            assertTrue(b.isClosed());
+            b.close(); // This should be a noop since the parser is closed
+            
+            // Calling getProperty after a close should return null, not an exception
+            assertTrue(reader.getProperty("dummyProperty") == null);
         } catch (XMLStreamException e) {
             fail(e.getMessage());
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: commons-dev-help@ws.apache.org