Scanning modules?

Alan Bateman Alan.Bateman at oracle.com
Fri Sep 15 07:15:12 UTC 2017


On 15/09/2017 05:01, Greg Wilkins wrote:
> All,
> another question on scanning, but more jigsaw related than my other
> question.
>
> App containers have to scan jars deployed in EARs, WARs etc. for
> annotations, fragments, resources etc.  However, many containers also offer
> features to be able to optionally scan jars that are on the system
> classpath.
>
> For example, Jetty deploys the JSTL jar on the system classpath, but adds
> it to the annotation scanning for every webapp deployed.  Similarly when
> running in embedded mode, where all jars are on the system class path, we
> have been able to find and scan those that matched a pattern.
>
> However, with java9 modules, we have two problems if a jar like jstl.jar is
> deployed on at runtime.
>
> Firstly, the system loader is no longer a URLClassLoader, so we cannot scan
> to list of provided jar files looking for potential matches of known jars
> that we wish to scan.
>
> We can work around that by loading a known class from the known jars and
> asking for its location (via it's protection domain).   For a normal jar,
> this gives us a file URI, which we can then use as the basis for a scan.
>
> However, I expect that eventually JSTL will be converted to a module, and
> then the location will just be something like jrt://apache.jstl.      Which
> gives us our second problem - given a known or discovered module, how do we
> discover what classes are within that module in order to scan them for
> annotations?       We cannot find any API via Module nor the jrt URLHandler
> that gives us access to a list of classes?
>
> We can get a lists/sets of Packages,Exports etc. but nothing we can see
> gives us the actual classes/resources contained in that module, nor the
> location of the source jar file that we could open and thus determine the
> contents ourselves.
>
> So some guidance of how we are meant to do discovery of annotations,
> fragments etc in a modularized deployment would be very useful.
>
For the class path scanning then I assume you can use the 
java.class.path property, the type of the application or system class 
loader should not be interesting.

For the module path or other locations containing packaged modules then 
you create a ModuleFinder and invoke its findAll method to get a module 
reference to every module. Then you can use a ModuleReader to open the 
module. ModuleReader defines `list` and other methods to access its 
resources. My guess is this is where you will end up in that I suspect 
you are selecting the candidate modules to resolve and these modules 
will be instantiated in a child layer.

If you are really just looking to get at the contents of the modules in 
the boot layer (the modules that are resolved at startup) then you'll do 
something like this:

      ModuleLayer.boot().configuration().modules().stream()
             .map(ResolvedModule::reference)
             .forEach(mref -> {
                 System.out.println(mref.descriptor().name());
                 try (ModuleReader reader = mref.open()) {
                     reader.list().forEach(System.out::println);
                 } catch (IOException ioe) {
                     throw new UncheckedIOException(ioe);
                 }
             });

This code fragment processes the configuration for the boot layer, 
opening up each module and listing the names of the resources in every 
module.

You mention the jrt protocol handler. That is to support URL 
connections. There is also a jrt file system provider for tools that 
need to scan the contents of the current or other run-time image. I 
suspect you won't need this for what you are doing.

-Alan.



More information about the jigsaw-dev mailing list