天天看點

ServiceLoader源碼學習

一.ServiceLoader介紹

  1. 一個簡單的加載服務提供者的設施。
  2. 系統分為兩個角色:應用程式,服務提供者jar。在應用程式中通過ServiceLoader加載所需服務。

二.使用

  1. jar裡面包含服務程式。
  2. jar META-INF檔案夾下建立services檔案夾,在services檔案裡建立檔案,檔案名為提供服務的接口的全限定名,檔案内容為實作接口的類的全限定名,多個實作類時以行分割。見圖。
  3. 應用程式調用服務:
    ServiceLoader<NameServiceDescriptor> serviceLoader= ServiceLoader.load(NameServiceDescriptor.class);
               
  4. ServiceLoader實作了Iterator接口,疊代 ServiceLoader即可得到每個NameServiceDescriptor。
ServiceLoader源碼學習

三.源碼

import java.net.URL;
import java.util.ServiceConfigurationError;

public final class ServiceLoader<S> implements Iterable<S> {

	private static final String PREFIX = "META-INF/services/";

	//一個類或者接口對于一個ServiceLoader
	private Class<S> service;
	//加載服務服務提供者的ClassLoader
	private ClassLoader loader;
	//緩存服務提供者,按服務提供者執行個體化順序
	private LinkedHashMap<String, S> providers = new LinkedHashMap<String, S>();
	//ServiceLoader的疊代委托給了lookupIterator
	private LazyIterator lookupIterator;

	private ServiceLoader(Class<S> svc, ClassLoader cl) {
		service = svc;
		loader = cl;
		reload();
	}

	public void reload() {
		providers.clear();
		lookupIterator = new LazyIterator(service, loader);
	}
	
	/**
	 * 傳回URL對應的檔案内容的Iterator,内容以行分割
	 * @param service 類名,傳進去隻為了異常說明更詳細
	 * @param u 檔案名對應的URL
	 */
	private Iterator<String> parse(Class service, URL u)
			throws ServiceConfigurationError {
		InputStream in = null;
		BufferedReader r = null;
		ArrayList<String> names = new ArrayList<String>();
		try {
			in = u.openStream();
			r = new BufferedReader(new InputStreamReader(in, "utf-8"));
			int lc = 1;
			while ((lc = parseLine(service, u, r, lc, names)) >= 0)
				;
		} catch (IOException x) {
			fail(service, "Error reading configuration file", x);
		} finally {
			try {
				if (r != null)
					r.close();
				if (in != null)
					in.close();
			} catch (IOException y) {
				fail(service, "Error closing configuration file", y);
			}
		}
		return names.iterator();
	}
	
	//從檔案裡面一行一行讀,放到names裡面
	private int parseLine(Class service, URL u, BufferedReader r, int lc,
			List<String> names) throws IOException, ServiceConfigurationError {
		String ln = r.readLine();
		if (ln == null) {
			return -1;
		}
		int ci = ln.indexOf('#');
		if (ci >= 0)
			ln = ln.substring(0, ci);
		ln = ln.trim();
		int n = ln.length();
		if (n != 0) {
			if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
				fail(service, u, lc, "Illegal configuration-file syntax");
			int cp = ln.codePointAt(0);
			if (!Character.isJavaIdentifierStart(cp))
				fail(service, u, lc, "Illegal provider-class name: " + ln);
			for (int i = Character.charCount(cp); i < n; i += Character
					.charCount(cp)) {
				cp = ln.codePointAt(i);
				if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
					fail(service, u, lc, "Illegal provider-class name: " + ln);
			}
			//如果該服務提供者未被執行個體化,并且names不包含
			if (!providers.containsKey(ln) && !names.contains(ln))
				names.add(ln);
		}
		return lc + 1;
	}
	
	 private static void fail(Class service, String msg, ...)throws ServiceConfigurationError{
		throw new ServiceConfigurationError(service.getName() + ": " + msg);
	 }

	
	private class LazyIterator implements Iterator<S> {
		Class<S> service;
		ClassLoader loader;
		Enumeration<URL> configs = null;//檔案對應路徑URL
		Iterator<String> pending = null;//檔案内容,一行一行存放
		String nextName = null;//目前調用hashNext方法時,就得到下一個檔案内容。

		private LazyIterator(Class<S> service, ClassLoader loader) {
			this.service = service;
			this.loader = loader;
		}

		public boolean hasNext() {
			if (nextName != null) {
				return true;
			}
			if (configs == null) {
				try {
					String fullName = PREFIX + service.getName();
					if (loader == null)
						configs = ClassLoader.getSystemResources(fullName);
					else
						configs = loader.getResources(fullName);
				} catch (IOException x) {
					fail(service, "Error locating configuration files", x);
				}
			}
			while ((pending == null) || !pending.hasNext()) {
				if (!configs.hasMoreElements()) {
					return false;
				pending = parse(service, configs.nextElement());
			}
			//儲存下一個檔案内容
			nextName = pending.next();
			return true;
		}

		public S next() {
			if (!hasNext()) {
				throw new NoSuchElementException();
			}
			String cn = nextName;
			nextName = null;
			try {
				//反射,關鍵所在
				S p = service.cast(Class.forName(cn, true, loader)
						.newInstance());
				//緩存已經執行個體化的服務提供者
				providers.put(cn, p);
				return p;
			} catch (ClassNotFoundException x) {
				fail(service, "Provider " + cn + " not found");
			} catch (Throwable x) {
				fail(service, "Provider " + cn + " could not be instantiated: "
						+ x, x);
			}
			throw new Error(); // This cannot happen
		}

		public void remove() {
			throw new UnsupportedOperationException();
		}

	}

	//以延遲方式加載服務提供者
	//首先疊代被緩存的服務提供者,然後以延遲方式加載和執行個體化所有剩餘的服務提供者,依次将每個服務提供者添加到緩存。
	public Iterator<S> iterator() {
		return new Iterator<S>() {
			//緩存起來的服務提供者
			Iterator<Map.Entry<String, S>> knownProviders = providers
					.entrySet().iterator();

			//先去緩存服務提供者裡面找
			public boolean hasNext() {
				if (knownProviders.hasNext())
					return true;
				return lookupIterator.hasNext();
			}

			public S next() {
				if (knownProviders.hasNext())
					return knownProviders.next().getValue();
				return lookupIterator.next();
			}

			public void remove() {
				throw new UnsupportedOperationException();
			}

		};
	}
	
	//執行個體化ServiceLoader
	public static <S> ServiceLoader<S> load(Class<S> service) {
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		return ServiceLoader.load(service, cl);
	}
	public static <S> ServiceLoader<S> load(Class<S> service, ClassLoader loader) {
		return new ServiceLoader<S>(service, loader);
	}
	
	//用目前類加載器的父加載器
	public static <S> ServiceLoader<S> loadInstalled(Class<S> service) {
		ClassLoader cl = ClassLoader.getSystemClassLoader();
		ClassLoader prev = null;
		while (cl != null) {
			prev = cl;
			cl = cl.getParent();
		}
		return ServiceLoader.load(service, prev);
	}

	public String toString() {
		return "java.util.ServiceLoader[" + service.getName() + "]";
	}

}
           

四.Lookup 

      NetBeans使用的東西。和ServiceLoader提供一樣的功能。具體參見http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/doc-files/lookup-api.html

五.為什麼要使用

  1.  為什麼不直接使用反射,得到服務提供者。因為ServiceLoader提供了緩存機制,因為Lookup提供了監聽機制。還有沒有其他原因?