src/share/jaxws_classes/com/sun/xml/internal/ws/util/ServiceFinder.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.ws.util;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.NotNull;
aoqi@0 29 import com.sun.istack.internal.Nullable;
aoqi@0 30 import com.sun.xml.internal.ws.api.Component;
aoqi@0 31 import com.sun.xml.internal.ws.api.ComponentEx;
aoqi@0 32 import com.sun.xml.internal.ws.api.server.ContainerResolver;
aoqi@0 33
aoqi@0 34 import java.io.BufferedReader;
aoqi@0 35 import java.io.IOException;
aoqi@0 36 import java.io.InputStream;
aoqi@0 37 import java.io.InputStreamReader;
aoqi@0 38 import java.lang.reflect.Array;
aoqi@0 39 import java.net.URL;
aoqi@0 40 import java.util.ArrayList;
aoqi@0 41 import java.util.Arrays;
aoqi@0 42 import java.util.Collection;
aoqi@0 43 import java.util.Collections;
aoqi@0 44 import java.util.Enumeration;
aoqi@0 45 import java.util.Iterator;
aoqi@0 46 import java.util.List;
aoqi@0 47 import java.util.NoSuchElementException;
aoqi@0 48 import java.util.Set;
aoqi@0 49 import java.util.TreeSet;
aoqi@0 50 import java.util.WeakHashMap;
aoqi@0 51 import java.util.concurrent.ConcurrentHashMap;
aoqi@0 52
aoqi@0 53
aoqi@0 54 /**
aoqi@0 55 * A simple service-provider lookup mechanism. A <i>service</i> is a
aoqi@0 56 * well-known set of interfaces and (usually abstract) classes. A <i>service
aoqi@0 57 * provider</i> is a specific implementation of a service. The classes in a
aoqi@0 58 * provider typically implement the interfaces and subclass the classes defined
aoqi@0 59 * in the service itself. Service providers may be installed in an
aoqi@0 60 * implementation of the Java platform in the form of extensions, that is, jar
aoqi@0 61 * files placed into any of the usual extension directories. Providers may
aoqi@0 62 * also be made available by adding them to the applet or application class
aoqi@0 63 * path or by some other platform-specific means.
aoqi@0 64 * <p/>
aoqi@0 65 * <p> In this lookup mechanism a service is represented by an interface or an
aoqi@0 66 * abstract class. (A concrete class may be used, but this is not
aoqi@0 67 * recommended.) A provider of a given service contains one or more concrete
aoqi@0 68 * classes that extend this <i>service class</i> with data and code specific to
aoqi@0 69 * the provider. This <i>provider class</i> will typically not be the entire
aoqi@0 70 * provider itself but rather a proxy that contains enough information to
aoqi@0 71 * decide whether the provider is able to satisfy a particular request together
aoqi@0 72 * with code that can create the actual provider on demand. The details of
aoqi@0 73 * provider classes tend to be highly service-specific; no single class or
aoqi@0 74 * interface could possibly unify them, so no such class has been defined. The
aoqi@0 75 * only requirement enforced here is that provider classes must have a
aoqi@0 76 * zero-argument constructor so that they may be instantiated during lookup.
aoqi@0 77 * <p/>
aoqi@0 78 * <p> A service provider identifies itself by placing a provider-configuration
aoqi@0 79 * file in the resource directory <tt>META-INF/services</tt>. The file's name
aoqi@0 80 * should consist of the fully-qualified name of the abstract service class.
aoqi@0 81 * The file should contain a list of fully-qualified concrete provider-class
aoqi@0 82 * names, one per line. Space and tab characters surrounding each name, as
aoqi@0 83 * well as blank lines, are ignored. The comment character is <tt>'#'</tt>
aoqi@0 84 * (<tt>0x23</tt>); on each line all characters following the first comment
aoqi@0 85 * character are ignored. The file must be encoded in UTF-8.
aoqi@0 86 * <p/>
aoqi@0 87 * <p> If a particular concrete provider class is named in more than one
aoqi@0 88 * configuration file, or is named in the same configuration file more than
aoqi@0 89 * once, then the duplicates will be ignored. The configuration file naming a
aoqi@0 90 * particular provider need not be in the same jar file or other distribution
aoqi@0 91 * unit as the provider itself. The provider must be accessible from the same
aoqi@0 92 * class loader that was initially queried to locate the configuration file;
aoqi@0 93 * note that this is not necessarily the class loader that found the file.
aoqi@0 94 * <p/>
aoqi@0 95 * <p> <b>Example:</b> Suppose we have a service class named
aoqi@0 96 * <tt>java.io.spi.CharCodec</tt>. It has two abstract methods:
aoqi@0 97 * <p/>
aoqi@0 98 * <pre>
aoqi@0 99 * public abstract CharEncoder getEncoder(String encodingName);
aoqi@0 100 * public abstract CharDecoder getDecoder(String encodingName);
aoqi@0 101 * </pre>
aoqi@0 102 * <p/>
aoqi@0 103 * Each method returns an appropriate object or <tt>null</tt> if it cannot
aoqi@0 104 * translate the given encoding. Typical <tt>CharCodec</tt> providers will
aoqi@0 105 * support more than one encoding.
aoqi@0 106 * <p/>
aoqi@0 107 * <p> If <tt>sun.io.StandardCodec</tt> is a provider of the <tt>CharCodec</tt>
aoqi@0 108 * service then its jar file would contain the file
aoqi@0 109 * <tt>META-INF/services/java.io.spi.CharCodec</tt>. This file would contain
aoqi@0 110 * the single line:
aoqi@0 111 * <p/>
aoqi@0 112 * <pre>
aoqi@0 113 * sun.io.StandardCodec # Standard codecs for the platform
aoqi@0 114 * </pre>
aoqi@0 115 * <p/>
aoqi@0 116 * To locate an codec for a given encoding name, the internal I/O code would
aoqi@0 117 * do something like this:
aoqi@0 118 * <p/>
aoqi@0 119 * <pre>
aoqi@0 120 * CharEncoder getEncoder(String encodingName) {
aoqi@0 121 * for( CharCodec cc : ServiceFinder.find(CharCodec.class) ) {
aoqi@0 122 * CharEncoder ce = cc.getEncoder(encodingName);
aoqi@0 123 * if (ce != null)
aoqi@0 124 * return ce;
aoqi@0 125 * }
aoqi@0 126 * return null;
aoqi@0 127 * }
aoqi@0 128 * </pre>
aoqi@0 129 * <p/>
aoqi@0 130 * The provider-lookup mechanism always executes in the security context of the
aoqi@0 131 * caller. Trusted system code should typically invoke the methods in this
aoqi@0 132 * class from within a privileged security context.
aoqi@0 133 *
aoqi@0 134 * @author Mark Reinhold
aoqi@0 135 * @version 1.11, 03/12/19
aoqi@0 136 * @since 1.3
aoqi@0 137 */
aoqi@0 138 public final class ServiceFinder<T> implements Iterable<T> {
aoqi@0 139
aoqi@0 140 private static final String prefix = "META-INF/services/";
aoqi@0 141
aoqi@0 142 private static WeakHashMap<ClassLoader, ConcurrentHashMap<String, ServiceName[]>> serviceNameCache
aoqi@0 143 = new WeakHashMap<ClassLoader, ConcurrentHashMap<String, ServiceName[]>>();
aoqi@0 144
aoqi@0 145 private final Class<T> serviceClass;
aoqi@0 146 private final @Nullable ClassLoader classLoader;
aoqi@0 147 private final @Nullable ComponentEx component;
aoqi@0 148
aoqi@0 149 private static class ServiceName {
aoqi@0 150 final String className;
aoqi@0 151 final URL config;
aoqi@0 152 public ServiceName(String className, URL config) {
aoqi@0 153 this.className = className;
aoqi@0 154 this.config = config;
aoqi@0 155 }
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 public static <T> ServiceFinder<T> find(@NotNull Class<T> service, @Nullable ClassLoader loader, Component component) {
aoqi@0 159 return new ServiceFinder<T>(service, loader, component);
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 public static <T> ServiceFinder<T> find(@NotNull Class<T> service, Component component) {
aoqi@0 163 return find(service,Thread.currentThread().getContextClassLoader(),component);
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 /**
aoqi@0 167 * Locates and incrementally instantiates the available providers of a
aoqi@0 168 * given service using the given class loader.
aoqi@0 169 * <p/>
aoqi@0 170 * <p> This method transforms the name of the given service class into a
aoqi@0 171 * provider-configuration filename as described above and then uses the
aoqi@0 172 * <tt>getResources</tt> method of the given class loader to find all
aoqi@0 173 * available files with that name. These files are then read and parsed to
aoqi@0 174 * produce a list of provider-class names. The iterator that is returned
aoqi@0 175 * uses the given class loader to lookup and then instantiate each element
aoqi@0 176 * of the list.
aoqi@0 177 * <p/>
aoqi@0 178 * <p> Because it is possible for extensions to be installed into a running
aoqi@0 179 * Java virtual machine, this method may return different results each time
aoqi@0 180 * it is invoked. <p>
aoqi@0 181 *
aoqi@0 182 * @param service The service's abstract service class
aoqi@0 183 * @param loader The class loader to be used to load provider-configuration files
aoqi@0 184 * and instantiate provider classes, or <tt>null</tt> if the system
aoqi@0 185 * class loader (or, failing that the bootstrap class loader) is to
aoqi@0 186 * be used
aoqi@0 187 * @throws ServiceConfigurationError If a provider-configuration file violates the specified format
aoqi@0 188 * or names a provider class that cannot be found and instantiated
aoqi@0 189 * @see #find(Class)
aoqi@0 190 */
aoqi@0 191 public static <T> ServiceFinder<T> find(@NotNull Class<T> service, @Nullable ClassLoader loader) {
aoqi@0 192 return find(service, loader, ContainerResolver.getInstance().getContainer());
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 /**
aoqi@0 196 * Locates and incrementally instantiates the available providers of a
aoqi@0 197 * given service using the context class loader. This convenience method
aoqi@0 198 * is equivalent to
aoqi@0 199 * <p/>
aoqi@0 200 * <pre>
aoqi@0 201 * ClassLoader cl = Thread.currentThread().getContextClassLoader();
aoqi@0 202 * return Service.providers(service, cl);
aoqi@0 203 * </pre>
aoqi@0 204 *
aoqi@0 205 * @param service The service's abstract service class
aoqi@0 206 *
aoqi@0 207 * @throws ServiceConfigurationError If a provider-configuration file violates the specified format
aoqi@0 208 * or names a provider class that cannot be found and instantiated
aoqi@0 209 * @see #find(Class, ClassLoader)
aoqi@0 210 */
aoqi@0 211 public static <T> ServiceFinder<T> find(Class<T> service) {
aoqi@0 212 return find(service,Thread.currentThread().getContextClassLoader());
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 private ServiceFinder(Class<T> service, ClassLoader loader, Component component) {
aoqi@0 216 this.serviceClass = service;
aoqi@0 217 this.classLoader = loader;
aoqi@0 218 this.component = getComponentEx(component);
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 private static ServiceName[] serviceClassNames(Class serviceClass, ClassLoader classLoader) {
aoqi@0 222 ArrayList<ServiceName> l = new ArrayList<ServiceName>();
aoqi@0 223 for (Iterator<ServiceName> it = new ServiceNameIterator(serviceClass,classLoader);it.hasNext();) l.add(it.next());
aoqi@0 224 return l.toArray(new ServiceName[l.size()]);
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 /**
aoqi@0 228 * Returns discovered objects incrementally.
aoqi@0 229 *
aoqi@0 230 * @return An <tt>Iterator</tt> that yields provider objects for the given
aoqi@0 231 * service, in some arbitrary order. The iterator will throw a
aoqi@0 232 * <tt>ServiceConfigurationError</tt> if a provider-configuration
aoqi@0 233 * file violates the specified format or if a provider class cannot
aoqi@0 234 * be found and instantiated.
aoqi@0 235 */
aoqi@0 236 @SuppressWarnings("unchecked")
aoqi@0 237 public Iterator<T> iterator() {
aoqi@0 238 Iterator<T> it = new LazyIterator<T>(serviceClass,classLoader);
aoqi@0 239 return component != null ?
aoqi@0 240 new CompositeIterator<T>(
aoqi@0 241 component.getIterableSPI(serviceClass).iterator(),it) :
aoqi@0 242 it;
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 /**
aoqi@0 246 * Returns discovered objects all at once.
aoqi@0 247 *
aoqi@0 248 * @return
aoqi@0 249 * can be empty but never null.
aoqi@0 250 *
aoqi@0 251 * @throws ServiceConfigurationError
aoqi@0 252 */
aoqi@0 253 public T[] toArray() {
aoqi@0 254 List<T> result = new ArrayList<T>();
aoqi@0 255 for (T t : this) {
aoqi@0 256 result.add(t);
aoqi@0 257 }
aoqi@0 258 return result.toArray((T[])Array.newInstance(serviceClass,result.size()));
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 private static void fail(Class service, String msg, Throwable cause)
aoqi@0 262 throws ServiceConfigurationError {
aoqi@0 263 ServiceConfigurationError sce
aoqi@0 264 = new ServiceConfigurationError(service.getName() + ": " + msg);
aoqi@0 265 sce.initCause(cause);
aoqi@0 266 throw sce;
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 private static void fail(Class service, String msg)
aoqi@0 270 throws ServiceConfigurationError {
aoqi@0 271 throw new ServiceConfigurationError(service.getName() + ": " + msg);
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 private static void fail(Class service, URL u, int line, String msg)
aoqi@0 275 throws ServiceConfigurationError {
aoqi@0 276 fail(service, u + ":" + line + ": " + msg);
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 /**
aoqi@0 280 * Parse a single line from the given configuration file, adding the name
aoqi@0 281 * on the line to both the names list and the returned set iff the name is
aoqi@0 282 * not already a member of the returned set.
aoqi@0 283 */
aoqi@0 284 private static int parseLine(Class service, URL u, BufferedReader r, int lc,
aoqi@0 285 List<String> names, Set<String> returned)
aoqi@0 286 throws IOException, ServiceConfigurationError {
aoqi@0 287 String ln = r.readLine();
aoqi@0 288 if (ln == null) {
aoqi@0 289 return -1;
aoqi@0 290 }
aoqi@0 291 int ci = ln.indexOf('#');
aoqi@0 292 if (ci >= 0) ln = ln.substring(0, ci);
aoqi@0 293 ln = ln.trim();
aoqi@0 294 int n = ln.length();
aoqi@0 295 if (n != 0) {
aoqi@0 296 if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
aoqi@0 297 fail(service, u, lc, "Illegal configuration-file syntax");
aoqi@0 298 int cp = ln.codePointAt(0);
aoqi@0 299 if (!Character.isJavaIdentifierStart(cp))
aoqi@0 300 fail(service, u, lc, "Illegal provider-class name: " + ln);
aoqi@0 301 for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
aoqi@0 302 cp = ln.codePointAt(i);
aoqi@0 303 if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
aoqi@0 304 fail(service, u, lc, "Illegal provider-class name: " + ln);
aoqi@0 305 }
aoqi@0 306 if (!returned.contains(ln)) {
aoqi@0 307 names.add(ln);
aoqi@0 308 returned.add(ln);
aoqi@0 309 }
aoqi@0 310 }
aoqi@0 311 return lc + 1;
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 /**
aoqi@0 315 * Parse the content of the given URL as a provider-configuration file.
aoqi@0 316 *
aoqi@0 317 * @param service The service class for which providers are being sought;
aoqi@0 318 * used to construct error detail strings
aoqi@0 319 * @param u The URL naming the configuration file to be parsed
aoqi@0 320 * @param returned A Set containing the names of provider classes that have already
aoqi@0 321 * been returned. This set will be updated to contain the names
aoqi@0 322 * that will be yielded from the returned <tt>Iterator</tt>.
aoqi@0 323 * @return A (possibly empty) <tt>Iterator</tt> that will yield the
aoqi@0 324 * provider-class names in the given configuration file that are
aoqi@0 325 * not yet members of the returned set
aoqi@0 326 * @throws ServiceConfigurationError If an I/O error occurs while reading from the given URL, or
aoqi@0 327 * if a configuration-file format error is detected
aoqi@0 328 */
aoqi@0 329 @SuppressWarnings({"StatementWithEmptyBody"})
aoqi@0 330 private static Iterator<String> parse(Class service, URL u, Set<String> returned)
aoqi@0 331 throws ServiceConfigurationError {
aoqi@0 332 InputStream in = null;
aoqi@0 333 BufferedReader r = null;
aoqi@0 334 ArrayList<String> names = new ArrayList<String>();
aoqi@0 335 try {
aoqi@0 336 in = u.openStream();
aoqi@0 337 r = new BufferedReader(new InputStreamReader(in, "utf-8"));
aoqi@0 338 int lc = 1;
aoqi@0 339 while ((lc = parseLine(service, u, r, lc, names, returned)) >= 0) ;
aoqi@0 340 } catch (IOException x) {
aoqi@0 341 fail(service, ": " + x);
aoqi@0 342 } finally {
aoqi@0 343 try {
aoqi@0 344 if (r != null) r.close();
aoqi@0 345 if (in != null) in.close();
aoqi@0 346 } catch (IOException y) {
aoqi@0 347 fail(service, ": " + y);
aoqi@0 348 }
aoqi@0 349 }
aoqi@0 350 return names.iterator();
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 private static ComponentEx getComponentEx(Component component) {
aoqi@0 354 if (component instanceof ComponentEx)
aoqi@0 355 return (ComponentEx) component;
aoqi@0 356
aoqi@0 357 return component != null ? new ComponentExWrapper(component) : null;
aoqi@0 358 }
aoqi@0 359
aoqi@0 360 private static class ComponentExWrapper implements ComponentEx {
aoqi@0 361 private final Component component;
aoqi@0 362
aoqi@0 363 public ComponentExWrapper(Component component) {
aoqi@0 364 this.component = component;
aoqi@0 365 }
aoqi@0 366
aoqi@0 367 public <S> S getSPI(Class<S> spiType) {
aoqi@0 368 return component.getSPI(spiType);
aoqi@0 369 }
aoqi@0 370
aoqi@0 371 public <S> Iterable<S> getIterableSPI(Class<S> spiType) {
aoqi@0 372 S item = getSPI(spiType);
aoqi@0 373 if (item != null) {
aoqi@0 374 Collection<S> c = Collections.singletonList(item);
aoqi@0 375 return c;
aoqi@0 376 }
aoqi@0 377 return Collections.emptySet();
aoqi@0 378 }
aoqi@0 379 }
aoqi@0 380
aoqi@0 381 private static class CompositeIterator<T> implements Iterator<T> {
aoqi@0 382 private final Iterator<Iterator<T>> it;
aoqi@0 383 private Iterator<T> current = null;
aoqi@0 384
aoqi@0 385 public CompositeIterator(Iterator<T>... iterators) {
aoqi@0 386 it = Arrays.asList(iterators).iterator();
aoqi@0 387 }
aoqi@0 388
aoqi@0 389 public boolean hasNext() {
aoqi@0 390 if (current != null && current.hasNext())
aoqi@0 391 return true;
aoqi@0 392
aoqi@0 393 while (it.hasNext()) {
aoqi@0 394 current = it.next();
aoqi@0 395 if (current.hasNext())
aoqi@0 396 return true;
aoqi@0 397
aoqi@0 398 }
aoqi@0 399
aoqi@0 400 return false;
aoqi@0 401 }
aoqi@0 402
aoqi@0 403 public T next() {
aoqi@0 404 if (!hasNext())
aoqi@0 405 throw new NoSuchElementException();
aoqi@0 406
aoqi@0 407 return current.next();
aoqi@0 408 }
aoqi@0 409
aoqi@0 410 public void remove() {
aoqi@0 411 throw new UnsupportedOperationException();
aoqi@0 412 }
aoqi@0 413 }
aoqi@0 414
aoqi@0 415 /**
aoqi@0 416 * Private inner class implementing fully-lazy provider lookup
aoqi@0 417 */
aoqi@0 418 private static class ServiceNameIterator implements Iterator<ServiceName> {
aoqi@0 419 Class service;
aoqi@0 420 @Nullable ClassLoader loader;
aoqi@0 421 Enumeration<URL> configs = null;
aoqi@0 422 Iterator<String> pending = null;
aoqi@0 423 Set<String> returned = new TreeSet<String>();
aoqi@0 424 String nextName = null;
aoqi@0 425 URL currentConfig = null;
aoqi@0 426
aoqi@0 427 private ServiceNameIterator(Class service, ClassLoader loader) {
aoqi@0 428 this.service = service;
aoqi@0 429 this.loader = loader;
aoqi@0 430 }
aoqi@0 431
aoqi@0 432 public boolean hasNext() throws ServiceConfigurationError {
aoqi@0 433 if (nextName != null) {
aoqi@0 434 return true;
aoqi@0 435 }
aoqi@0 436 if (configs == null) {
aoqi@0 437 try {
aoqi@0 438 String fullName = prefix + service.getName();
aoqi@0 439 if (loader == null)
aoqi@0 440 configs = ClassLoader.getSystemResources(fullName);
aoqi@0 441 else
aoqi@0 442 configs = loader.getResources(fullName);
aoqi@0 443 } catch (IOException x) {
aoqi@0 444 fail(service, ": " + x);
aoqi@0 445 }
aoqi@0 446 }
aoqi@0 447 while ((pending == null) || !pending.hasNext()) {
aoqi@0 448 if (!configs.hasMoreElements()) {
aoqi@0 449 return false;
aoqi@0 450 }
aoqi@0 451 currentConfig = configs.nextElement();
aoqi@0 452 pending = parse(service, currentConfig, returned);
aoqi@0 453 }
aoqi@0 454 nextName = pending.next();
aoqi@0 455 return true;
aoqi@0 456 }
aoqi@0 457
aoqi@0 458 public ServiceName next() throws ServiceConfigurationError {
aoqi@0 459 if (!hasNext()) {
aoqi@0 460 throw new NoSuchElementException();
aoqi@0 461 }
aoqi@0 462 String cn = nextName;
aoqi@0 463 nextName = null;
aoqi@0 464 return new ServiceName(cn, currentConfig);
aoqi@0 465 }
aoqi@0 466
aoqi@0 467 public void remove() {
aoqi@0 468 throw new UnsupportedOperationException();
aoqi@0 469 }
aoqi@0 470 }
aoqi@0 471
aoqi@0 472 private static class LazyIterator<T> implements Iterator<T> {
aoqi@0 473 Class<T> service;
aoqi@0 474 @Nullable ClassLoader loader;
aoqi@0 475 ServiceName[] names;
aoqi@0 476 int index;
aoqi@0 477
aoqi@0 478 private LazyIterator(Class<T> service, ClassLoader loader) {
aoqi@0 479 this.service = service;
aoqi@0 480 this.loader = loader;
aoqi@0 481 this.names = null;
aoqi@0 482 index = 0;
aoqi@0 483 }
aoqi@0 484
aoqi@0 485 @Override
aoqi@0 486 public boolean hasNext() {
aoqi@0 487 if (names == null) {
aoqi@0 488 ConcurrentHashMap<String, ServiceName[]> nameMap = null;
aoqi@0 489 synchronized(serviceNameCache){ nameMap = serviceNameCache.get(loader); }
aoqi@0 490 names = (nameMap != null)? nameMap.get(service.getName()) : null;
aoqi@0 491 if (names == null) {
aoqi@0 492 names = serviceClassNames(service, loader);
aoqi@0 493 if (nameMap == null) nameMap = new ConcurrentHashMap<String, ServiceName[]>();
aoqi@0 494 nameMap.put(service.getName(), names);
aoqi@0 495 synchronized(serviceNameCache){ serviceNameCache.put(loader,nameMap); }
aoqi@0 496 }
aoqi@0 497 }
aoqi@0 498 return (index < names.length);
aoqi@0 499 }
aoqi@0 500
aoqi@0 501 @Override
aoqi@0 502 public T next() {
aoqi@0 503 if (!hasNext()) throw new NoSuchElementException();
aoqi@0 504 ServiceName sn = names[index++];
aoqi@0 505 String cn = sn.className;
aoqi@0 506 URL currentConfig = sn.config;
aoqi@0 507 try {
aoqi@0 508 return service.cast(Class.forName(cn, true, loader).newInstance());
aoqi@0 509 } catch (ClassNotFoundException x) {
aoqi@0 510 fail(service, "Provider " + cn + " is specified in "+currentConfig+" but not found");
aoqi@0 511 } catch (Exception x) {
aoqi@0 512 fail(service, "Provider " + cn + " is specified in "+currentConfig+"but could not be instantiated: " + x, x);
aoqi@0 513 }
aoqi@0 514 return null; /* This cannot happen */
aoqi@0 515 }
aoqi@0 516
aoqi@0 517 @Override
aoqi@0 518 public void remove() {
aoqi@0 519 throw new UnsupportedOperationException();
aoqi@0 520 }
aoqi@0 521
aoqi@0 522 }
aoqi@0 523 }

mercurial