1 package org.apache.maven.plugin.surefire;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Set;
26 import org.apache.maven.plugin.logging.Log;
27 import org.apache.maven.plugin.surefire.booterclient.ProviderDetector;
28 import org.apache.maven.surefire.providerapi.SurefireProvider;
29 import org.apache.maven.surefire.util.NestedRuntimeException;
30
31 import javax.annotation.Nonnull;
32
33
34
35
36 public class ProviderList
37 {
38 private final ProviderInfo[] wellKnownProviders;
39
40 private final ConfigurableProviderInfo dynamicProvider;
41
42 public ProviderList( ConfigurableProviderInfo dynamicProviderInfo, ProviderInfo... wellKnownProviders )
43 {
44 this.wellKnownProviders = wellKnownProviders;
45 this.dynamicProvider = dynamicProviderInfo;
46 }
47
48
49 public @Nonnull List<ProviderInfo> resolve( @Nonnull Log log )
50 {
51 List<ProviderInfo> providersToRun = new ArrayList<ProviderInfo>();
52
53 Set<String> manuallyConfiguredProviders = getManuallyConfiguredProviders();
54 if ( manuallyConfiguredProviders.size() > 0 )
55 {
56 for ( String name : manuallyConfiguredProviders )
57 {
58 ProviderInfo wellKnown = findByName( name );
59 ProviderInfo providerToAdd = wellKnown != null ? wellKnown : dynamicProvider.instantiate( name );
60 log.info( "Using configured provider " + providerToAdd.getProviderName() );
61 providersToRun.add( providerToAdd );
62 }
63 return providersToRun;
64 }
65
66 return autoDetectOneProvider();
67 }
68
69 private @Nonnull List<ProviderInfo> autoDetectOneProvider()
70 {
71 List<ProviderInfo> providersToRun = new ArrayList<ProviderInfo>();
72 for ( ProviderInfo wellKnownProvider : wellKnownProviders )
73 {
74 if ( wellKnownProvider.isApplicable() )
75 {
76 providersToRun.add( wellKnownProvider );
77 return providersToRun;
78 }
79 }
80 return providersToRun;
81 }
82
83 private Set<String> getManuallyConfiguredProviders()
84 {
85 try
86 {
87 return ProviderDetector.getServiceNames( SurefireProvider.class,
88 Thread.currentThread().getContextClassLoader() );
89 }
90
91 catch ( IOException e )
92 {
93 throw new NestedRuntimeException( e );
94 }
95 }
96
97 private ProviderInfo findByName( String providerClassName )
98 {
99 for ( ProviderInfo wellKnownProvider : wellKnownProviders )
100 {
101 if ( wellKnownProvider.getProviderName().equals( providerClassName ) )
102 {
103 return wellKnownProvider;
104 }
105 }
106 return null;
107 }
108 }