Coverage Report - org.apache.maven.plugin.surefire.booterclient.ProviderDetector
 
Classes in this File Line Coverage Branch Coverage Complexity
ProviderDetector
0%
0/34
0%
0/24
5,333
 
 1  
 package org.apache.maven.plugin.surefire.booterclient;
 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 javax.annotation.Nonnull;
 23  
 import java.io.BufferedReader;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.io.InputStreamReader;
 27  
 import java.net.URL;
 28  
 import java.util.Collections;
 29  
 import java.util.Enumeration;
 30  
 import java.util.HashSet;
 31  
 import java.util.Set;
 32  
 
 33  
 /**
 34  
  * @author Stephen Conolly
 35  
  * @author Kristian Rosenvold
 36  
  * @noinspection UnusedDeclaration
 37  
  */
 38  0
 public class ProviderDetector
 39  
 {
 40  
 
 41  
     @Nonnull public static Set<String> getServiceNames( Class<?> clazz, ClassLoader classLoader )
 42  
         throws IOException
 43  
     {
 44  0
         final String resourceName = "META-INF/services/" + clazz.getName();
 45  
 
 46  0
         if ( classLoader == null )
 47  
         {
 48  0
             return Collections.emptySet();
 49  
         }
 50  0
         final Enumeration<URL> urlEnumeration = classLoader.getResources( resourceName );
 51  0
         return getNames( urlEnumeration );
 52  
     }
 53  
 
 54  
 
 55  
     /**
 56  
      * Method loadServices loads the services of a class that are
 57  
      * defined using the SPI mechanism.
 58  
      *
 59  
      * @param urlEnumeration The urls from the resource
 60  
      * @return The set of service provider names
 61  
      * @throws IOException When reading the streams fails
 62  
      */
 63  
     @Nonnull private static Set<String> getNames( final Enumeration<URL> urlEnumeration )
 64  
         throws IOException
 65  
     {
 66  0
         final Set<String> names = new HashSet<String>();
 67  
         nextUrl:
 68  0
         while ( urlEnumeration.hasMoreElements() )
 69  
         {
 70  0
             final URL url = urlEnumeration.nextElement();
 71  0
             final BufferedReader reader = getReader( url );
 72  
             try
 73  
             {
 74  
                 String line;
 75  0
                 while ( ( line = reader.readLine() ) != null )
 76  
                 {
 77  0
                     int ci = line.indexOf( '#' );
 78  0
                     if ( ci >= 0 )
 79  
                     {
 80  0
                         line = line.substring( 0, ci );
 81  
                     }
 82  0
                     line = line.trim();
 83  0
                     int n = line.length();
 84  0
                     if ( n == 0 )
 85  
                     {
 86  0
                         continue; // next line
 87  
                     }
 88  
 
 89  0
                     if ( ( line.indexOf( ' ' ) >= 0 ) || ( line.indexOf( '\t' ) >= 0 ) )
 90  
                     {
 91  
                         continue nextUrl; // next url
 92  
                     }
 93  0
                     char cp = line.charAt( 0 ); // should use codePointAt but this is JDK1.3
 94  0
                     if ( !Character.isJavaIdentifierStart( cp ) )
 95  
                     {
 96  
                         continue nextUrl; // next url
 97  
                     }
 98  0
                     for ( int i = 1; i < n; i++ )
 99  
                     {
 100  0
                         cp = line.charAt( i );  // should use codePointAt but this is JDK1.3
 101  0
                         if ( !Character.isJavaIdentifierPart( cp ) && ( cp != '.' ) )
 102  
                         {
 103  
                             continue nextUrl; // next url
 104  
                         }
 105  
                     }
 106  0
                     if ( !names.contains( line ) )
 107  
                     {
 108  0
                         names.add( line );
 109  
                     }
 110  0
                 }
 111  
             }
 112  
             finally
 113  
             {
 114  0
                 reader.close();
 115  0
             }
 116  0
         }
 117  
 
 118  0
         return names;
 119  
     }
 120  
 
 121  
     @Nonnull private static BufferedReader getReader( @Nonnull URL url )
 122  
         throws IOException
 123  
     {
 124  0
         final InputStream inputStream = url.openStream();
 125  0
         final InputStreamReader inputStreamReader = new InputStreamReader( inputStream );
 126  0
         return new BufferedReader( inputStreamReader );
 127  
     }
 128  
 }