1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package io.earcam.acme.app;
20
21 import java.util.ServiceLoader;
22 import java.util.stream.StreamSupport;
23
24 import io.earcam.acme.api.AcmeApi;
25
26 public class AcmeApp {
27
28 public static void main(String[] args)
29 {
30 Module module = AcmeApp.class.getModule();
31 AcmeApi implementation = loadService(module);
32
33 String msg = implementation.greet(args[0]);
34
35 System.out.println(module + ": " + msg);
36 }
37
38
39 private static AcmeApi loadService(Module module)
40 {
41 ServiceLoader<AcmeApi> services = loadServiceLoader(module);
42
43 return StreamSupport
44 .stream(services.spliterator(), false)
45 .findFirst()
46 .orElseThrow(NullPointerException::new);
47 }
48
49
50 private static ServiceLoader<AcmeApi> loadServiceLoader(Module module)
51 {
52 ModuleLayer layer = module.getLayer();
53
54 ServiceLoader<AcmeApi> services = (layer == null) ? ServiceLoader.load(AcmeApi.class) : ServiceLoader.load(layer, AcmeApi.class);
55 return services;
56 }
57 }