Cross module single ServiceProvider for JPMS services
Uwe Schindler
uschindler at apache.org
Sun Apr 19 15:05:13 UTC 2020
Hi Alan,
from what I understood: He has some utility method in a shared class that wraps ServiceLoader to discover services and do something with it (the usual way to prevent code duplication). Problem is that ServiceLoade.load() is caller-sensitive, so a shared utility method only sees the services that the modules is allowed to see.
I answered on this in another mail: He can have a shared utility class, but the ServiceLoader instance should NOT be instantiated inside the utility class. What you suggest is to additionally open the shared module to allow reading services, which looks wrong.
Unfortunately, the caller-sensitiveness of ServiceLoader.load() cannot easily be seen from the Javadocs. IMHO there should be some improvements on this. I was stumbling on this, too.
Uwe
-----
Uwe Schindler
uschindler at apache.org
ASF Member, Apache Lucene PMC / Committer
Bremen, Germany
https://lucene.apache.org/
> -----Original Message-----
> From: jigsaw-dev <jigsaw-dev-bounces at openjdk.java.net> On Behalf Of Alan
> Bateman
> Sent: Sunday, April 19, 2020 4:23 PM
> To: Alex Sviridov <ooo_saturn7 at mail.ru>; jigsaw-dev <jigsaw-
> dev at openjdk.java.net>
> Subject: Re: Cross module single ServiceProvider for JPMS services
>
> On 19/04/2020 15:00, Alex Sviridov wrote:
> > Hi all,
> >
> > Let's suppose we have modules (A, B, C..) and every module consumes and
> provides services.
> > To find these services I want to create one util class, for example
> ServiceProvider that will be used
> > by all modules to get necessary services. There are two ideas:
> > * to have one point to control providing services within system
> > * to avoid code duplication.
> > However, I can't do it. The problem is that if we put ServiceProvider, for
> example, in module A,
> > then it can find only services that module A uses (service is declared in uses
> in module A module-info).
> > Is it possible to do, if yes, then how?
> If I read this correctly then we have code in module A looking to load
> service provider of type S but A's module declaration doesn't declares
> `uses S`. Have you tried the Module addUses method? That will add the
> "use" edge at run-time that should make this work.
>
> -Alan.
More information about the jigsaw-dev
mailing list