You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Kristofor Selden <ks...@oxygen.com> on 2000/05/09 02:58:58 UTC

thread safety issues

The following class is a test to show a thread safety problem with sharing
the same node for transformations but what I don't understand this shouldn't
this only be reading (rather than modifying) from the Node it is
transforming? Right now I am assuming that the Xerces implementation of a
Node keeps some state information that isn't thread safe. It seems to me
that the Xerces implementation has some thread safety issues but is there
something that Xalan could be doing? This test will sometimes not throw
exceptions but rarely will the transformations be accurate.

-Kristofor Selden

//Begin TestNodeThreadSafety.java

import java.io.*;
import org.apache.xalan.xslt.*;
import org.apache.xalan.xpath.xdom.XercesLiaison;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.*;
import org.xml.sax.*;

class TestNodeThreadSafety extends Thread
{
	private static DOMParser domParser = new DOMParser();

	private Document m_domDoc;
	private StylesheetRoot m_stylesheet;
	private Writer m_writer;
	
	public TestNodeThreadSafety(Document domDoc, StylesheetRoot
stylesheet, Writer writer) {
		m_domDoc = domDoc;
		m_stylesheet = stylesheet;
		m_writer = writer;
	}
	
	public void run() {
		try {
			XSLTProcessor processor =
XSLTProcessorFactory.getProcessor(new XercesLiaison());
			//This test fails without synchronizing the Node.
			//synchronized (m_domDoc) {
				m_stylesheet.process(processor, m_domDoc,
new XSLTResultTarget(m_writer));
			//}
			m_writer.close();
	
System.out.println(Thread.currentThread().getName()+" finished");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		String xmlSource = args.length>0 ? args[0]:"foo.xml";
		String xslSource = args.length>1 ? args[1]:"raw.xsl";
		int numberOfThreads = args.length>2 ?
Integer.parseInt(args[2]):200;
		StylesheetRoot stylesheet = null;
		try {
			domParser.parse(new InputSource(xmlSource));
			stylesheet = XSLTProcessorFactory.getProcessor(new
XercesLiaison()).processStylesheet(xslSource);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		Document domDoc = (Document)domParser.getDocument();
		System.out.println(stylesheet.getOutputMethod()+";
"+stylesheet.getOutputMediaType());
		System.out.println(xmlSource+"; "+xslSource+";
"+numberOfThreads);
		String extension = ".xml";
		if
(stylesheet.getOutputMethod()!=null&&stylesheet.getOutputMethod().equals("ht
ml")) {
			extension = ".html";
		}
		else if
(stylesheet.getOutputMethod()!=null&&stylesheet.getOutputMethod().equals("te
xt")) {
			extension = ".txt";
		}
		for (int i = 0; i < numberOfThreads; i++) {
			try {
				Writer writer = new
FileWriter("test"+i+extension);
				TestNodeThreadSafety test = new
TestNodeThreadSafety(domDoc,stylesheet,writer);
				test.start();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}// End TestNodeThreadSafety


The xsl that I used that seems to show the problem every time I run the test
is the following:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
	<xsl:output method="xml"/>
	<xsl:template match="/">
		<xsl:copy-of select="."/>
	</xsl:template>
</xsl:stylesheet>