Skip to content

Commit

Permalink
Fixes asciidoctor#942. When an Asciidoctor instance is created with a…
Browse files Browse the repository at this point in the history
… classloader parameter, use this classloader also to autoregister extensions, converters, syntax highlighters and loggers.
  • Loading branch information
robertpanzer committed Aug 13, 2020
1 parent 7456234 commit 4eac2d6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[

== Unreleased

Bug Fixes::

* When creating an AsciidoctorJ instance with classloader parameter, extensions are not discovered (@ahus1) (#942)

== 2.4.0 (2020-07-19)

Improvement::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public ConverterRegistryExecutor(AsciidoctorJRuby asciidoctor) {
}

public void registerAllConverters() {
registerAllConverters(Thread.currentThread().getContextClassLoader());
}

public void registerAllConverters(ClassLoader classloader) {
ServiceLoader<ConverterRegistry> converterRegistryServiceLoader = ServiceLoader
.load(ConverterRegistry.class);
.load(ConverterRegistry.class, classloader);

for (ConverterRegistry converterRegistry : converterRegistryServiceLoader) {
converterRegistry.register(asciidoctor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ public ExtensionRegistryExecutor(AsciidoctorJRuby asciidoctor) {
}

public void registerAllExtensions() {
registerAllExtensions(Thread.currentThread().getContextClassLoader());
}

public void registerAllExtensions(ClassLoader classloader) {
ServiceLoader<ExtensionRegistry> extensionRegistryServiceLoader = ServiceLoader
.load(ExtensionRegistry.class);
.load(ExtensionRegistry.class, classloader);

for (ExtensionRegistry extensionRegistry : extensionRegistryServiceLoader) {
extensionRegistry.register(asciidoctor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ public static JRubyAsciidoctor create(List<String> loadPaths) {
}

public static JRubyAsciidoctor create(ClassLoader classloader) {
return processRegistrations(createJRubyAsciidoctorInstance(null, new ArrayList<>(), classloader));
return processRegistrations(
createJRubyAsciidoctorInstance(null, new ArrayList<>(), classloader),
classloader);
}

public static JRubyAsciidoctor create(ClassLoader classloader, String gemPath) {
return processRegistrations(createJRubyAsciidoctorInstance(Collections.singletonMap(GEM_PATH, gemPath), new ArrayList<>(), classloader));
return processRegistrations(
createJRubyAsciidoctorInstance(Collections.singletonMap(GEM_PATH, gemPath), new ArrayList<>(), classloader),
classloader);
}

public static JRubyAsciidoctor create(List<String> loadPaths, String gemPath) {
Expand All @@ -96,22 +100,49 @@ private static JRubyAsciidoctor processRegistrations(JRubyAsciidoctor asciidocto
return asciidoctor;
}

private static JRubyAsciidoctor processRegistrations(JRubyAsciidoctor asciidoctor, ClassLoader classloader) {
registerExtensions(asciidoctor, classloader);
registerConverters(asciidoctor, classloader);
registerSyntaxHighlighters(asciidoctor, classloader);
registerLogHandlers(asciidoctor, classloader);

JavaLogger.install(asciidoctor.getRubyRuntime(), asciidoctor);

return asciidoctor;
}

private static void registerConverters(AsciidoctorJRuby asciidoctor) {
new ConverterRegistryExecutor(asciidoctor).registerAllConverters();
}

private static void registerConverters(AsciidoctorJRuby asciidoctor, ClassLoader classloader) {
new ConverterRegistryExecutor(asciidoctor).registerAllConverters(classloader);
}

private static void registerExtensions(AsciidoctorJRuby asciidoctor) {
new ExtensionRegistryExecutor(asciidoctor).registerAllExtensions();
}

private static void registerExtensions(AsciidoctorJRuby asciidoctor, ClassLoader classloader) {
new ExtensionRegistryExecutor(asciidoctor).registerAllExtensions(classloader);
}

private static void registerSyntaxHighlighters(AsciidoctorJRuby asciidoctor) {
new SyntaxHighlighterRegistryExecutor(asciidoctor).registerAllSyntaxHighlighter();
}

private static void registerSyntaxHighlighters(AsciidoctorJRuby asciidoctor, ClassLoader classloader) {
new SyntaxHighlighterRegistryExecutor(asciidoctor).registerAllSyntaxHighlighter(classloader);
}

private static void registerLogHandlers(AsciidoctorJRuby asciidoctor) {
new LogHandlerRegistryExecutor(asciidoctor).registerAllLogHandlers();
}

private static void registerLogHandlers(AsciidoctorJRuby asciidoctor, ClassLoader classloader) {
new LogHandlerRegistryExecutor(asciidoctor).registerAllLogHandlers(classloader);
}

private static JRubyAsciidoctor createJRubyAsciidoctorInstance(Map<String, String> environmentVars, List<String> loadPaths, ClassLoader classloader) {
Ruby rubyRuntime = createRubyRuntime(environmentVars, loadPaths, classloader);
return new JRubyAsciidoctor(rubyRuntime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.util.ServiceLoader;

public class LogHandlerRegistryExecutor {
private static ServiceLoader<LogHandler> logHandlerServiceLoader = ServiceLoader
.load(LogHandler.class);

private AsciidoctorJRuby asciidoctor;

Expand All @@ -16,6 +14,13 @@ public LogHandlerRegistryExecutor(AsciidoctorJRuby asciidoctor) {
}

public void registerAllLogHandlers() {
registerAllLogHandlers(Thread.currentThread().getContextClassLoader());
}

public void registerAllLogHandlers(ClassLoader classloader) {
ServiceLoader<LogHandler> logHandlerServiceLoader = ServiceLoader
.load(LogHandler.class, classloader);

for (LogHandler logHandler: logHandlerServiceLoader) {
asciidoctor.registerLogHandler(logHandler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ public SyntaxHighlighterRegistryExecutor(AsciidoctorJRuby asciidoctor) {
}

public void registerAllSyntaxHighlighter() {
registerAllSyntaxHighlighter(Thread.currentThread().getContextClassLoader());
}

public void registerAllSyntaxHighlighter(ClassLoader classLoader) {
ServiceLoader<SyntaxHighlighterRegistry> serviceLoader = ServiceLoader
.load(SyntaxHighlighterRegistry.class);
.load(SyntaxHighlighterRegistry.class, classLoader);

for (SyntaxHighlighterRegistry extensionRegistry : serviceLoader) {
extensionRegistry.register(asciidoctor);
}
Expand Down

0 comments on commit 4eac2d6

Please sign in to comment.