View Javadoc
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  public class ProviderDetector
39  {
40  
41      @Nonnull public static Set<String> getServiceNames( Class<?> clazz, ClassLoader classLoader )
42          throws IOException
43      {
44          final String resourceName = "META-INF/services/" + clazz.getName();
45  
46          if ( classLoader == null )
47          {
48              return Collections.emptySet();
49          }
50          final Enumeration<URL> urlEnumeration = classLoader.getResources( resourceName );
51          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          final Set<String> names = new HashSet<String>();
67          nextUrl:
68          while ( urlEnumeration.hasMoreElements() )
69          {
70              final URL url = urlEnumeration.nextElement();
71              final BufferedReader reader = getReader( url );
72              try
73              {
74                  String line;
75                  while ( ( line = reader.readLine() ) != null )
76                  {
77                      int ci = line.indexOf( '#' );
78                      if ( ci >= 0 )
79                      {
80                          line = line.substring( 0, ci );
81                      }
82                      line = line.trim();
83                      int n = line.length();
84                      if ( n == 0 )
85                      {
86                          continue; // next line
87                      }
88  
89                      if ( ( line.indexOf( ' ' ) >= 0 ) || ( line.indexOf( '\t' ) >= 0 ) )
90                      {
91                          continue nextUrl; // next url
92                      }
93                      char cp = line.charAt( 0 ); // should use codePointAt but this is JDK1.3
94                      if ( !Character.isJavaIdentifierStart( cp ) )
95                      {
96                          continue nextUrl; // next url
97                      }
98                      for ( int i = 1; i < n; i++ )
99                      {
100                         cp = line.charAt( i );  // should use codePointAt but this is JDK1.3
101                         if ( !Character.isJavaIdentifierPart( cp ) && ( cp != '.' ) )
102                         {
103                             continue nextUrl; // next url
104                         }
105                     }
106                     if ( !names.contains( line ) )
107                     {
108                         names.add( line );
109                     }
110                 }
111             }
112             finally
113             {
114                 reader.close();
115             }
116         }
117 
118         return names;
119     }
120 
121     @Nonnull private static BufferedReader getReader( @Nonnull URL url )
122         throws IOException
123     {
124         final InputStream inputStream = url.openStream();
125         final InputStreamReader inputStreamReader = new InputStreamReader( inputStream );
126         return new BufferedReader( inputStreamReader );
127     }
128 }