View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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 }