9
9
import java .util .Iterator ;
10
10
import java .util .Map ;
11
11
import java .util .ServiceLoader ;
12
+ import java .util .function .Supplier ;
12
13
import java .util .stream .Collectors ;
13
14
import java .util .stream .Stream ;
14
15
16
+ import static java .lang .Thread .currentThread ;
15
17
import static java .util .Objects .requireNonNull ;
16
18
17
19
public final class ObjectFactoryServiceLoader {
18
20
21
+ private final Supplier <ClassLoader > classLoaderSupplier ;
19
22
private final Options options ;
20
23
24
+ @ Deprecated
21
25
public ObjectFactoryServiceLoader (Options options ) {
26
+ this (currentThread ()::getContextClassLoader , options );
27
+ }
28
+
29
+ public ObjectFactoryServiceLoader (Supplier <ClassLoader > classLoaderSupplier , Options options ) {
30
+ this .classLoaderSupplier = requireNonNull (classLoaderSupplier );
22
31
this .options = requireNonNull (options );
23
32
}
24
33
@@ -38,9 +47,9 @@ public ObjectFactoryServiceLoader(Options options) {
38
47
* @return an instance of {@link ObjectFactory}
39
48
*/
40
49
ObjectFactory loadObjectFactory () {
41
- Class <? extends ObjectFactory > objectFactoryClass = this . options .getObjectFactoryClass ();
42
-
43
- final ServiceLoader <ObjectFactory > loader = ServiceLoader .load (ObjectFactory .class );
50
+ Class <? extends ObjectFactory > objectFactoryClass = options .getObjectFactoryClass ();
51
+ ClassLoader classLoader = classLoaderSupplier . get ();
52
+ ServiceLoader <ObjectFactory > loader = ServiceLoader .load (ObjectFactory .class , classLoader );
44
53
if (objectFactoryClass == null ) {
45
54
return loadSingleObjectFactoryOrDefault (loader );
46
55
@@ -50,7 +59,7 @@ ObjectFactory loadObjectFactory() {
50
59
}
51
60
52
61
private static ObjectFactory loadSingleObjectFactoryOrDefault (ServiceLoader <ObjectFactory > loader ) {
53
- final Iterator <ObjectFactory > objectFactories = loader .iterator ();
62
+ Iterator <ObjectFactory > objectFactories = loader .iterator ();
54
63
55
64
ObjectFactory objectFactory ;
56
65
if (objectFactories .hasNext ()) {
0 commit comments