You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "Matthias Hild (Jira)" <ji...@apache.org> on 2019/09/10 13:43:00 UTC

[jira] [Commented] (MNG-6275) ServiceLoaderFactory can't find implementations via ClassRealm

    [ https://issues.apache.org/jira/browse/MNG-6275?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16926655#comment-16926655 ] 

Matthias Hild commented on MNG-6275:
------------------------------------

This is issue is rightfully marked as critical and blocks migration to Java >= 9 for any plugins relying on ServiceLoader logic.

Could you please comment on _where you see the problem_ with the August 2017 commit that was trying to address the issue:

[https://github.com/apache/maven/commit/27a2bda3f4a8f5385c4cab360ed7365d3d3d3c09#diff-1cb44e9c06f435b494a39ea8bbaed863]

This commit uses the class-loader of {{ClassRealmManager.class}} as the parent class-loader when creating a new {{ClassRealm}}. This commit was reverted in October 2017:

[https://github.com/apache/maven/commit/cae779e4fbc6565a581f2c6adf9fb15348005603#diff-1cb44e9c06f435b494a39ea8bbaed863]

I recompiled 3.6.3-SNAPSHOT with this fix in place and, for example, the JS ScriptEngine (Nashorn) now loads as expected.

Moreover, the logic of this fix seems at first glance correct.

Many thanks for your great work!!!

M.

PS: With the risk of restating what is already obvious the the committers, here a brief problem statement for the uninitiated: {{java.util.ServiceLoader}} was heavily revised in Java 9 (see comments in code) and its method {{public Iterator<S> iterator()}} now delegates to an iterator constructed of the inner classes {{ModuleServicesLookupIterator}} and {{LazyClassPathLookupIterator}}.

In the following code snippet from  {{ModuleServicesLookupIterator, }}{{DefaultClassRealmManager}} is the {{currentLoader}}. As its {{.parent}} is null in the released versions of Maven, {{iteratorFor(null)}} uses the {{BootLoader}}'s {{ServiceCatalog}} where the requested services cannot be not found (as pointed out in earlier posts). 

 
{quote}@Override
 public boolean hasNext() {
  while (nextProvider == null && nextError == null) {
  // get next provider to load
  while (!iterator.hasNext()) {
    if (currentLoader == null) {
      return false;
    } else {
      currentLoader = currentLoader.getParent();
      iterator = iteratorFor(currentLoader);
    }
  }

  ...

}
{quote}
 

> ServiceLoaderFactory can't find implementations via ClassRealm
> --------------------------------------------------------------
>
>                 Key: MNG-6275
>                 URL: https://issues.apache.org/jira/browse/MNG-6275
>             Project: Maven
>          Issue Type: Bug
>          Components: Class Loading
>            Reporter: Robert Scholte
>            Assignee: Stephen Connolly
>            Priority: Critical
>             Fix For: 3.x / Backlog
>
>
> Spotted this issue via MANTRUN-200. The reason is that in the {{DefaultClassRealmManager}} a new realm is created where the parent classLoader is {{null}}. This implies that the bootstrap classloader is used as parent.
> With Java8 nashorn has become an extension and is not part of the bootstrap classloader anymore.
> It is kind of strange that we want the bootstrap classloader here, it makes more sense if the system classloader is used (but with Java 7 and older versions of Java this was not an issue, both worked fine).



--
This message was sent by Atlassian Jira
(v8.3.2#803003)