Re: How to do “--add-modules” with dynamically created layer in JPMS?

2020-11-16 Thread Alan Bateman

On 16/11/2020 09:17, Alex Orlov wrote:

Hello Alan,
Thank you for such detailed answer. I read it with attention, but 
still don’t know how to solve my situation.

Maybe may question didn’t provide all details, so here they are.
I want to create the following layer structure:
+++
+ Boot Layer +
+++
       |
       +
       + Web Server Layer +
       +
                         |
                         
                         + Web Application Layer +
                         
So, we have always one boot layer (it’s clear), one web server layer, 
and multiple web application layers. At
the same time all web application layers have one parent — web server 
layer. Boot layer doesn’t know and mustn’t know
about any child layer modules So it is impossible to add all 
modules to boot layer!
In web server layer I have servlet container (Jetty) and rest 
implementation (Jersey). When I create web server layer
Configuration cf = 
parentLayer.configuration().resolveAndBind(moduleFinder, 
ModuleFinder.of(), moduleNames);
ModuleLayer layer = parentLayer.defineModulesWithOneLoader(cf, 
parentClassLoader);
I add to it all modules of jetty, jersey and their dependencies. At 
the same time jetty knows nothing about jersey. When web server layer 
is created one of its modules starts Jetty.
So, when does jersey start? It starts only when web application layer 
is created with .war file that has web.xml with jersey servlet. For 
example:


    JerseyServlet
org.glassfish.jersey.servlet.ServletContainer

So, in such configuration, when web server layer is created no one can 
know if jersey will be used.
What what does JPMS do? It ignores all jersey modules including all 
its dependencies (including API). So, the question.

How to ask JPMS not to ignore modules that it must add to layer?


Thanks for the diagram, and names, of the module layers.

If I read your mail correctly then you are saying that the Jersey module 
is observable when you create the configuration for "Web Server Layer". 
It is not resolved because it's not in the set of root modules that you 
specify to resolveAndBind. It is also not required by any of the modules 
that end up in the configuration. This should not be a surprise. If the 
intention for "Web Server Layer" to contain Jersey and other modules 
that web applications may need then you will need to add them to the set 
of root modules ("moduleNames" in your code fragment. If the modules are 
completely unrelated then you might want to scan them so that 
"moduleNames" has the names of the modules that export an API.


Does the .war file containing "JerseyServlet" have a module-info.class? 
I'm curious if it declares "requires Jersey" as I would expect that the 
resolve or resolveAndBind for the web application will fail if it 
requires Jersey and it is not observable or is not in the parent layer.


-Alan


Re: How to do “--add-modules” with dynamically created layer in JPMS?

2020-11-16 Thread Alan Bateman

On 15/11/2020 19:16, Alex Orlov wrote:

Hi all,
  
I create JPMS layer this way:
  
Configuration cf = parentLayer.configuration().resolveAndBind(moduleFinder, ModuleFinder.of(), moduleNames);

ModuleLayer layer = parentLayer.defineModulesWithOneLoader(cf, 
parentClassLoader);
  
And I have a problem with adding implementation modules. These implementation modules are ignored by JPMS because classes from these modules are not used anywhere (it is clear, that classes from API modules are used). So, JPMS doesn't add these modules to layer.
  
As I understand, if I spoke about implementation modules on boot layer, I could use --add-modules jvm argument. However, I couldn't find any information how to force JPMS load my module (even if its classes are not used) for dynamically created layers.
  
Could anyone say how to do it?
It would be useful if you could give an example of "implementation 
module". I can't tell if you mean a jdk.* module, a service provider 
module that doesn't export an API, or something else.


In any case, I think the picture you are describing is a module that is 
observable, either in the run-time image or on module path. The module 
is not resolved at startup (and so is not in the boot layer) because the 
initial module that you specify with `java -m` doesn't transitively 
require it and it doesn't provide an implementation of a service that 
any of the other resolved modules use.


The summary is a module layer is immutable and there isn't any way to 
add modules to a module layer once it has been created. For what you are 
doing then maybe `--add-modules ALL-SYSTEM` can be used as workaround. 
This will ensure that all system/platform modules are in the boot layer 
as these modules cannot be loaded into a child layer (Johannes's suggest 
to specify the module name to resolveAndBind will not work for java.* 
modules as they cannot be loaded into child layers, his suggestion may 
work for other modules).


One other point to mention is that the original set of requirement 
envisaged some means to augment the set of platform modules at run-time. 
We didn't get to that requirement except for the limited scenario that 
is tools loading the JMX agent or a java agent into a running VM. These 
limited scenario involve loading the jdk.management.agent or 
java.instrument modules and that is some with a special child layer 
rather than augmenting the boot layer. This partial solution, along with 
`--add-modules ALL-SYSTEM`as a workaround for the general cases, has 
been sufficient to date.


-Alan


Re: How to do “--add-modules” with dynamically created layer in JPMS?

2020-11-15 Thread Johannes Spangenberg
I think you need to specify the "implementation modules" in the last 
argument of Configuration.resolveAndBind(...).


Best regards,
Johannes

Am 15.11.2020 um 20:16 schrieb Alex Orlov:

Hi all,
  
I create JPMS layer this way:
  
Configuration cf = parentLayer.configuration().resolveAndBind(moduleFinder, ModuleFinder.of(), moduleNames);

ModuleLayer layer = parentLayer.defineModulesWithOneLoader(cf, 
parentClassLoader);
  
And I have a problem with adding implementation modules. These implementation modules are ignored by JPMS because classes from these modules are not used anywhere (it is clear, that classes from API modules are used). So, JPMS doesn't add these modules to layer.
  
As I understand, if I spoke about implementation modules on boot layer, I could use --add-modules jvm argument. However, I couldn't find any information how to force JPMS load my module (even if its classes are not used) for dynamically created layers.
  
Could anyone say how to do it?
  
  
--

Best regards, Alex Orlov