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

Sun, 15 Dec 2013 23:35:45 +0100

author
mkos
date
Sun, 15 Dec 2013 23:35:45 +0100
changeset 494
2fcd3ddb57a6
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025152: Enhance activation set up
8028388: 9 jaxws tests failed in nightly build with java.lang.ClassCastException
Summary: fix also reviewed by Bill Shannon, Alexander Fomin
Reviewed-by: dfuchs, hawtin, mgrebac
Contributed-by: bill.shannon@oracle.com

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

mercurial