Mandatory service requirement
Jesse Glick
jesse.glick at oracle.com
Fri Feb 17 08:15:50 PST 2012
On 02/17/2012 10:23 AM, Alan Bateman wrote:
> For cases where there isn't a default/fallback implementation and where any provider implementation will suffice
Probably the minority case as I argued before.
> it would be preferable to catch such issues at install time rather than failing at runtime.
It would be preferable, yes - the question is whether this minor bit of safety is worth the added conceptual & implementation complexity.
>> drop the need to declare that a service is used at all: if and when ServiceLoader is called, determine the providers
>
> the modules that are providing the service need to involved when generating the configuration.
My point is that the providing modules could be added to the configuration in other ways. For example:
module jaxp {
exports javax.xml.parsers;
}
module xerces {
requires jaxp;
provides service javax.xml.parsers.SAXParserFactory with ...;
}
module saxon {
requires jaxp;
provides service javax.xml.parsers.SAXParserFactory with ...;
}
module my.corp.wsparser {
requires jaxp;
}
package my.corp.wsparser;
class Parser {
void parse(my.corp.WebServiceInput input) {
SAXParser = SAXParserFactory.newInstance().newSAXParser();
// ...
}
}
module my.corp.app {
requires my.corp.wsparser;
requires saxon; // I have tested with this
}
Here the call to SAXParserFactory.newInstance() would succeed so long as the application ultimately includes some XML parser, without the need for any "requires service
javax.xml.parsers.SAXParserFactory" declaration. If you forgot to include one, this would probably be obvious (unless the parsing were on a rare and untested code path).
The application module hardcodes a particular parser implementation which is known to work well, but this does not seem onerous: the wsparser module could be reused with
other parsers, and offering a version of the app based on Xerces would be trivial. Conversely, quietly substituting Xerces at installation time might well lead to a host
of subtle bugs.
Even if you wanted to support "requires service javax.xml.parsers.SAXParserFactory" for the hypothetical case of an application whose author optimistically believes that
any XML parser would work equally well, there is no obvious reason why this declaration must be in the wsparser module and not in the app module - or should it be in the
jaxp module which would actually be calling ServiceLoader? So long as the xerces and/or saxon modules are *somewhere* in the configuration, it should not matter which
module(s) requested the service: the "provides service ..." declarations suffice for the module system to create an index of service -> providers, maintained during
installation.
More information about the jigsaw-dev
mailing list