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.
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);
});