Skip to content

Commit

Permalink
#185 extra synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 3, 2022
1 parent 8c91e37 commit 85ccf57
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/main/java/com/jcabi/xml/XSLDocument.java
Expand Up @@ -357,7 +357,9 @@ public String toString() {
public XML transform(final XML xml) {
final DocumentBuilder builder;
try {
builder = XSLDocument.DFACTORY.newDocumentBuilder();
synchronized (XSLDocument.DFACTORY) {
builder = XSLDocument.DFACTORY.newDocumentBuilder();
}
} catch (final ParserConfigurationException ex) {
throw new IllegalArgumentException(
String.format(
Expand Down Expand Up @@ -397,15 +399,17 @@ public String applyTo(final XML xml) {
* @param xml XML
* @param result Result
* @since 0.11
* @link https://stackoverflow.com/questions/4695489/capture-xslmessage-output-in-java
* @link <a href="https://stackoverflow.com/questions/4695489">Relevant SO question</a>
*/
private void transformInto(final XML xml, final Result result) {
final Transformer trans = this.transformer();
final ConsoleErrorListener errors = new ConsoleErrorListener();
trans.setErrorListener(errors);
final long start = System.nanoTime();
try {
trans.transform(new DOMSource(xml.node()), result);
synchronized (XSLDocument.DFACTORY) {
trans.transform(new DOMSource(xml.node()), result);
}
} catch (final TransformerException ex) {
throw new IllegalArgumentException(
String.format(
Expand All @@ -431,10 +435,10 @@ private void transformInto(final XML xml, final Result result) {
* @return The transformer
*/
private Transformer transformer() {
if (this.cached.get() == null) {
final Transformer trans;
synchronized (XSLDocument.TFACTORY) {
synchronized (XSLDocument.TFACTORY) {
if (this.cached.get() == null) {
XSLDocument.TFACTORY.setURIResolver(this.sources);
final Transformer trans;
try {
trans = XSLDocument.TFACTORY.newTransformer(
new StreamSource(new StringReader(this.xsl), this.sid)
Expand All @@ -448,11 +452,11 @@ private Transformer transformer() {
ex
);
}
for (final Map.Entry<String, Object> ent : this.params.entrySet()) {
trans.setParameter(ent.getKey(), ent.getValue());
}
this.cached.set(XSLDocument.forSaxon(trans));
}
for (final Map.Entry<String, Object> ent : this.params.entrySet()) {
trans.setParameter(ent.getKey(), ent.getValue());
}
this.cached.set(XSLDocument.forSaxon(trans));
}
return this.cached.get();
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/jcabi/xml/XSLDocumentTest.java
Expand Up @@ -230,13 +230,14 @@ void transformsInManyThreads() throws Exception {
);
final AtomicInteger done = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(1);
final int total = 10;
final int total = 50;
final ExecutorService exec = Executors.newFixedThreadPool(total);
for (int task = 0; task < total; ++task) {
exec.submit(
() -> {
latch.await();
xsl.transform(xml);
xsl.transform(xml);
done.incrementAndGet();
return 0;
}
Expand Down

0 comments on commit 85ccf57

Please sign in to comment.