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