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