Author: veithen
Date: Wed Aug  5 19:29:14 2009
New Revision: 801376

URL: http://svn.apache.org/viewvc?rev=801376&view=rev
Log:
Since StAXUtils caches the factory instances, we should not allow callers of 
getXMLInputFactory and getXMLOutputFactory to change the state of these 
factories.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLInputFactory.java
   (with props)
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLOutputFactory.java
   (with props)
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/xpath/DocumentNavigator.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialect.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
    webservices/commons/trunk/modules/axiom/src/docbkx/userguide.xml

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=801376&r1=801375&r2=801376&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
 Wed Aug  5 19:29:14 2009
@@ -24,6 +24,8 @@
 import org.apache.axiom.om.OMConstants;
 import org.apache.axiom.util.stax.dialect.StAXDialect;
 import org.apache.axiom.util.stax.dialect.StAXDialectDetector;
+import org.apache.axiom.util.stax.wrapper.ImmutableXMLInputFactory;
+import org.apache.axiom.util.stax.wrapper.ImmutableXMLOutputFactory;
 
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLOutputFactory;
@@ -426,7 +428,8 @@
                         });
                     }
                     StAXDialect dialect = 
StAXDialectDetector.getDialect(factory.getClass());
-                    return dialect.normalize(dialect.makeThreadSafe(factory));
+                    return new ImmutableXMLInputFactory(dialect.normalize(
+                            dialect.makeThreadSafe(factory)));
                 } finally {
                     if (savedClassLoader != null) {
                         
Thread.currentThread().setContextClassLoader(savedClassLoader);
@@ -553,7 +556,8 @@
                         }
                     }
                     StAXDialect dialect = 
StAXDialectDetector.getDialect(factory.getClass());
-                    return dialect.normalize(dialect.makeThreadSafe(factory));
+                    return new ImmutableXMLOutputFactory(dialect.normalize(
+                            dialect.makeThreadSafe(factory)));
                 } finally {
                     if (savedClassLoader != null) {
                         
Thread.currentThread().setContextClassLoader(savedClassLoader);

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/xpath/DocumentNavigator.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/xpath/DocumentNavigator.java?rev=801376&r1=801375&r2=801376&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/xpath/DocumentNavigator.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/xpath/DocumentNavigator.java
 Wed Aug  5 19:29:14 2009
@@ -42,9 +42,9 @@
 import org.jaxen.util.SingleObjectIterator;
 
 import javax.xml.namespace.QName;
-import javax.xml.stream.XMLInputFactory;
-import javax.xml.stream.XMLStreamReader;
 import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -552,30 +552,23 @@
      */
     public Object getDocument(String uri)
             throws FunctionCallException {
+        InputStream in = null;
         try {
-            XMLStreamReader parser;
-            XMLInputFactory xmlInputFactory = StAXUtils.getXMLInputFactory();
-            Boolean oldValue = (Boolean) 
xmlInputFactory.getProperty(XMLInputFactory.IS_COALESCING);
-            try {
-                xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, 
Boolean.TRUE);
-                if (uri.indexOf(':') == -1) {
-                    parser = xmlInputFactory.createXMLStreamReader(
-                            new FileInputStream(uri));
-                } else {
-                    URL url = new URL(uri);
-                    parser = xmlInputFactory.createXMLStreamReader(
-                            url.openStream());
-                }
-            } finally {
-                if (oldValue != null) {
-                    xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, 
oldValue);
-                }
-                StAXUtils.releaseXMLInputFactory(xmlInputFactory);
+            if (uri.indexOf(':') == -1) {
+                in = new FileInputStream(uri);
+            } else {
+                URL url = new URL(uri);
+                in = url.openStream();
             }
-            StAXOMBuilder builder =
-                    new StAXOMBuilder(parser);
-            return builder.getDocumentElement().getParent();
+            return new 
StAXOMBuilder(StAXUtils.createXMLStreamReader(in)).getDocument();
         } catch (Exception e) {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException ex) {
+                    // Ignore
+                }
+            }
             throw new FunctionCallException(e);
         }
     }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialect.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialect.java?rev=801376&r1=801375&r2=801376&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialect.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialect.java
 Wed Aug  5 19:29:14 2009
@@ -114,7 +114,13 @@
     
     /**
      * Make an {...@link XMLInputFactory} object thread safe. The 
implementation may do this either by
-     * configuring the factory or by creating a thread safe wrapper.
+     * configuring the factory or by creating a thread safe wrapper. The 
returned factory must be
+     * thread safe for all method calls that don't change the (visible) state 
of the factory. This
+     * means that thread safety is not required for
+     * {...@link 
XMLInputFactory#setEventAllocator(javax.xml.stream.util.XMLEventAllocator)},
+     * {...@link XMLInputFactory#setProperty(String, Object)},
+     * {...@link XMLInputFactory#setXMLReporter(javax.xml.stream.XMLReporter)} 
and
+     * {...@link XMLInputFactory#setXMLResolver(javax.xml.stream.XMLResolver)}.
      * 
      * @param factory
      *            the factory to make thread safe
@@ -124,7 +130,9 @@
     
     /**
      * Make an {...@link XMLOutputFactory} object thread safe. The 
implementation may do this either by
-     * configuring the factory or by creating a thread safe wrapper.
+     * configuring the factory or by creating a thread safe wrapper. The 
returned factory must be
+     * thread safe for all method calls that don't change the (visible) state, 
i.e. the properties,
+     * of the factory.
      * 
      * @param factory
      *            the factory to make thread safe

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLInputFactory.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLInputFactory.java?rev=801376&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLInputFactory.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLInputFactory.java
 Wed Aug  5 19:29:14 2009
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.util.stax.wrapper;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLReporter;
+import javax.xml.stream.XMLResolver;
+import javax.xml.stream.util.XMLEventAllocator;
+
+/**
+ * Wraps an {...@link XMLInputFactory} so that its state can no longer be 
changed. The state includes
+ * the properties as well as the {...@link XMLEventAllocator}, {...@link 
XMLReporter} and
+ * {...@link XMLResolver} instances configured on the factory.
+ */
+public class ImmutableXMLInputFactory extends XMLInputFactoryWrapper {
+    /**
+     * Constructor.
+     * 
+     * @param parent the parent factory
+     */
+    public ImmutableXMLInputFactory(XMLInputFactory parent) {
+        super(parent);
+    }
+
+    public void setEventAllocator(XMLEventAllocator allocator) {
+        throw new IllegalStateException("This factory is immutable");
+    }
+
+    public void setProperty(String name, Object value) throws 
IllegalArgumentException {
+        throw new IllegalStateException("This factory is immutable");
+    }
+
+    public void setXMLReporter(XMLReporter reporter) {
+        throw new IllegalStateException("This factory is immutable");
+    }
+
+    public void setXMLResolver(XMLResolver resolver) {
+        throw new IllegalStateException("This factory is immutable");
+    }
+}

Propchange: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLInputFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLOutputFactory.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLOutputFactory.java?rev=801376&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLOutputFactory.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLOutputFactory.java
 Wed Aug  5 19:29:14 2009
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.util.stax.wrapper;
+
+import javax.xml.stream.XMLOutputFactory;
+
+/**
+ * Wraps an {...@link XMLOutputFactory} so that its state (i.e. its 
properties) can no longer be
+ * changed.
+ */
+public class ImmutableXMLOutputFactory extends XMLOutputFactoryWrapper {
+    /**
+     * Constructor.
+     * 
+     * @param parent the parent factory
+     */
+    public ImmutableXMLOutputFactory(XMLOutputFactory parent) {
+        super(parent);
+    }
+
+    public void setProperty(String name, Object value) throws 
IllegalArgumentException {
+        throw new IllegalStateException("This factory is immutable");
+    }
+}

Propchange: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/wrapper/ImmutableXMLOutputFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java?rev=801376&r1=801375&r2=801376&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
 Wed Aug  5 19:29:14 2009
@@ -112,4 +112,24 @@
         writer.writeEmptyElement("root");
         writer.close();
     }
+    
+    public void testInputFactoryIsImmutable() throws Exception {
+        try {
+            
StAXUtils.getXMLInputFactory().setProperty("javax.xml.stream.isValidating",
+                    Boolean.TRUE);
+            fail("Expected IllegalStateException");
+        } catch (IllegalStateException ex) {
+            // Expected
+        }
+    }
+    
+    public void testOutputFactoryIsImmutable() throws Exception {
+        try {
+            
StAXUtils.getXMLOutputFactory().setProperty("javax.xml.stream.isRepairingNamespaces",
+                    Boolean.TRUE);
+            fail("Expected IllegalStateException");
+        } catch (IllegalStateException ex) {
+            // Expected
+        }
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/src/docbkx/userguide.xml
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/src/docbkx/userguide.xml?rev=801376&r1=801375&r2=801376&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/src/docbkx/userguide.xml (original)
+++ webservices/commons/trunk/modules/axiom/src/docbkx/userguide.xml Wed Aug  5 
19:29:14 2009
@@ -610,13 +610,13 @@
                 <para>
                     The <methodname>getInputFactory</methodname> and 
<methodname>getOutputFactory</methodname>
                     methods in <classname>StAXUtils</classname> give access to 
the cached factories.
-                    Axiom doesn't restrict access to the 
<methodname>setProperty</methodname> method
+                    In versions prior to 1.2.9, Axiom didn't restrict access 
to the <methodname>setProperty</methodname> method
                     of these factories. In principle this makes it possible to 
change the configuration
                     of these factories for the whole application. However, 
since this depends on the
                     implementation details of <classname>StAXUtils</classname> 
(e.g. how factories
                     are cached) and since there is a proper configuration 
mechanism for that purpose,
-                    using this possibility is strongly discouraged. Future 
versions of Axiom may
-                    restrict access to <methodname>setProperty</methodname> to 
prevent tampering with
+                    using this possibility is strongly discouraged. Starting 
with version 1.2.9, Axiom
+                    restricts access to <methodname>setProperty</methodname> 
to prevent tampering with
                     the cached factories.
                 </para>
             </important>
@@ -1066,6 +1066,22 @@
                     </para>
 
<programlisting>org.apache.axiom.om.OMMetaFactory=org.apache.axiom.om.impl.dom.factory.OMDOMMetaFactory</programlisting>
                 </section>
+                <section>
+                    <title>Factories returned by 
<classname>StAXUtils</classname></title>
+                    <para>
+                        In versions prior to 1.2.9, the 
<classname>XMLInputFactory</classname> and
+                        <classname>XMLOutputFactory</classname> instances 
returned by <classname>StAXUtils</classname>
+                        were mutable, i.e. it was possible to change the 
properties of these factories. This is obviously
+                        an issue since the factory instances are cached and 
can be shared among several thread.
+                        To avoid programming errors, starting from 1.2.9, the 
factories are immutable and any attempt to
+                        change their state will result in an 
<classname>IllegalStateException</classname>.
+                    </para>
+                    <para>
+                        Note that the possibility to change the properties of 
these factories could be used to apply
+                        application wide settings. Starting with 1.2.9, Axiom 
has a proper mechanism to allow this.
+                        This feature is described in <xref 
linkend="factory.properties"/>.
+                    </para>
+                </section>
             </section>
         </section>
     </chapter>


Reply via email to