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
30 import javax.annotation.Nonnull;
31
32
33
34
35 public class ProviderList
36 {
37 private final ProviderInfo[] wellKnownProviders;
38
39 private final ConfigurableProviderInfo dynamicProvider;
40
41 public ProviderList( ConfigurableProviderInfo dynamicProviderInfo, ProviderInfo... wellKnownProviders )
42 {
43 this.wellKnownProviders = wellKnownProviders;
44 this.dynamicProvider = dynamicProviderInfo;
45 }
46
47
48 public @Nonnull List<ProviderInfo> resolve( @Nonnull Log log )
49 {
50 List<ProviderInfo> providersToRun = new ArrayList<ProviderInfo>();
51
52 Set<String> manuallyConfiguredProviders = getManuallyConfiguredProviders();
53 if ( manuallyConfiguredProviders.size() > 0 )
54 {
55 for ( String name : manuallyConfiguredProviders )
56 {
57 ProviderInfo wellKnown = findByName( name );
58 ProviderInfo providerToAdd = wellKnown != null ? wellKnown : dynamicProvider.instantiate( name );
59 log.info( "Using configured provider " + providerToAdd.getProviderName() );
60 providersToRun.add( providerToAdd );
61 }
62 return providersToRun;
63 }
64
65 return autoDetectOneProvider();
66 }
67
68 private @Nonnull List<ProviderInfo> autoDetectOneProvider()
69 {
70 List<ProviderInfo> providersToRun = new ArrayList<ProviderInfo>();
71 for ( ProviderInfo wellKnownProvider : wellKnownProviders )
72 {
73 if ( wellKnownProvider.isApplicable() )
74 {
75 providersToRun.add( wellKnownProvider );
76 return providersToRun;
77 }
78 }
79 return providersToRun;
80 }
81
82 private Set<String> getManuallyConfiguredProviders()
83 {
84 try
85 {
86 return ProviderDetector.getServiceNames( SurefireProvider.class,
87 Thread.currentThread().getContextClassLoader() );
88 }
89
90 catch ( IOException e )
91 {
92 throw new RuntimeException( e );
93 }
94 }
95
96 private ProviderInfo findByName( String providerClassName )
97 {
98 for ( ProviderInfo wellKnownProvider : wellKnownProviders )
99 {
100 if ( wellKnownProvider.getProviderName().equals( providerClassName ) )
101 {
102 return wellKnownProvider;
103 }
104 }
105 return null;
106 }
107 }