Skip to content

Commit

Permalink
j.m.u.FactoryFinder.factoryFromServiceLoader needs PrivilegedAction j…
Browse files Browse the repository at this point in the history
…akartaee#621

Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Oct 4, 2022
1 parent c424303 commit 90d942d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions api/src/main/java/jakarta/mail/util/FactoryFinder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 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 @@ -126,12 +126,17 @@ private static <T> T lookupUsingOSGiServiceLoader(String factoryId) {
private static <T> T factoryFromServiceLoader(Class<T> factory) {
try {
ServiceLoader<T> sl = ServiceLoader.load(factory);
Iterator<T> iter = sl.iterator();
if (iter.hasNext()) {
return iter.next();
} else {
return null;
}
return AccessController.doPrivileged(new PrivilegedAction<T>() {
@Override
public T run() {
Iterator<T> iter = sl.iterator();
if (iter.hasNext()) {
return iter.next();
} else {
return null;
}
}
});
} catch (Throwable t) {
// For example, ServiceConfigurationError can be thrown if the factory class is not declared with 'uses' in module-info
throw new IllegalStateException("Cannot load " + factory + " as ServiceLoader", t);
Expand Down

0 comments on commit 90d942d

Please sign in to comment.