jdk/src/share/classes/sun/misc/Service.java
author chegar
Mon, 08 Apr 2013 06:15:18 +0100
changeset 18223 35a5c2462991
parent 14342 8435a30053c1
permissions -rw-r--r--
8008593: Better URLClassLoader resource management Reviewed-by: alanb, sherman, hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14218
diff changeset
     2
 * Copyright (c) 1999, 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 sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.TreeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A simple service-provider lookup mechanism.  A <i>service</i> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * well-known set of interfaces and (usually abstract) classes.  A <i>service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * provider</i> is a specific implementation of a service.  The classes in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * provider typically implement the interfaces and subclass the classes defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * in the service itself.  Service providers may be installed in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * implementation of the Java platform in the form of extensions, that is, jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * files placed into any of the usual extension directories.  Providers may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * also be made available by adding them to the applet or application class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * 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> In this lookup mechanism a service is represented by an interface or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * abstract class.  (A concrete class may be used, but this is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * recommended.)  A provider of a given service contains one or more concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * classes that extend this <i>service class</i> with data and code specific to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * the provider.  This <i>provider class</i> will typically not be the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * provider itself but rather a proxy that contains enough information to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * decide whether the provider is able to satisfy a particular request together
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * with code that can create the actual provider on demand.  The details of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * provider classes tend to be highly service-specific; no single class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * interface could possibly unify them, so no such class has been defined.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * only requirement enforced here is that provider classes must have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * zero-argument constructor so that they may be instantiated during lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p> A service provider identifies itself by placing a provider-configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * file in the resource directory <tt>META-INF/services</tt>.  The file's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * should consist of the fully-qualified name of the abstract service class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * The file should contain a list of fully-qualified concrete provider-class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * names, one per line.  Space and tab characters surrounding each name, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * well as blank lines, are ignored.  The comment character is <tt>'#'</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * (<tt>0x23</tt>); on each line all characters following the first comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * character are ignored.  The file must be encoded in UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p> If a particular concrete provider class is named in more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * configuration file, or is named in the same configuration file more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * once, then the duplicates will be ignored.  The configuration file naming a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * particular provider need not be in the same jar file or other distribution
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * unit as the provider itself.  The provider must be accessible from the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * class loader that was initially queried to locate the configuration file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * note that this is not necessarily the class loader that found the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p> <b>Example:</b> Suppose we have a service class named
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <tt>java.io.spi.CharCodec</tt>.  It has two abstract methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *   public abstract CharEncoder getEncoder(String encodingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   public abstract CharDecoder getDecoder(String encodingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * Each method returns an appropriate object or <tt>null</tt> if it cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * translate the given encoding.  Typical <tt>CharCodec</tt> providers will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * support more than one encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p> If <tt>sun.io.StandardCodec</tt> is a provider of the <tt>CharCodec</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * service then its jar file would contain the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <tt>META-INF/services/java.io.spi.CharCodec</tt>.  This file would contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * the single line:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   sun.io.StandardCodec    # Standard codecs for the platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * To locate an encoder for a given encoding name, the internal I/O code would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * do something like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *   CharEncoder getEncoder(String encodingName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *       Iterator ps = Service.providers(CharCodec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *       while (ps.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *           CharCodec cc = (CharCodec)ps.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *           CharEncoder ce = cc.getEncoder(encodingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *           if (ce != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *               return ce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *       return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * The provider-lookup mechanism always executes in the security context of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * caller.  Trusted system code should typically invoke the methods in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * class from within a privileged security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   128
public final class Service<S> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final String prefix = "META-INF/services/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private Service() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   134
    private static void fail(Class<?> service, String msg, Throwable cause)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        ServiceConfigurationError sce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            = new ServiceConfigurationError(service.getName() + ": " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        sce.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        throw sce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   143
    private static void fail(Class<?> service, String msg)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        throw new ServiceConfigurationError(service.getName() + ": " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   149
    private static void fail(Class<?> service, URL u, int line, String msg)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        fail(service, u + ":" + line + ": " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Parse a single line from the given configuration file, adding the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * on the line to both the names list and the returned set iff the name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * not already a member of the returned set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   160
    private static int parseLine(Class<?> service, URL u, BufferedReader r, int lc,
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   161
                                 List<String> names, Set<String> returned)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        throws IOException, ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        String ln = r.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (ln == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int ci = ln.indexOf('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (ci >= 0) ln = ln.substring(0, ci);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        ln = ln.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int n = ln.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (n != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                fail(service, u, lc, "Illegal configuration-file syntax");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int cp = ln.codePointAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (!Character.isJavaIdentifierStart(cp))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                fail(service, u, lc, "Illegal provider-class name: " + ln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                cp = ln.codePointAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    fail(service, u, lc, "Illegal provider-class name: " + ln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (!returned.contains(ln)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                names.add(ln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                returned.add(ln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return lc + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Parse the content of the given URL as a provider-configuration file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param  service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *         The service class for which providers are being sought;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *         used to construct error detail strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param  url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *         The URL naming the configuration file to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param  returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *         A Set containing the names of provider classes that have already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *         been returned.  This set will be updated to contain the names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *         that will be yielded from the returned <tt>Iterator</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @return A (possibly empty) <tt>Iterator</tt> that will yield the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *         provider-class names in the given configuration file that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *         not yet members of the returned set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *         If an I/O error occurs while reading from the given URL, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *         if a configuration-file format error is detected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   214
    private static Iterator<String> parse(Class<?> service, URL u, Set<String> returned)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        BufferedReader r = null;
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   219
        ArrayList<String> names = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            in = u.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            r = new BufferedReader(new InputStreamReader(in, "utf-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            int lc = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            while ((lc = parseLine(service, u, r, lc, names, returned)) >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            fail(service, ": " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (r != null) r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if (in != null) in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            } catch (IOException y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                fail(service, ": " + y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return names.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Private inner class implementing fully-lazy provider lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   242
    private static class LazyIterator<S> implements Iterator<S> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   244
        Class<S> service;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        ClassLoader loader;
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   246
        Enumeration<URL> configs = null;
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   247
        Iterator<String> pending = null;
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   248
        Set<String> returned = new TreeSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        String nextName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   251
        private LazyIterator(Class<S> service, ClassLoader loader) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            this.service = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            this.loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        public boolean hasNext() throws ServiceConfigurationError {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (nextName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (configs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    String fullName = prefix + service.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        configs = ClassLoader.getSystemResources(fullName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        configs = loader.getResources(fullName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    fail(service, ": " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            while ((pending == null) || !pending.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                if (!configs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                }
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   275
                pending = parse(service, configs.nextElement(), returned);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   277
            nextName = pending.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   281
        public S next() throws ServiceConfigurationError {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (!hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            String cn = nextName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            nextName = null;
14218
c6f2434dd88d 7195919: (sl) ServiceLoader can throw CCE without needing to create instance
smarks
parents: 11119
diff changeset
   287
            Class<?> c = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            try {
14218
c6f2434dd88d 7195919: (sl) ServiceLoader can throw CCE without needing to create instance
smarks
parents: 11119
diff changeset
   289
                c = Class.forName(cn, false, loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                fail(service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                     "Provider " + cn + " not found");
14218
c6f2434dd88d 7195919: (sl) ServiceLoader can throw CCE without needing to create instance
smarks
parents: 11119
diff changeset
   293
            }
c6f2434dd88d 7195919: (sl) ServiceLoader can throw CCE without needing to create instance
smarks
parents: 11119
diff changeset
   294
            if (!service.isAssignableFrom(c)) {
c6f2434dd88d 7195919: (sl) ServiceLoader can throw CCE without needing to create instance
smarks
parents: 11119
diff changeset
   295
                fail(service,
c6f2434dd88d 7195919: (sl) ServiceLoader can throw CCE without needing to create instance
smarks
parents: 11119
diff changeset
   296
                     "Provider " + cn  + " not a subtype");
c6f2434dd88d 7195919: (sl) ServiceLoader can throw CCE without needing to create instance
smarks
parents: 11119
diff changeset
   297
            }
c6f2434dd88d 7195919: (sl) ServiceLoader can throw CCE without needing to create instance
smarks
parents: 11119
diff changeset
   298
            try {
c6f2434dd88d 7195919: (sl) ServiceLoader can throw CCE without needing to create instance
smarks
parents: 11119
diff changeset
   299
                return service.cast(c.newInstance());
c6f2434dd88d 7195919: (sl) ServiceLoader can throw CCE without needing to create instance
smarks
parents: 11119
diff changeset
   300
            } catch (Throwable x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                fail(service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                     "Provider " + cn + " could not be instantiated: " + x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                     x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return null;        /* This cannot happen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Locates and incrementally instantiates the available providers of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * given service using the given class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p> This method transforms the name of the given service class into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * provider-configuration filename as described above and then uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <tt>getResources</tt> method of the given class loader to find all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * available files with that name.  These files are then read and parsed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * produce a list of provider-class names.  The iterator that is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * uses the given class loader to lookup and then instantiate each element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p> Because it is possible for extensions to be installed into a running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Java virtual machine, this method may return different results each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * it is invoked. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param  service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *         The service's abstract service class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param  loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *         The class loader to be used to load provider-configuration files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *         and instantiate provider classes, or <tt>null</tt> if the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *         class loader (or, failing that the bootstrap class loader) is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *         be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @return An <tt>Iterator</tt> that yields provider objects for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *         service, in some arbitrary order.  The iterator will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *         <tt>ServiceConfigurationError</tt> if a provider-configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *         file violates the specified format or if a provider class cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *         be found and instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *         If a provider-configuration file violates the specified format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *         or names a provider class that cannot be found and instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @see #providers(java.lang.Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @see #installedProviders(java.lang.Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   353
    public static <S> Iterator<S> providers(Class<S> service, ClassLoader loader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   356
        return new LazyIterator<S>(service, loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Locates and incrementally instantiates the available providers of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * given service using the context class loader.  This convenience method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *   ClassLoader cl = Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *   return Service.providers(service, cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param  service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *         The service's abstract service class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return An <tt>Iterator</tt> that yields provider objects for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *         service, in some arbitrary order.  The iterator will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *         <tt>ServiceConfigurationError</tt> if a provider-configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *         file violates the specified format or if a provider class cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *         be found and instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *         If a provider-configuration file violates the specified format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *         or names a provider class that cannot be found and instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see #providers(java.lang.Class, java.lang.ClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   385
    public static <S> Iterator<S> providers(Class<S> service)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return Service.providers(service, cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Locates and incrementally instantiates the available providers of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * given service using the extension class loader.  This convenience method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * simply locates the extension class loader, call it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <tt>extClassLoader</tt>, and then does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *   return Service.providers(service, extClassLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * If the extension class loader cannot be found then the system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * loader is used; if there is no system class loader then the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * class loader is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @param  service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *         The service's abstract service class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @return An <tt>Iterator</tt> that yields provider objects for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *         service, in some arbitrary order.  The iterator will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *         <tt>ServiceConfigurationError</tt> if a provider-configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *         file violates the specified format or if a provider class cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *         be found and instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *         If a provider-configuration file violates the specified format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         or names a provider class that cannot be found and instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @see #providers(java.lang.Class, java.lang.ClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   422
    public static <S> Iterator<S> installedProviders(Class<S> service)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        throws ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        ClassLoader cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        ClassLoader prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        while (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            prev = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            cl = cl.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return Service.providers(service, prev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
}