private synchronized void loadResolvers() { if (resolvers == null) { resolvers = ServiceLoader.load(TemplateResolver.class); } }
private void loadService(String serviceClassName) { Class<? extends S> serviceClass = loadClass(serviceClassName); if (serviceClass == null) { return; } S serviceInstance = prepareInstance(serviceClass); if (serviceInstance == null) { return; } providers.add(serviceInstance); }
/** * Clear this loader's provider cache so that all providers will be reloaded. * <p/> * After invoking this method, subsequent invocations of the iterator method * will lazily look up and instantiate providers from scratch, just as is * done by a newly-created loader. * <p/> * This method is intended for use in situations in which new providers can * be installed into a running Java virtual machine. */ public void reload() { providers = new HashSet<S>(); for (URL serviceFile : loadServiceFiles()) { loadServiceFile(serviceFile); } }
private void loadServiceFile(URL serviceFile) { InputStream is = null; try { is = serviceFile.openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String serviceClassName = null; while ((serviceClassName = reader.readLine()) != null) { serviceClassName = trim(serviceClassName); if (serviceClassName.length() > 0) { loadService(serviceClassName); } } } catch (IOException e) { // FIXME: correct exception throw new RuntimeException("Could not read services file " + serviceFile); } finally { if (is != null) { try { is.close(); } catch (IOException e) { // FIXME: correct exception throw new RuntimeException("Could not close services file " + serviceFile, e); } } } }
/** * Creates a new service loader for the given service type and class loader. * * @param service The interface or abstract class representing the service * @param loader The class loader to be used to load provider-configuration * files and provider classes, or null if the system class loader * (or, failing that, the bootstrap class loader) is to be used * @return A new service loader */ public static <S> ServiceLoader<S> load(Class<S> service, ClassLoader loader) { if (loader == null) { loader = service.getClassLoader(); } return new ServiceLoader<S>(service, loader); }
/** * Creates a new service loader for the given service type, using the current * thread's context class loader. * <p/> * An invocation of this convenience method of the form * <p/> * {@code ServiceLoader.load(service)</code>} * <p/> * is equivalent to * <p/> * <code>ServiceLoader.load(service, * Thread.currentThread().getContextClassLoader())</code> * * @param service The interface or abstract class representing the service * @return A new service loader */ public static <S> ServiceLoader<S> load(Class<S> service) { return load(service, Thread.currentThread().getContextClassLoader()); }