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.surefire.providerapi;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.io.IOException;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Optional;
27  import java.util.Set;
28  
29  import org.apache.maven.surefire.api.provider.SurefireProvider;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.annotations.Requirement;
32  import org.codehaus.plexus.logging.Logger;
33  
34  import static java.lang.Thread.currentThread;
35  import static java.util.Arrays.stream;
36  import static java.util.Collections.emptyList;
37  import static java.util.stream.Collectors.toList;
38  
39  /**
40   * @author Kristian Rosenvold
41   */
42  @Component(role = ProviderDetector.class)
43  public final class ProviderDetector {
44      @Requirement
45      private Logger logger;
46  
47      @Requirement
48      private ServiceLoader serviceLoader;
49  
50      @Nonnull
51      public List<ProviderInfo> resolve(ConfigurableProviderInfo dynamicProvider, ProviderInfo... wellKnownProviders) {
52          Set<String> manuallyConfiguredProviders = getManuallyConfiguredProviders();
53  
54          List<ProviderInfo> providersToRun = manuallyConfiguredProviders.stream()
55                  .map(name -> findByName(name, wellKnownProviders).orElseGet(() -> dynamicProvider.instantiate(name)))
56                  .collect(toList());
57  
58          providersToRun.forEach(p -> logger.info("Using configured provider " + p.getProviderName()));
59  
60          if (providersToRun.isEmpty()) {
61              return autoDetectOneWellKnownProvider(wellKnownProviders)
62                      .map(Collections::singletonList)
63                      .orElse(emptyList());
64          } else {
65              return Collections.unmodifiableList(providersToRun);
66          }
67      }
68  
69      private Optional<ProviderInfo> autoDetectOneWellKnownProvider(ProviderInfo... wellKnownProviders) {
70          Optional<ProviderInfo> providerInfo =
71                  stream(wellKnownProviders).filter(ProviderInfo::isApplicable).findFirst();
72  
73          providerInfo.ifPresent(p -> logger.info("Using auto detected provider " + p.getProviderName()));
74  
75          return providerInfo;
76      }
77  
78      private Set<String> getManuallyConfiguredProviders() {
79          try {
80              ClassLoader cl = currentThread().getContextClassLoader();
81              return serviceLoader.lookup(SurefireProvider.class, cl);
82          } catch (IOException e) {
83              throw new RuntimeException(e);
84          }
85      }
86  
87      @Nonnull
88      private Optional<ProviderInfo> findByName(String providerClassName, ProviderInfo... wellKnownProviders) {
89          return stream(wellKnownProviders)
90                  .filter(p -> p.getProviderName().equals(providerClassName))
91                  .findFirst();
92      }
93  }