Ch17 Modular Applications Flashcards

1
Q

What command lines available to dynamically change Module config?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to set declare multiple implementatiors of a service within one module?

A

module gamble.slots.impl{
Requires gamble.slots.spi;
Provides gamble.slots.spi.PayOffService with gamble.slots.impl.PayOffServiceImpl2,gamble.slots.impl.PayOffServiceImpl;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Service Provider?

A

A service provider is the implementation of a service provider interface. At runtime it is possible to have multiple implementation classes or modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Provide an example of Service Provider class

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the rules of Service Provider class?

A

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.

  1. 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.
  2. If a service provider explicitly declares a public static method called provider with no formal parameters, then that method is called the provider method.
  3. 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.
  4. 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.
  5. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Provide an example of a Provider with static Provider() method implementated

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Service Locator?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Provide an example how ServiceLocator can be used

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to load a service provider?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly