Repository: maven-surefire Updated Branches: refs/heads/master 2b6926abd -> 1a65d61dd
[SUREFIRE-1404] Package of ServiceLoader was changed in version 2.20 and documentation uses package providerapi Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/1a65d61d Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/1a65d61d Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/1a65d61d Branch: refs/heads/master Commit: 1a65d61dd18147e905bbba1911534a07bfa03eed Parents: 2b6926a Author: Tibor17 <tibordig...@apache.org> Authored: Mon Aug 7 12:23:43 2017 +0200 Committer: Tibor17 <tibordig...@apache.org> Committed: Mon Aug 7 12:23:43 2017 +0200 ---------------------------------------------------------------------- .../surefire/booterclient/ProviderDetector.java | 2 +- .../surefire/providerapi/ServiceLoader.java | 174 +++++++++++++++++++ .../maven/surefire/spi/ServiceLoader.java | 171 ------------------ .../org/apache/maven/surefire/spi/SPITest.java | 1 + 4 files changed, 176 insertions(+), 172 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/1a65d61d/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ProviderDetector.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ProviderDetector.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ProviderDetector.java index 3460e3e..fce3d46 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ProviderDetector.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ProviderDetector.java @@ -19,7 +19,7 @@ package org.apache.maven.plugin.surefire.booterclient; * under the License. */ -import org.apache.maven.surefire.spi.ServiceLoader; +import org.apache.maven.surefire.providerapi.ServiceLoader; import javax.annotation.Nonnull; import java.io.IOException; http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/1a65d61d/maven-surefire-common/src/main/java/org/apache/maven/surefire/providerapi/ServiceLoader.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/surefire/providerapi/ServiceLoader.java b/maven-surefire-common/src/main/java/org/apache/maven/surefire/providerapi/ServiceLoader.java new file mode 100644 index 0000000..8753ff1 --- /dev/null +++ b/maven-surefire-common/src/main/java/org/apache/maven/surefire/providerapi/ServiceLoader.java @@ -0,0 +1,174 @@ +package org.apache.maven.surefire.providerapi; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import javax.annotation.Nonnull; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; + +import static java.lang.Character.isJavaIdentifierPart; +import static java.lang.Character.isJavaIdentifierStart; +import static java.util.Collections.emptySet; +import static org.apache.maven.surefire.util.ReflectionUtils.getConstructor; + +/** + * SPI loader for Surefire/Failsafe should use {@link Thread#getContextClassLoader() current ClassLoader}. + * <br> + * The {@link java.util.ServiceLoader} embedded in JVM uses + * {@link ClassLoader#getSystemClassLoader() System ClassLoader} and cannot be used in Surefire/Failsafe. + * + * @since 2.20 + */ +public class ServiceLoader +{ + + @Nonnull + @SuppressWarnings( "unchecked" ) + public <T> Set<T> load( Class<T> clazz, ClassLoader classLoader ) + { + try + { + Set<T> implementations = new HashSet<T>(); + for ( String fullyQualifiedClassName : lookup( clazz, classLoader ) ) + { + Class<?> implClass = classLoader.loadClass( fullyQualifiedClassName ); + implementations.add( (T) getConstructor( implClass ).newInstance() ); + } + return implementations; + } + catch ( IOException e ) + { + throw new IllegalStateException( e.getLocalizedMessage(), e ); + } + catch ( ClassNotFoundException e ) + { + throw new IllegalStateException( e.getLocalizedMessage(), e ); + } + catch ( InvocationTargetException e ) + { + throw new IllegalStateException( e.getLocalizedMessage(), e ); + } + catch ( InstantiationException e ) + { + throw new IllegalStateException( e.getLocalizedMessage(), e ); + } + catch ( IllegalAccessException e ) + { + throw new IllegalStateException( e.getLocalizedMessage(), e ); + } + } + + @Nonnull + public Set<String> lookup( Class<?> clazz, ClassLoader classLoader ) + throws IOException + { + final String resourceName = "META-INF/services/" + clazz.getName(); + + if ( classLoader == null ) + { + return emptySet(); + } + final Enumeration<URL> urls = classLoader.getResources( resourceName ); + return lookupSpiImplementations( urls ); + } + + /** + * Method loadServices loads the services of a class that are + * defined using the SPI mechanism. + * + * @param urlEnumeration The urls from the resource + * @return The set of service provider names + * @throws IOException When reading the streams fails + */ + @Nonnull + @SuppressWarnings( "checkstyle:innerassignment" ) + private static Set<String> lookupSpiImplementations( final Enumeration<URL> urlEnumeration ) + throws IOException + { + final Set<String> names = new HashSet<String>(); + nextUrl: + while ( urlEnumeration.hasMoreElements() ) + { + final URL url = urlEnumeration.nextElement(); + final BufferedReader reader = getReader( url ); + try + { + for ( String line; ( line = reader.readLine() ) != null; ) + { + int ci = line.indexOf( '#' ); + if ( ci >= 0 ) + { + line = line.substring( 0, ci ); + } + line = line.trim(); + int n = line.length(); + if ( n == 0 ) + { + continue; // next line + } + + if ( line.indexOf( ' ' ) >= 0 || line.indexOf( '\t' ) >= 0 ) + { + continue nextUrl; // next url + } + char cp = line.charAt( 0 ); // should use codePointAt but this was JDK1.3 + if ( !isJavaIdentifierStart( cp ) ) + { + continue nextUrl; // next url + } + for ( int i = 1; i < n; i++ ) + { + cp = line.charAt( i ); // should use codePointAt but this was JDK1.3 + if ( !isJavaIdentifierPart( cp ) && cp != '.' ) + { + continue nextUrl; // next url + } + } + if ( !names.contains( line ) ) + { + names.add( line ); + } + } + } + finally + { + reader.close(); + } + } + + return names; + } + + @Nonnull + private static BufferedReader getReader( @Nonnull URL url ) + throws IOException + { + final InputStream inputStream = url.openStream(); + final InputStreamReader inputStreamReader = new InputStreamReader( inputStream ); + return new BufferedReader( inputStreamReader ); + } +} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/1a65d61d/maven-surefire-common/src/main/java/org/apache/maven/surefire/spi/ServiceLoader.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/surefire/spi/ServiceLoader.java b/maven-surefire-common/src/main/java/org/apache/maven/surefire/spi/ServiceLoader.java deleted file mode 100644 index 1deae7c..0000000 --- a/maven-surefire-common/src/main/java/org/apache/maven/surefire/spi/ServiceLoader.java +++ /dev/null @@ -1,171 +0,0 @@ -package org.apache.maven.surefire.spi; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import javax.annotation.Nonnull; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.lang.reflect.InvocationTargetException; -import java.net.URL; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Set; - -import static java.lang.Character.isJavaIdentifierPart; -import static java.lang.Character.isJavaIdentifierStart; -import static java.util.Collections.emptySet; -import static org.apache.maven.surefire.util.ReflectionUtils.getConstructor; - -/** - * SPI loader for Java 1.5. - * - * @since 2.20 - */ -public class ServiceLoader -{ - - @Nonnull - @SuppressWarnings( "unchecked" ) - public <T> Set<T> load( Class<T> clazz, ClassLoader classLoader ) - { - try - { - Set<T> implementations = new HashSet<T>(); - for ( String fullyQualifiedClassName : lookup( clazz, classLoader ) ) - { - Class<?> implClass = classLoader.loadClass( fullyQualifiedClassName ); - implementations.add( (T) getConstructor( implClass ).newInstance() ); - } - return implementations; - } - catch ( IOException e ) - { - throw new IllegalStateException( e.getLocalizedMessage(), e ); - } - catch ( ClassNotFoundException e ) - { - throw new IllegalStateException( e.getLocalizedMessage(), e ); - } - catch ( InvocationTargetException e ) - { - throw new IllegalStateException( e.getLocalizedMessage(), e ); - } - catch ( InstantiationException e ) - { - throw new IllegalStateException( e.getLocalizedMessage(), e ); - } - catch ( IllegalAccessException e ) - { - throw new IllegalStateException( e.getLocalizedMessage(), e ); - } - } - - @Nonnull - public Set<String> lookup( Class<?> clazz, ClassLoader classLoader ) - throws IOException - { - final String resourceName = "META-INF/services/" + clazz.getName(); - - if ( classLoader == null ) - { - return emptySet(); - } - final Enumeration<URL> urls = classLoader.getResources( resourceName ); - return lookupSpiImplementations( urls ); - } - - /** - * Method loadServices loads the services of a class that are - * defined using the SPI mechanism. - * - * @param urlEnumeration The urls from the resource - * @return The set of service provider names - * @throws IOException When reading the streams fails - */ - @Nonnull - @SuppressWarnings( "checkstyle:innerassignment" ) - private static Set<String> lookupSpiImplementations( final Enumeration<URL> urlEnumeration ) - throws IOException - { - final Set<String> names = new HashSet<String>(); - nextUrl: - while ( urlEnumeration.hasMoreElements() ) - { - final URL url = urlEnumeration.nextElement(); - final BufferedReader reader = getReader( url ); - try - { - for ( String line; ( line = reader.readLine() ) != null; ) - { - int ci = line.indexOf( '#' ); - if ( ci >= 0 ) - { - line = line.substring( 0, ci ); - } - line = line.trim(); - int n = line.length(); - if ( n == 0 ) - { - continue; // next line - } - - if ( line.indexOf( ' ' ) >= 0 || line.indexOf( '\t' ) >= 0 ) - { - continue nextUrl; // next url - } - char cp = line.charAt( 0 ); // should use codePointAt but this was JDK1.3 - if ( !isJavaIdentifierStart( cp ) ) - { - continue nextUrl; // next url - } - for ( int i = 1; i < n; i++ ) - { - cp = line.charAt( i ); // should use codePointAt but this was JDK1.3 - if ( !isJavaIdentifierPart( cp ) && cp != '.' ) - { - continue nextUrl; // next url - } - } - if ( !names.contains( line ) ) - { - names.add( line ); - } - } - } - finally - { - reader.close(); - } - } - - return names; - } - - @Nonnull - private static BufferedReader getReader( @Nonnull URL url ) - throws IOException - { - final InputStream inputStream = url.openStream(); - final InputStreamReader inputStreamReader = new InputStreamReader( inputStream ); - return new BufferedReader( inputStreamReader ); - } -} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/1a65d61d/maven-surefire-common/src/test/java/org/apache/maven/surefire/spi/SPITest.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/test/java/org/apache/maven/surefire/spi/SPITest.java b/maven-surefire-common/src/test/java/org/apache/maven/surefire/spi/SPITest.java index 2d8cc9f..2d00799 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/surefire/spi/SPITest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/surefire/spi/SPITest.java @@ -20,6 +20,7 @@ package org.apache.maven.surefire.spi; */ import org.apache.maven.plugin.surefire.booterclient.ProviderDetector; +import org.apache.maven.surefire.providerapi.ServiceLoader; import org.junit.Test; import java.io.IOException;