View Javadoc

1   package org.apache.maven.plugin.surefire;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.logging.Log;
23  import org.apache.maven.plugin.surefire.booterclient.ProviderDetector;
24  import org.apache.maven.surefire.providerapi.SurefireProvider;
25  import org.apache.maven.surefire.util.NestedRuntimeException;
26  
27  import java.io.IOException;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Set;
32  
33  /**
34   * @author Kristian Rosenvold
35   */
36  public class ProviderList
37  {
38      private final ProviderInfo[] wellKnownProviders;
39  
40      private final ConfigurableProviderInfo dynamicProvider;
41  
42      public ProviderList( ProviderInfo[] wellKnownProviders, ConfigurableProviderInfo dynamicProviderInfo )
43      {
44          this.wellKnownProviders = wellKnownProviders;
45          this.dynamicProvider = dynamicProviderInfo;
46      }
47  
48  
49      public List resolve( Log log )
50      {
51          List providersToRun = new ArrayList();
52  
53          Set manuallyConfiguredProviders = getManuallyConfiguredProviders();
54          if ( manuallyConfiguredProviders.size() > 0 )
55          {
56              Iterator iter = manuallyConfiguredProviders.iterator();
57              String name;
58              while ( iter.hasNext() )
59              {
60                  name = (String) iter.next();
61                  ProviderInfo wellKnown = findByName( name );
62                  ProviderInfo providerToAdd = wellKnown != null ? wellKnown : dynamicProvider.instantiate( name );
63                  log.info( "Using configured provider " + providerToAdd.getProviderName() );
64                  providersToRun.add( providerToAdd );
65              }
66              return providersToRun;
67          }
68  
69          return autoDetectOneProvider();
70      }
71  
72      private List autoDetectOneProvider()
73      {
74          List providersToRun = new ArrayList();
75          for ( int i = 0; i < wellKnownProviders.length; i++ )
76          {
77              if ( wellKnownProviders[i].isApplicable() )
78              {
79                  providersToRun.add( wellKnownProviders[i] );
80                  return providersToRun;
81              }
82          }
83          return providersToRun;
84      }
85  
86      private Set getManuallyConfiguredProviders()
87      {
88          try
89          {
90              return ProviderDetector.getServiceNames( SurefireProvider.class,
91                                                       Thread.currentThread().getContextClassLoader() );
92          }
93  
94          catch ( IOException e )
95          {
96              throw new NestedRuntimeException( e );
97          }
98  
99  
100     }
101 
102     private ProviderInfo findByName( String providerClassName )
103     {
104         for ( int i = 0; i < wellKnownProviders.length; i++ )
105         {
106             ProviderInfo wellKnownProvider = wellKnownProviders[i];
107             if ( wellKnownProvider.getProviderName().equals( providerClassName ) )
108             {
109                 return wellKnownProvider;
110             }
111         }
112         return null;
113     }
114 }