【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源码分析到此全部结束。