1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }