Skip to content

Commit

Permalink
Replacing custom loop by ServiceLoader.findFirst()
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarg committed Jan 10, 2025
1 parent e50c16d commit ebced83
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -174,10 +174,7 @@ private static ClassLoader getClassLoader() {
private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
final PrivilegedAction<T> action = () -> {
try {
final ServiceLoader<T> loader = ServiceLoader.load(service, cl);
if (loader.iterator().hasNext()) {
return loader.iterator().next();
}
return ServiceLoader.load(service, cl).findFirst().orElse(null);
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
Expand Down
7 changes: 2 additions & 5 deletions jaxrs-api/src/main/java/jakarta/ws/rs/ext/FactoryFinder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -174,10 +174,7 @@ private static ClassLoader getClassLoader() {
private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
final PrivilegedAction<T> action = () -> {
try {
final ServiceLoader<T> loader = ServiceLoader.load(service, cl);
if (loader.iterator().hasNext()) {
return loader.iterator().next();
}
return ServiceLoader.load(service, cl).findFirst().orElse(null);
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
Expand Down
7 changes: 2 additions & 5 deletions jaxrs-api/src/main/java/jakarta/ws/rs/sse/FactoryFinder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -174,10 +174,7 @@ private static ClassLoader getClassLoader() {
private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
final PrivilegedAction<T> action = () -> {
try {
final ServiceLoader<T> loader = ServiceLoader.load(service, cl);
if (loader.iterator().hasNext()) {
return loader.iterator().next();
}
return ServiceLoader.load(service, cl).findFirst().orElse(null);
} catch (Exception e) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
}
Expand Down

0 comments on commit ebced83

Please sign in to comment.