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