Author: veithen
Date: Sat Jul 25 18:23:55 2009
New Revision: 797814
URL: http://svn.apache.org/viewvc?rev=797814&view=rev
Log:
WSCOMMONS-489: Extended the dialect feature to make StAXUtils thread safe. Note
that this only changes the behavior for SJSXP; the factories in Woodstox are
already thread safe.
Added:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.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/util/stax/dialect/SJSXPDialect.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/main/java/org/apache/axiom/util/stax/dialect/UnknownStAXDialect.java
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/WoodstoxDialect.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=797814&r1=797813&r2=797814&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
Sat Jul 25 18:23:55 2009
@@ -22,6 +22,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.axiom.om.OMConstants;
+import org.apache.axiom.util.stax.dialect.StAXDialect;
import org.apache.axiom.util.stax.dialect.StAXDialectDetector;
import javax.xml.stream.XMLInputFactory;
@@ -424,7 +425,8 @@
}
});
}
- return StAXDialectDetector.normalize(factory);
+ StAXDialect dialect =
StAXDialectDetector.getDialect(factory.getClass());
+ return dialect.normalize(dialect.makeThreadSafe(factory));
} finally {
if (savedClassLoader != null) {
Thread.currentThread().setContextClassLoader(savedClassLoader);
@@ -550,7 +552,8 @@
factory.setProperty((String)entry.getKey(),
entry.getValue());
}
}
- return StAXDialectDetector.normalize(factory);
+ StAXDialect dialect =
StAXDialectDetector.getDialect(factory.getClass());
+ return 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/util/stax/dialect/SJSXPDialect.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/SJSXPDialect.java?rev=797814&r1=797813&r2=797814&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/SJSXPDialect.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/SJSXPDialect.java
Sat Jul 25 18:23:55 2009
@@ -37,6 +37,16 @@
Boolean.TRUE);
}
+ public XMLInputFactory makeThreadSafe(XMLInputFactory factory) {
+ factory.setProperty("reuse-instance", Boolean.FALSE);
+ return factory;
+ }
+
+ public XMLOutputFactory makeThreadSafe(XMLOutputFactory factory) {
+ factory.setProperty("reuse-instance", Boolean.FALSE);
+ return factory;
+ }
+
public XMLStreamReader normalize(XMLStreamReader reader) {
return new SJSXPStreamReaderWrapper(reader);
}
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=797814&r1=797813&r2=797814&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
Sat Jul 25 18:23:55 2009
@@ -71,6 +71,26 @@
void enableCDataReporting(XMLInputFactory factory);
/**
+ * Make an {...@link XMLInputFactory} object thread safe. The
implementation may do this either by
+ * configuring the factory or by creating a thread safe wrapper.
+ *
+ * @param factory
+ * the factory to make thread safe
+ * @return the thread safe factory
+ */
+ XMLInputFactory makeThreadSafe(XMLInputFactory factory);
+
+ /**
+ * Make an {...@link XMLOutputFactory} object thread safe. The
implementation may do this either by
+ * configuring the factory or by creating a thread safe wrapper.
+ *
+ * @param factory
+ * the factory to make thread safe
+ * @return the thread safe factory
+ */
+ XMLOutputFactory makeThreadSafe(XMLOutputFactory factory);
+
+ /**
* Normalize an {...@link XMLInputFactory}. This will make sure that the
readers created from the
* factory conform to the StAX specifications.
*
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/UnknownStAXDialect.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/UnknownStAXDialect.java?rev=797814&r1=797813&r2=797814&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/UnknownStAXDialect.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/UnknownStAXDialect.java
Sat Jul 25 18:23:55 2009
@@ -34,6 +34,16 @@
factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
}
+ public XMLInputFactory makeThreadSafe(XMLInputFactory factory) {
+ // Cross fingers and assume that the factory is already thread safe
+ return factory;
+ }
+
+ public XMLOutputFactory makeThreadSafe(XMLOutputFactory factory) {
+ // Cross fingers and assume that the factory is already thread safe
+ return factory;
+ }
+
public XMLInputFactory normalize(XMLInputFactory factory) {
return factory;
}
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/WoodstoxDialect.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/WoodstoxDialect.java?rev=797814&r1=797813&r2=797814&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/WoodstoxDialect.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/WoodstoxDialect.java
Sat Jul 25 18:23:55 2009
@@ -36,6 +36,16 @@
factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
}
+ public XMLInputFactory makeThreadSafe(XMLInputFactory factory) {
+ // Woodstox' factories are designed to be thread safe
+ return factory;
+ }
+
+ public XMLOutputFactory makeThreadSafe(XMLOutputFactory factory) {
+ // Woodstox' factories are designed to be thread safe
+ return factory;
+ }
+
public XMLStreamReader normalize(XMLStreamReader reader) {
return new WoodstoxStreamReaderWrapper(reader);
}
Added:
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=797814&view=auto
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
(added)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
Sat Jul 25 18:23:55 2009
@@ -0,0 +1,108 @@
+/*
+ * 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.om.util;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import junit.framework.TestCase;
+
+public class StAXUtilsTest extends TestCase {
+ public interface Action {
+ void execute() throws Exception;
+ }
+
+ private void testThreadSafety(final Action action) throws Throwable {
+ int threadCount = 10;
+ final List results = new ArrayList(threadCount);
+ for (int i=0; i<threadCount; i++) {
+ new Thread(new Runnable() {
+ public void run() {
+ Throwable result;
+ try {
+ for (int i=0; i<1000; i++) {
+ action.execute();
+ }
+ result = null;
+ } catch (Throwable ex) {
+ result = ex;
+ }
+ synchronized (results) {
+ results.add(result);
+ results.notifyAll();
+ }
+ }
+ }).start();
+ }
+ synchronized (results) {
+ while (results.size() < threadCount) {
+ results.wait();
+ }
+ }
+ for (Iterator it = results.iterator(); it.hasNext(); ) {
+ Throwable result = (Throwable)it.next();
+ if (result != null) {
+ throw result;
+ }
+ }
+ }
+
+ // Regression test for WSCOMMONS-489
+ public void testCreateXMLStreamReaderIsThreadSafe() throws Throwable {
+ testThreadSafety(new Action() {
+ public void execute() throws Exception {
+ String text = String.valueOf((int)(Math.random() * 10000));
+ String xml = "<root>" + text + "</root>";
+ XMLStreamReader reader = StAXUtils.createXMLStreamReader(new
StringReader(xml));
+ assertEquals(XMLStreamReader.START_DOCUMENT,
reader.getEventType());
+ assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
+ assertEquals(XMLStreamReader.CHARACTERS, reader.next());
+ assertEquals(text, reader.getText());
+ assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
+ assertEquals(XMLStreamReader.END_DOCUMENT, reader.next());
+ reader.close();
+ }
+ });
+ }
+
+ // Regression test for WSCOMMONS-489
+ public void testCreateXMLStreamWriterIsThreadSafe() throws Throwable {
+ testThreadSafety(new Action() {
+ public void execute() throws Exception {
+ String text = String.valueOf((int)(Math.random() * 10000));
+ StringWriter out = new StringWriter();
+ XMLStreamWriter writer = StAXUtils.createXMLStreamWriter(out);
+ writer.writeStartElement("root");
+ writer.writeCharacters(text);
+ writer.writeEndElement();
+ writer.writeEndDocument();
+ writer.flush();
+ writer.close();
+ assertEquals("<root>" + text + "</root>", out.toString());
+ }
+ });
+ }
+}
Propchange:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
------------------------------------------------------------------------------
svn:eol-style = native