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