@Test public void loadTwiceFromDifferentLoaders() throws Exception { URL jarUrl = urlForJarWithEntries( CharSequence.class.getName(), String.class.getName(), StringBuilder.class.getName()); ClassLoader loader1 = new URLClassLoader(new URL[] {jarUrl}); ImmutableList<CharSequence> providers1 = SimpleServiceLoader.load(CharSequence.class, loader1); assertThat(providers1).contains(""); ClassLoader loader2 = new URLClassLoader(new URL[] {jarUrl}); ImmutableList<CharSequence> providers2 = SimpleServiceLoader.load(CharSequence.class, loader2); List<Class<?>> classes1 = providers1.stream().map(Object::getClass).collect(toList()); List<Class<?>> classes2 = providers2.stream().map(Object::getClass).collect(toList()); assertThat(classes2).containsExactlyElementsIn(classes1).inOrder(); }
public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader) { String resourceName = "META-INF/services/" + service.getName(); List<URL> resourceUrls; try { resourceUrls = Collections.list(loader.getResources(resourceName)); } catch (IOException e) { throw new ServiceConfigurationError("Could not look up " + resourceName, e); } ImmutableList.Builder<T> providers = ImmutableList.builder(); for (URL resourceUrl : resourceUrls) { try { providers.addAll(providersFromUrl(resourceUrl, service, loader)); } catch (IOException e) { throw new ServiceConfigurationError("Could not read " + resourceUrl, e); } } return providers.build(); }
SimpleServiceLoader { public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader) { String resourceName = "META-INF/services/" + service.getName(); List<URL> resourceUrls; try { resourceUrls = Collections.list(loader.getResources(resourceName)); } catch (IOException e) { throw new ServiceConfigurationError("Could not look up " + resourceName, e); } ImmutableList.Builder<T> providers = ImmutableList.builder(); for (URL resourceUrl : resourceUrls) { try { providers.addAll(providersFromUrl(resourceUrl, service, loader)); } catch (IOException e) { throw new ServiceConfigurationError("Could not read " + resourceUrl, e); } } return providers.build(); } }
SimpleServiceLoader { public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader) { String resourceName = "META-INF/services/" + service.getName(); List<URL> resourceUrls; try { resourceUrls = Collections.list(loader.getResources(resourceName)); } catch (IOException e) { throw new ServiceConfigurationError("Could not look up " + resourceName, e); } ImmutableList.Builder<T> providers = ImmutableList.builder(); for (URL resourceUrl : resourceUrls) { try { providers.addAll(providersFromUrl(resourceUrl, service, loader)); } catch (IOException e) { throw new ServiceConfigurationError("Could not read " + resourceUrl, e); } } return providers.build(); } private  SimpleServiceLoader(); }
SimpleServiceLoader { public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader) { String resourceName = "META-INF/services/" + service.getName(); List<URL> resourceUrls; try { resourceUrls = Collections.list(loader.getResources(resourceName)); } catch (IOException e) { throw new ServiceConfigurationError("Could not look up " + resourceName, e); } ImmutableList.Builder<T> providers = ImmutableList.builder(); for (URL resourceUrl : resourceUrls) { try { providers.addAll(providersFromUrl(resourceUrl, service, loader)); } catch (IOException e) { throw new ServiceConfigurationError("Could not read " + resourceUrl, e); } } return providers.build(); } private  SimpleServiceLoader(); static ImmutableList<T> load(Class<? extends T> service, ClassLoader loader); }
SimpleServiceLoader { public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader) { String resourceName = "META-INF/services/" + service.getName(); List<URL> resourceUrls; try { resourceUrls = Collections.list(loader.getResources(resourceName)); } catch (IOException e) { throw new ServiceConfigurationError("Could not look up " + resourceName, e); } ImmutableList.Builder<T> providers = ImmutableList.builder(); for (URL resourceUrl : resourceUrls) { try { providers.addAll(providersFromUrl(resourceUrl, service, loader)); } catch (IOException e) { throw new ServiceConfigurationError("Could not read " + resourceUrl, e); } } return providers.build(); } private  SimpleServiceLoader(); static ImmutableList<T> load(Class<? extends T> service, ClassLoader loader);  }