langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/ServiceProxy.java
author vromero
Wed, 10 Feb 2016 15:11:40 -0800
changeset 35810 9ee6e90d679c
parent 25874 83c19f00452c
permissions -rw-r--r--
8148808: javac, remove unused options, step 1 Reviewed-by: mcimadamore, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 5847
diff changeset
     2
 * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.processing;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedReader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.FileNotFoundException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.InputStreamReader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.net.MalformedURLException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.net.URL;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * Utility class to determine if a service can be found on the
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * path that might be used to create a class loader.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    40
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
// based on sun.misc.Service
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
class ServiceProxy {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    static class ServiceConfigurationError extends Error {
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        static final long serialVersionUID = 7732091036771098303L;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        ServiceConfigurationError(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
            super(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    private static final String prefix = "META-INF/services/";
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
    58
    private static void fail(Class<?> service, String msg)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
            throws ServiceConfigurationError {
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        throw new ServiceConfigurationError(service.getName() + ": " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
    63
    private static void fail(Class<?> service, URL u, int line, String msg)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
            throws ServiceConfigurationError {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        fail(service, u + ":" + line + ": " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     * Parse the content of the given URL as a provider-configuration file.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * @param  service
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     *         The service class for which providers are being sought;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     *         used to construct error detail strings
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     *
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 5847
diff changeset
    75
     * @param  u
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     *         The URL naming the configuration file to be parsed
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * @return true if the name of a service is found
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     * @throws ServiceConfigurationError
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     *         If an I/O error occurs while reading from the given URL, or
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     *         if a configuration-file format error is detected
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     */
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
    84
    private static boolean parse(Class<?> service, URL u) throws ServiceConfigurationError {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        InputStream in = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        BufferedReader r = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            in = u.openStream();
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            r = new BufferedReader(new InputStreamReader(in, "utf-8"));
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            int lc = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            String ln;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            while ((ln = r.readLine()) != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
                int ci = ln.indexOf('#');
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
                if (ci >= 0) ln = ln.substring(0, ci);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
                ln = ln.trim();
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
                int n = ln.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                if (n != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
                    if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                        fail(service, u, lc, "Illegal configuration-file syntax");
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                    int cp = ln.codePointAt(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                    if (!Character.isJavaIdentifierStart(cp))
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                        fail(service, u, lc, "Illegal provider-class name: " + ln);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                    for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                        cp = ln.codePointAt(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                        if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                            fail(service, u, lc, "Illegal provider-class name: " + ln);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        } catch (FileNotFoundException x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        } catch (IOException x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            fail(service, ": " + x);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                if (r != null) r.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            } catch (IOException y) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                fail(service, ": " + y);
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                if (in != null) in.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            } catch (IOException y) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                fail(service, ": " + y);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     * Return true if a description for at least one service is found in the
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     * service configuration files in the given URLs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    public static boolean hasService(Class<?> service, URL[] urls)
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            throws ServiceConfigurationError {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        for (URL url: urls) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                String fullName = prefix + service.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                URL u = new URL(url, fullName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                boolean found = parse(service, u);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                if (found)
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            } catch (MalformedURLException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                // should not happen; ignore it if it does
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
}