1
2
3
4
5
6 package gov.nist.secauto.metaschema.cli.processor.command;
7
8 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
9
10 import java.util.List;
11 import java.util.ServiceLoader;
12 import java.util.ServiceLoader.Provider;
13 import java.util.stream.Collectors;
14
15 import edu.umd.cs.findbugs.annotations.NonNull;
16 import nl.talsmasoftware.lazy4j.Lazy;
17
18
19
20
21
22
23
24
25
26 public final class CommandService {
27 private static final Lazy<CommandService> INSTANCE = Lazy.lazy(CommandService::new);
28 @NonNull
29 private final ServiceLoader<ICommand> loader;
30
31
32
33
34
35
36 public static CommandService getInstance() {
37 return INSTANCE.get();
38 }
39
40
41
42
43
44
45
46
47 private CommandService() {
48 ServiceLoader<ICommand> loader = ServiceLoader.load(ICommand.class);
49 assert loader != null;
50 this.loader = loader;
51 }
52
53
54
55
56
57
58 @NonNull
59 private ServiceLoader<ICommand> getLoader() {
60 return loader;
61 }
62
63
64
65
66
67
68 @NonNull
69 public List<ICommand> getCommands() {
70 return ObjectUtils.notNull(getLoader().stream()
71 .map(Provider<ICommand>::get)
72 .collect(Collectors.toUnmodifiableList()));
73 }
74 }