Skip to content

Commit a7b12ea

Browse files
authored
Merge pull request #1173 from robertpanzer/fix-1158-v.2.5.x
(v2.5.x) Fixes #1158. Avoid concurrent initialization of Ruby wrapper class wh…
2 parents 098cfda + 1ef4aed commit a7b12ea

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

CHANGELOG.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Improvement::
3030
* Upgrade to asciidoctorj-diagram 2.2.4 (#1140)
3131
* Upgrade to jruby 9.3.10.0 (#1138) (@alexlevinfr)
3232

33+
Bug Fixes::
34+
35+
* Fix ConcurrentModificationException when converting to stream concurrently (#1158) (@rocketraman)
36+
3337
Build / Infrastructure::
3438

3539
* Replace use of deprecated 'numbered' attribute by 'sectnums' (#1127) (@abelsromero)

asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/JRubyAsciidoctor.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import org.asciidoctor.extension.JavaExtensionRegistry;
1111
import org.asciidoctor.extension.RubyExtensionRegistry;
1212
import org.asciidoctor.jruby.AsciidoctorJRuby;
13-
import org.asciidoctor.jruby.DirectoryWalker;
14-
import org.asciidoctor.jruby.ast.impl.AuthorImpl;
1513
import org.asciidoctor.jruby.ast.impl.DocumentHeaderImpl;
1614
import org.asciidoctor.jruby.ast.impl.NodeConverter;
1715
import org.asciidoctor.jruby.converter.internal.ConverterRegistryExecutor;
@@ -23,13 +21,29 @@
2321
import org.asciidoctor.log.LogHandler;
2422
import org.asciidoctor.log.LogRecord;
2523
import org.asciidoctor.syntaxhighlighter.SyntaxHighlighterRegistry;
26-
import org.jruby.*;
24+
import org.jruby.Ruby;
25+
import org.jruby.RubyClass;
26+
import org.jruby.RubyHash;
27+
import org.jruby.RubyInstanceConfig;
28+
import org.jruby.RubyModule;
2729
import org.jruby.exceptions.RaiseException;
2830
import org.jruby.javasupport.JavaEmbedUtils;
2931
import org.jruby.runtime.builtin.IRubyObject;
3032

31-
import java.io.*;
32-
import java.util.*;
33+
import java.io.File;
34+
import java.io.IOException;
35+
import java.io.InputStream;
36+
import java.io.OutputStream;
37+
import java.io.Reader;
38+
import java.io.Writer;
39+
import java.util.ArrayList;
40+
import java.util.Arrays;
41+
import java.util.Collection;
42+
import java.util.Collections;
43+
import java.util.HashMap;
44+
import java.util.List;
45+
import java.util.Map;
46+
import java.util.UUID;
3347
import java.util.logging.Logger;
3448

3549
public class JRubyAsciidoctor implements AsciidoctorJRuby, LogHandler {
@@ -60,6 +74,7 @@ private JRubyAsciidoctor(final Ruby rubyRuntime) {
6074

6175
this.rubyGemsPreloader = new RubyGemsPreloader(this.rubyRuntime);
6276
this.logHandlers.add(new JULLogHandler());
77+
RubyOutputStreamWrapper.getOrCreateOutputStreamWrapperClass(this.rubyRuntime);
6378
}
6479

6580
public static JRubyAsciidoctor create() {
@@ -184,7 +199,6 @@ public DocumentHeader readDocumentHeader(File file) {
184199
return toDocumentHeader(document);
185200
}
186201

187-
@SuppressWarnings("unchecked")
188202
@Override
189203
public DocumentHeader readDocumentHeader(String content) {
190204

@@ -217,10 +231,6 @@ private List<String> convertAllFiles(Map<String, Object> options, final Iterable
217231
return asciidoctorContent;
218232
}
219233

220-
private List<File> scanForAsciiDocFiles(DirectoryWalker directoryWalker) {
221-
return directoryWalker.scan();
222-
}
223-
224234
@Override
225235
public void requireLibrary(String... library) {
226236
requireLibraries(Arrays.asList(library));

asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyOutputStreamWrapper.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.jruby.RubyObject;
99
import org.jruby.RubyString;
1010
import org.jruby.anno.JRubyMethod;
11-
import org.jruby.runtime.ObjectAllocator;
1211
import org.jruby.runtime.ThreadContext;
1312
import org.jruby.runtime.builtin.IRubyObject;
1413

@@ -25,7 +24,7 @@ public class RubyOutputStreamWrapper extends RubyObject {
2524

2625
public static IRubyObject wrap(final Ruby rubyRuntime, final OutputStream out) {
2726

28-
final RubyClass rubyClass = getOrCreateOutputStreamWrapperClass(rubyRuntime);
27+
final RubyClass rubyClass = getOutputStreamWrapperClass(rubyRuntime);
2928

3029
final IRubyObject wrapper = rubyClass.allocate();
3130

@@ -53,18 +52,16 @@ public static RubyClass getOrCreateOutputStreamWrapperClass(final Ruby rubyRunti
5352
return outputStreamWrapperClass;
5453
}
5554

56-
final RubyClass rubyClass = asciidoctorModule.defineClassUnder(RUBY_CLASS_NAME, rubyRuntime.getObject(), new ObjectAllocator() {
57-
@Override
58-
public IRubyObject allocate(final Ruby runtime, final RubyClass klazz) {
59-
return new RubyOutputStreamWrapper(runtime, klazz);
60-
}
61-
});
62-
55+
final RubyClass rubyClass = asciidoctorModule.defineClassUnder(RUBY_CLASS_NAME, rubyRuntime.getObject(), RubyOutputStreamWrapper::new);
6356
rubyClass.defineAnnotatedMethods(RubyOutputStreamWrapper.class);
64-
6557
return rubyClass;
6658
}
6759

60+
private static RubyClass getOutputStreamWrapperClass(final Ruby rubyRuntime) {
61+
RubyModule asciidoctorModule = rubyRuntime.getModule("AsciidoctorJ");
62+
return asciidoctorModule.getClass(RUBY_CLASS_NAME);
63+
}
64+
6865
@JRubyMethod(name = "write", required = 1)
6966
public IRubyObject write(ThreadContext context, IRubyObject arg) throws IOException {
7067
writeToStream(arg);

0 commit comments

Comments
 (0)