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 @SuppressWarnings( "checkstyle:modifierorder" )
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 @SuppressWarnings( "checkstyle:modifierorder" )
70 private @Nonnull List<ProviderInfo> autoDetectOneProvider()
71 {
72 List<ProviderInfo> providersToRun = new ArrayList<ProviderInfo>();
73 for ( ProviderInfo wellKnownProvider : wellKnownProviders )
74 {
75 if ( wellKnownProvider.isApplicable() )
76 {
77 providersToRun.add( wellKnownProvider );
78 return providersToRun;
79 }
80 }
81 return providersToRun;
82 }
83
84 private Set<String> getManuallyConfiguredProviders()
85 {
86 try
87 {
88 return ProviderDetector.getServiceNames( SurefireProvider.class,
89 Thread.currentThread().getContextClassLoader() );
90 }
91
92 catch ( IOException e )
93 {
94 throw new RuntimeException( e );
95 }
96 }
97
98 private ProviderInfo findByName( String providerClassName )
99 {
100 for ( ProviderInfo wellKnownProvider : wellKnownProviders )
101 {
102 if ( wellKnownProvider.getProviderName().equals( providerClassName ) )
103 {
104 return wellKnownProvider;
105 }
106 }
107 return null;
108 }
109 }