jdk/src/share/classes/java/util/ServiceLoader.java
author psandoz
Tue, 02 Oct 2012 10:36:05 +0100
changeset 14016 56be5085fc31
parent 12448 b95438b17098
child 14228 131a42bc2c20
child 14191 01f4a54604a6
permissions -rw-r--r--
7197642: (sl) ServiceLoader.load(null) doesn't throw NPE Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7803
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A simple service-provider loading facility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> A <i>service</i> is a well-known set of interfaces and (usually
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * abstract) classes.  A <i>service provider</i> is a specific implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * of a service.  The classes in a provider typically implement the interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * and subclass the classes defined in the service itself.  Service providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * can be installed in an implementation of the Java platform in the form of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * extensions, that is, jar files placed into any of the usual extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * directories.  Providers can also be made available by adding them to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * application's class path or by some other platform-specific means.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p> For the purpose of loading, a service is represented by a single type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * that is, a single interface or abstract class.  (A concrete class can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * used, but this is not recommended.)  A provider of a given service contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * one or more concrete classes that extend this <i>service type</i> with data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * and code specific to the provider.  The <i>provider class</i> is typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * not the entire provider itself but rather a proxy which contains enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * information to decide whether the provider is able to satisfy a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * request together with code that can create the actual provider on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * The details of provider classes tend to be highly service-specific; no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * single class or interface could possibly unify them, so no such type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * defined here.  The only requirement enforced by this facility is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * provider classes must have a zero-argument constructor so that they can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * instantiated during loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p><a name="format"> A service provider is identified by placing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <i>provider-configuration file</i> in the resource directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <tt>META-INF/services</tt>.  The file's name is the fully-qualified <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * href="../lang/ClassLoader.html#name">binary name</a> of the service's type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The file contains a list of fully-qualified binary names of concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * provider classes, one per line.  Space and tab characters surrounding each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * name, as well as blank lines, are ignored.  The comment character is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <tt>'#'</tt> (<tt>'&#92;u0023'</tt>, <font size="-1">NUMBER SIGN</font>); on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * each line all characters following the first comment character are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * The file must be encoded in UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p> If a particular concrete provider class is named in more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * configuration file, or is named in the same configuration file more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * once, then the duplicates are ignored.  The configuration file naming a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * particular provider need not be in the same jar file or other distribution
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * unit as the provider itself.  The provider must be accessible from the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * class loader that was initially queried to locate the configuration file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * note that this is not necessarily the class loader from which the file was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * actually loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p> Providers are located and instantiated lazily, that is, on demand.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * service loader maintains a cache of the providers that have been loaded so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * far.  Each invocation of the {@link #iterator iterator} method returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * iterator that first yields all of the elements of the cache, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * instantiation order, and then lazily locates and instantiates any remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * providers, adding each one to the cache in turn.  The cache can be cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * via the {@link #reload reload} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p> Service loaders always execute in the security context of the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Trusted system code should typically invoke the methods in this class, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * the methods of the iterators which they return, from within a privileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p> Instances of this class are not safe for use by multiple concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p> Unless otherwise specified, passing a <tt>null</tt> argument to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * method in this class will cause a {@link NullPointerException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p><span style="font-weight: bold; padding-right: 1em">Example</span>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Suppose we have a service type <tt>com.example.CodecSet</tt> which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * intended to represent sets of encoder/decoder pairs for some protocol.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * this case it is an abstract class with two abstract methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * public abstract Encoder getEncoder(String encodingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * public abstract Decoder getDecoder(String encodingName);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * Each method returns an appropriate object or <tt>null</tt> if the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * does not support the given encoding.  Typical providers support more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * one encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <p> If <tt>com.example.impl.StandardCodecs</tt> is an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <tt>CodecSet</tt> service then its jar file also contains a file named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * META-INF/services/com.example.CodecSet</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <p> This file contains the single line:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * com.example.impl.StandardCodecs    # Standard codecs</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <p> The <tt>CodecSet</tt> class creates and saves a single service instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * at initialization:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * private static ServiceLoader&lt;CodecSet&gt; codecSetLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *     = ServiceLoader.load(CodecSet.class);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <p> To locate an encoder for a given encoding name it defines a static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * factory method which iterates through the known and available providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * returning only when it has located a suitable encoder or has run out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * public static Encoder getEncoder(String encodingName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *     for (CodecSet cp : codecSetLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *         Encoder enc = cp.getEncoder(encodingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *         if (enc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *             return enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *     return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * }</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * <p> A <tt>getDecoder</tt> method is defined similarly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <p><span style="font-weight: bold; padding-right: 1em">Usage Note</span> If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * the class path of a class loader that is used for provider loading includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * remote network URLs then those URLs will be dereferenced in the process of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * searching for provider-configuration files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <p> This activity is normal, although it may cause puzzling entries to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * created in web-server logs.  If a web server is not configured correctly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * however, then this activity may cause the provider-loading algorithm to fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * spuriously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <p> A web server should return an HTTP 404 (Not Found) response when a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * requested resource does not exist.  Sometimes, however, web servers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * erroneously configured to return an HTTP 200 (OK) response along with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * helpful HTML error page in such cases.  This will cause a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * ServiceConfigurationError} to be thrown when this class attempts to parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * the HTML page as a provider-configuration file.  The best solution to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * problem is to fix the misconfigured web server to return the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * response code (HTTP 404) along with the HTML error page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * @param  <S>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *         The type of the service to be loaded by this loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
public final class ServiceLoader<S>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    implements Iterable<S>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private static final String PREFIX = "META-INF/services/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    // The class or interface representing the service being loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private Class<S> service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    // The class loader used to locate, load, and instantiate providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private ClassLoader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    // Cached providers, in instantiation order
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   194
    private LinkedHashMap<String,S> providers = new LinkedHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // The current lazy-lookup iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private LazyIterator lookupIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Clear this loader's provider cache so that all providers will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * reloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <p> After invoking this method, subsequent invocations of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * #iterator() iterator} method will lazily look up and instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * providers from scratch, just as is done by a newly-created loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p> This method is intended for use in situations in which new providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * can be installed into a running Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public void reload() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        providers.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        lookupIterator = new LazyIterator(service, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private ServiceLoader(Class<S> svc, ClassLoader cl) {
14016
56be5085fc31 7197642: (sl) ServiceLoader.load(null) doesn't throw NPE
psandoz
parents: 12448
diff changeset
   216
        service = Objects.requireNonNull(svc, "Service interface cannot be null");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        loader = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        reload();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   221
    private static void fail(Class<?> service, String msg, Throwable cause)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        throw new ServiceConfigurationError(service.getName() + ": " + msg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                            cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   228
    private static void fail(Class<?> service, String msg)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        throw new ServiceConfigurationError(service.getName() + ": " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   234
    private static void fail(Class<?> service, URL u, int line, String msg)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        fail(service, u + ":" + line + ": " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    // Parse a single line from the given configuration file, adding the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    // on the line to the names list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    //
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   243
    private int parseLine(Class<?> service, URL u, BufferedReader r, int lc,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                          List<String> names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        throws IOException, ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        String ln = r.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (ln == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int ci = ln.indexOf('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (ci >= 0) ln = ln.substring(0, ci);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        ln = ln.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int n = ln.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (n != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                fail(service, u, lc, "Illegal configuration-file syntax");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int cp = ln.codePointAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (!Character.isJavaIdentifierStart(cp))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                fail(service, u, lc, "Illegal provider-class name: " + ln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                cp = ln.codePointAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    fail(service, u, lc, "Illegal provider-class name: " + ln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (!providers.containsKey(ln) && !names.contains(ln))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                names.add(ln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return lc + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    // Parse the content of the given URL as a provider-configuration file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    // @param  service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    //         The service type for which providers are being sought;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    //         used to construct error detail strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    // @param  u
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    //         The URL naming the configuration file to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    // @return A (possibly empty) iterator that will yield the provider-class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    //         names in the given configuration file that are not yet members
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    //         of the returned set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    // @throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    //         If an I/O error occurs while reading from the given URL, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    //         if a configuration-file format error is detected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    //
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   289
    private Iterator<String> parse(Class<?> service, URL u)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        BufferedReader r = null;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   294
        ArrayList<String> names = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            in = u.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            r = new BufferedReader(new InputStreamReader(in, "utf-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            int lc = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            while ((lc = parseLine(service, u, r, lc, names)) >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            fail(service, "Error reading configuration file", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (r != null) r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                if (in != null) in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            } catch (IOException y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                fail(service, "Error closing configuration file", y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return names.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    // Private inner class implementing fully-lazy provider lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private class LazyIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        implements Iterator<S>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        Class<S> service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        ClassLoader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        Enumeration<URL> configs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        Iterator<String> pending = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        String nextName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        private LazyIterator(Class<S> service, ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            this.service = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            this.loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (nextName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (configs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    String fullName = PREFIX + service.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        configs = ClassLoader.getSystemResources(fullName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        configs = loader.getResources(fullName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    fail(service, "Error locating configuration files", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            while ((pending == null) || !pending.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                if (!configs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                pending = parse(service, configs.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            nextName = pending.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public S next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (!hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            String cn = nextName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            nextName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                S p = service.cast(Class.forName(cn, true, loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                   .newInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                providers.put(cn, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                fail(service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                     "Provider " + cn + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            } catch (Throwable x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                fail(service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                     "Provider " + cn + " could not be instantiated: " + x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                     x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            throw new Error();          // This cannot happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Lazily loads the available providers of this loader's service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <p> The iterator returned by this method first yields all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * elements of the provider cache, in instantiation order.  It then lazily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * loads and instantiates any remaining providers, adding each one to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * cache in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <p> To achieve laziness the actual work of parsing the available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * provider-configuration files and instantiating providers must be done by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * the iterator itself.  Its {@link java.util.Iterator#hasNext hasNext} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * {@link java.util.Iterator#next next} methods can therefore throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * {@link ServiceConfigurationError} if a provider-configuration file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * violates the specified format, or if it names a provider class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * cannot be found and instantiated, or if the result of instantiating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * class is not assignable to the service type, or if any other kind of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * exception or error is thrown as the next provider is located and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * instantiated.  To write robust code it is only necessary to catch {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * ServiceConfigurationError} when using a service iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <p> If such an error is thrown then subsequent invocations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * iterator will make a best effort to locate and instantiate the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * available provider, but in general such recovery cannot be guaranteed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * <blockquote style="font-size: smaller; line-height: 1.2"><span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * style="padding-right: 1em; font-weight: bold">Design Note</span>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Throwing an error in these cases may seem extreme.  The rationale for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * this behavior is that a malformed provider-configuration file, like a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * malformed class file, indicates a serious problem with the way the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * virtual machine is configured or is being used.  As such it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * preferable to throw an error rather than try to recover or, even worse,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * fail silently.</blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <p> The iterator returned by this method does not support removal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Invoking its {@link java.util.Iterator#remove() remove} method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * cause an {@link UnsupportedOperationException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @return  An iterator that lazily loads providers for this loader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *          service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public Iterator<S> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return new Iterator<S>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            Iterator<Map.Entry<String,S>> knownProviders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                = providers.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                if (knownProviders.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                return lookupIterator.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            public S next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                if (knownProviders.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    return knownProviders.next().getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                return lookupIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Creates a new service loader for the given service type and class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param  service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *         The interface or abstract class representing the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param  loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *         The class loader to be used to load provider-configuration files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *         and provider classes, or <tt>null</tt> if the system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *         loader (or, failing that, the bootstrap class loader) is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *         used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @return A new service loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public static <S> ServiceLoader<S> load(Class<S> service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                            ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   466
        return new ServiceLoader<>(service, loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Creates a new service loader for the given service type, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * current thread's {@linkplain java.lang.Thread#getContextClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * context class loader}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p> An invocation of this convenience method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * ServiceLoader.load(<i>service</i>)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * ServiceLoader.load(<i>service</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *                    Thread.currentThread().getContextClassLoader())</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @param  service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *         The interface or abstract class representing the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return A new service loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public static <S> ServiceLoader<S> load(Class<S> service) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        return ServiceLoader.load(service, cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Creates a new service loader for the given service type, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * extension class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * <p> This convenience method simply locates the extension class loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * call it <tt><i>extClassLoader</i></tt>, and then returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * ServiceLoader.load(<i>service</i>, <i>extClassLoader</i>)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * <p> If the extension class loader cannot be found then the system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * loader is used; if there is no system class loader then the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * class loader is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * <p> This method is intended for use when only installed providers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * desired.  The resulting service will only find and load providers that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * have been installed into the current Java virtual machine; providers on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * the application's class path will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @param  service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *         The interface or abstract class representing the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @return A new service loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public static <S> ServiceLoader<S> loadInstalled(Class<S> service) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        ClassLoader cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        ClassLoader prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        while (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            prev = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            cl = cl.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return ServiceLoader.load(service, prev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Returns a string describing this service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @return  A descriptive string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return "java.util.ServiceLoader[" + service.getName() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
}