1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.List;
26 import java.util.Map;
27 import java.util.NoSuchElementException;
28 import java.util.Optional;
29
30 import org.apache.maven.api.services.Lookup;
31 import org.apache.maven.api.services.LookupException;
32 import org.codehaus.plexus.PlexusContainer;
33 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
34
35 @Named
36 @Singleton
37 public class DefaultLookup implements Lookup {
38
39 private final PlexusContainer container;
40
41 @Inject
42 public DefaultLookup(PlexusContainer container) {
43 this.container = container;
44 }
45
46 @Override
47 public <T> T lookup(Class<T> type) {
48 try {
49 return container.lookup(type);
50 } catch (ComponentLookupException e) {
51 throw new LookupException(e);
52 }
53 }
54
55 @Override
56 public <T> T lookup(Class<T> type, String name) {
57 try {
58 return container.lookup(type, name);
59 } catch (ComponentLookupException e) {
60 throw new LookupException(e);
61 }
62 }
63
64 @Override
65 public <T> Optional<T> lookupOptional(Class<T> type) {
66 try {
67 return Optional.of(container.lookup(type));
68 } catch (ComponentLookupException e) {
69 if (e.getCause() instanceof NoSuchElementException) {
70 return Optional.empty();
71 }
72 throw new LookupException(e);
73 }
74 }
75
76 @Override
77 public <T> Optional<T> lookupOptional(Class<T> type, String name) {
78 try {
79 return Optional.of(container.lookup(type, name));
80 } catch (ComponentLookupException e) {
81 if (e.getCause() instanceof NoSuchElementException) {
82 return Optional.empty();
83 }
84 throw new LookupException(e);
85 }
86 }
87
88 @Override
89 public <T> List<T> lookupList(Class<T> type) {
90 try {
91 return container.lookupList(type);
92 } catch (ComponentLookupException e) {
93 throw new LookupException(e);
94 }
95 }
96
97 @Override
98 public <T> Map<String, T> lookupMap(Class<T> type) {
99 try {
100 return container.lookupMap(type);
101 } catch (ComponentLookupException e) {
102 throw new LookupException(e);
103 }
104 }
105 }