Ch17 Modular Applications Flashcards
What command lines available to dynamically change Module config?
There are three command line options applicable to javac and java that can be used for customizing exports and requires configurations of modules temporarily (temporarily means only for that particular command execution). These are: add-reads, add-exports, and, add-opens
How to set declare multiple implementatiors of a service within one module?
module gamble.slots.impl{
Requires gamble.slots.spi;
Provides gamble.slots.spi.PayOffService with gamble.slots.impl.PayOffServiceImpl2,gamble.slots.impl.PayOffServiceImpl;
}
What is a Service Provider?
A service provider is the implementation of a service provider interface. At runtime it is possible to have multiple implementation classes or modules.
Provide an example of Service Provider class
What are the rules of Service Provider class?
No Provider -> no-arg constructor
If there is no provider method in the service provider class, then it must have a public no-args constructor
If a service provider has public static provider() method, then it is not required for the service provider to be a subtype of the service type. Only the provider method should return a subtype of the service type.
- If a service provider explicitly declares a public constructor with no formal parameters, or implicitly declares a public default constructor, then that constructor is called the provider constructor.
- If a service provider explicitly declares a public static method called provider with no formal parameters, then that method is called the provider method.
- If a service provider has a provider method, then its return type must (i) either be declared in the current module, or be declared in another module and be accessible to code in the current module; and (ii) be a subtype of the service specified in the provides directive; or a compile-time error occurs.
- While a service provider that is specified by a provides directive must be declared in the current module, its provider method may have a return type that is declared in another module. Also, note that when a service provider declares a provider method, the service provider itself need not be a subtype of the service.
- If a service provider does not have a provider method, then that service provider must have a provider constructor and must be a subtype of the service specified in the provides directive, or a compile-time error occurs.
Provide an example of a Provider with static Provider() method implementated
Example with static Provider() method implemented
import api.;
import java.util.;
public class DoNothingFilter {
public DoNothingFilter(int a){ }
public static Filter provider(){
return new Filter(){
public List<String> filter(List<String> l) { return l; }
};
}
}</String></String>
What is a Service Locator?
A service locator is able to
The ServiceLocator class is used to find any classes that implement a service provider interface.
You pass the service provider interface type to its load() method, it will return any implementation services it can find.
Provide an example how ServiceLocator can be used
How to load a service provider?
java.util.ServiceLoader<BloggerService>
bsLoader = java.util.ServiceLoader.load(BloggerService.class);</BloggerService>
Once you have the service providers, you can iterate through them like this (ServiceLoader implements Iterable and so, it can be used in an enhanced for loop):
for (BloggerService bs : bsLoader){
bs.blog(“Hello from textbook”);
}
You can also pick the first provider like this:
Optional<BloggerService> bs1 = bsLoader.findFirst();
bs1.ifPresent(bs->bs.method());</BloggerService>
Using stream():
ServiceLoader.load(ModuleHook.class).stream().map(ServiceLoader.Provider::get).forEach(hook -> {
ModuleInfo info = new ModuleInfo(hook);
nameToModules.put(info.getModule().getName(), info);
urlToModules.put(info.getUrlPath(), info);
});