【Java】ServiceLoader源码分析

ServiceLoader主要的功能是用来完成对SPI的provider的加载。

先看下它的成员:

 1 public final class ServiceLoader<S>
 2     implements Iterable<S> {
 3 
 4     private static final String PREFIX = "META-INF/services/";
 5 
 6     // The class or interface representing the service being loaded
 7     private final Class<S> service;
 8 
 9     // The class loader used to locate, load, and instantiate providers
10     private final ClassLoader loader;
11 
12     // The access control context taken when the ServiceLoader is created
13     private final AccessControlContext acc;
14 
15     // Cached providers, in instantiation order
16     private LinkedHashMap<String,S> providers = new LinkedHashMap<>();
17 
18     // The current lazy-lookup iterator
19     private LazyIterator lookupIterator;
20     
21     ......
22         
23 }

可以看到他首先是实现了Iterable接口,可以迭代。
PREFIX:指明了路径是在"META-INF/services/"下。
service:表示正在加载的服务的类或接口。
loader:使用的类加载器。
acc:创建ServiceLoader时获取的访问控制上下文。
providers :缓存的服务提供集合。
lookupIterator:是其内部使用的迭代器,用于类的懒加载,只有在迭代时加载。

其构造方法是一个private方法,不对外提供,在使用时我们需要调用其静态的load方法,由其自身产生ServiceLoader对象:

1 public static <S> ServiceLoader<S> load(Class<S> service) {
2         ClassLoader cl = Thread.currentThread().getContextClassLoader();
3         return ServiceLoader.load(service, cl);
4 }
5 
6 public static <S> ServiceLoader<S> load(Class<S> service,
7                                             ClassLoader loader) {
8         return new ServiceLoader<>(service, loader);
9 }

可以看到对load方法进行了重载,其中参数service是要加载的类;单参方法没有类加载器,使用的是当前线程的类加载器;最后调用的是双参的load方法;而双参的load方法也很简单,只是直接调用ServiceLoader的构造方法,实例化了一个对象。

 

1 private ServiceLoader(Class<S> svc, ClassLoader cl) {
2         service = Objects.requireNonNull(svc, "Service interface cannot be null");
3         loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl;
4         acc = (System.getSecurityManager() != null) ? AccessController.getContext() : null;
5         reload();
6 }

可以看到其构造方法逻辑依旧很简单,首先是判断传入的svc(即传入的service)是否为空,若是为空直接报异常,否则给service 成员赋值:

1 public static <T> T requireNonNull(T obj, String message) {
2         if (obj == null)
3             throw new NullPointerException(message);
4         return obj;
5 }

然后给进行cl的非空判断,给loader 成员赋值;接着给acc 成员赋值,其根据是否设置了安全管理器SecurityManager来赋值;最后调用reload方法。

1 public void reload() {
2         providers.clear();
3         lookupIterator = new LazyIterator(service, loader);
4 }

可以看到reload方法是一个public方法,那么在每次调用reload时就需要将之前加载的清空掉,所以直接使用providers这个map的clear方法清空掉缓存;接着使用刚才赋值后的service和loader产生一个LazyIterator对象赋值给lookupIterator成员。

LazyIterator是ServiceLoader的内部类,其定义如下:

 1 private class LazyIterator
 2         implements Iterator<S> {
 3     Class<S> service;
 4     ClassLoader loader;
 5     Enumeration<URL> configs = null;
 6     Iterator<String> pending = null;
 7     String nextName = null;
 8     
 9     private LazyIterator(Class<S> service, ClassLoader loader) {
10         this.service = service;
11         this.loader = loader;
12     }
13     ......
14 }

这里就可以看到ServiceLoader的实际加载过程就交给了LazyIterator来做,将ServiceLoader的service和loader成员分别赋值给了LazyIterator的service和loader成员。
configs是服务的URL枚举;
pending是保存要加载的服务的名称集合;
nextName是下一个要加载的服务名称;

ServiceLoader实现了Iterable接口,其实现的iterator方法如下:

 1 public Iterator<S> iterator() {
 2     return new Iterator<S>() {
 3         Iterator<Map.Entry<String,S>> knownProviders
 4             = providers.entrySet().iterator();
 5     
 6         public boolean hasNext() {
 7             if (knownProviders.hasNext())
 8                 return true;
 9             return lookupIterator.hasNext();
10         }
11     
12         public S next() {
13             if (knownProviders.hasNext())
14                 return knownProviders.next().getValue();
15             return lookupIterator.next();
16         }
17     
18         public void remove() {
19             throw new UnsupportedOperationException();
20         }
21     
22     };
23 }

可以看到它是直接创建了一个Iterator对象返回;其knownProviders成员直接获取providers的entrySet集合的迭代器;在hasNext和next方法中我们可以看到,它是先通过判断knownProviders里有没有(即providers),若没有再去lookupIterator中找;
前面我们可以看到providers里并没用put任何东西,那么就说明put操作也是在lookupIterator中完成的。

先看到lookupIterator的next方法:

 1 public S next() {
 2    if (acc == null) {
 3         return nextService();
 4     } else {
 5         PrivilegedAction<S> action = new PrivilegedAction<S>() {
 6             public S run() { return nextService(); }
 7         };
 8         return AccessController.doPrivileged(action, acc);
 9     }
10 }

首先根据判断acc是否为空,若为空则说明没有设置安全策略直接调用nextService方法,否则以特权方式调用nextService方法。

 

 1 private S nextService() {
 2     if (!hasNextService())
 3         throw new NoSuchElementException();
 4     String cn = nextName;
 5     nextName = null;
 6     Class<?> c = null;
 7     try {
 8         c = Class.forName(cn, false, loader);
 9     } catch (ClassNotFoundException x) {
10         fail(service,
11              "Provider " + cn + " not found");
12     }
13     if (!service.isAssignableFrom(c)) {
14         fail(service,
15              "Provider " + cn  + " not a subtype");
16     }
17     try {
18         S p = service.cast(c.newInstance());
19         providers.put(cn, p);
20         return p;
21     } catch (Throwable x) {
22         fail(service,
23              "Provider " + cn + " could not be instantiated",
24              x);
25     }
26     throw new Error();          // This cannot happen
27 }

首先根据hasNextService方法判断,若为false直接抛出NoSuchElementException异常,否则继续执行。

hasNextService方法:

 1 private boolean hasNextService() {
 2     if (nextName != null) {
 3         return true;
 4     }
 5     if (configs == null) {
 6         try {
 7             String fullName = PREFIX + service.getName();
 8             if (loader == null)
 9                 configs = ClassLoader.getSystemResources(fullName);
10             else
11                 configs = loader.getResources(fullName);
12         } catch (IOException x) {
13             fail(service, "Error locating configuration files", x);
14         }
15     }
16     while ((pending == null) || !pending.hasNext()) {
17         if (!configs.hasMoreElements()) {
18             return false;
19         }
20         pending = parse(service, configs.nextElement());
21     }
22     nextName = pending.next();
23     return true;
24 }

hasNextService方法首先根据nextName成员是否为空判断,若不为空,则说明已经初始化过了,直接返回true,否则继续执行。接着configs成员是否为空,configs 是一个URL的枚举,若是configs 没有初始化,就需要对configs初始化。
configs初始化逻辑也很简单,首先根据PREFIX前缀加上PREFIX的全名得到完整路径,再根据loader的有无,获取URL的枚举。其中fail方法时ServiceLoader的静态方法,用于异常的处理,后面给出。
在configs初始化完成后,还需要完成pending的初始化或者添加。
可以看到只有当pending为null,或者没有元素时才进行循环。循环时若是configs里没有元素,则直接返回false;否则调用ServiceLoader的parse方法,通过service和URL给pending赋值;

parse方法:

 1 private Iterator<String> parse(Class<?> service, URL u)
 2         throws ServiceConfigurationError {
 3     InputStream in = null;
 4     BufferedReader r = null;
 5     ArrayList<String> names = new ArrayList<>();
 6     try {
 7         in = u.openStream();
 8         r = new BufferedReader(new InputStreamReader(in, "utf-8"));
 9         int lc = 1;
10         while ((lc = parseLine(service, u, r, lc, names)) >= 0);
11     } catch (IOException x) {
12         fail(service, "Error reading configuration file", x);
13     } finally {
14         try {
15             if (r != null) r.close();
16             if (in != null) in.close();
17         } catch (IOException y) {
18             fail(service, "Error closing configuration file", y);
19         }
20     }
21     return names.iterator();
22 }

可以看到parse方法直接通过URL打开输入流,通过parseLine一行一行地读取将结果保存在names数组里。

parseLine方法:

 1 private int parseLine(Class<?> service, URL u, BufferedReader r, int lc,
 2                           List<String> names)
 3         throws IOException, ServiceConfigurationError {
 4     String ln = r.readLine();
 5     if (ln == null) {
 6         return -1;
 7     }
 8     int ci = ln.indexOf('#');
 9     if (ci >= 0) ln = ln.substring(0, ci);
10     ln = ln.trim();
11     int n = ln.length();
12     if (n != 0) {
13         if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
14             fail(service, u, lc, "Illegal configuration-file syntax");
15         int cp = ln.codePointAt(0);
16         if (!Character.isJavaIdentifierStart(cp))
17             fail(service, u, lc, "Illegal provider-class name: " + ln);
18         for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
19             cp = ln.codePointAt(i);
20             if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
21                 fail(service, u, lc, "Illegal provider-class name: " + ln);
22         }
23         if (!providers.containsKey(ln) && !names.contains(ln))
24             names.add(ln);
25     }
26     return lc + 1;
27 }

parseLine方法就是读该URL对应地文件地一行,可以看到通过对“#”的位置判断,忽略注释,并且剔除空格,接着是一系列的参数合法检验,然后判断providers和names里是否都没包含这个服务名称,若都没包含names直接add,最后返回下一行的行标;

当parse将所有内容读取完毕,返回names.iterator()赋值给hasNextService中的pending。循环结束,获取pending中的第一个元素赋值给nextName,返回true,hasNextService方法结束。

在nextService方法往下执行时,先用cn保存nextName的值,再让nextName=null,为下一次的遍历做准备;接着通过类加载,加载名为cn的类,再通过该类实例化对象,并用providers缓存起来,最后返回该实例对象。

其中cast方法是判断对象是否合法:

1 public T cast(Object obj) {
2     if (obj != null && !isInstance(obj))
3         throw new ClassCastException(cannotCastMsg(obj));
4     return (T) obj;
5 }

至此ServiceLoader的迭代器的next方法结束。其hasNext方法与其类似,就不详细分析了。

而其remove方法就更直接,直接抛出异常来避免可能出现的危险情况:

1 public void remove() {
2     throw new UnsupportedOperationException();
3 }

 

其中使用到的静态fail方法只是抛出异常:

 1 private static void fail(Class<?> service, String msg, Throwable cause)
 2         throws ServiceConfigurationError {
 3     throw new ServiceConfigurationError(service.getName() + ": " + msg,
 4                                             cause);
 5 }
 6 
 7 private static void fail(Class<?> service, String msg)
 8         throws ServiceConfigurationError {
 9     throw new ServiceConfigurationError(service.getName() + ": " + msg);
10 }
11 
12 private static void fail(Class<?> service, URL u, int line, String msg)
13         throws ServiceConfigurationError {
14     fail(service, u + ":" + line + ": " + msg);
15 }

 

ServiceLoader除了load的两个方法外还有个loadInstalled方法:

1 public static <S> ServiceLoader<S> loadInstalled(Class<S> service) {
2     ClassLoader cl = ClassLoader.getSystemClassLoader();
3     ClassLoader prev = null;
4     while (cl != null) {
5         prev = cl;
6         cl = cl.getParent();
7     }
8     return ServiceLoader.load(service, prev);
9 }

该方法与load方法不同在于loadInstalled使用的是扩展类加载器,而load使用的是传入进来的或者是线程的上下文类加载器,其他都一样。

 

ServiceLoader源码分析到此全部结束。

 

posted @ 2019-05-15 20:01  松饼人  阅读(936)  评论(0编辑  收藏  举报