langtools/src/share/classes/com/sun/tools/javac/processing/ServiceProxy.java
author jjg
Fri, 29 Aug 2008 11:10:12 -0700
changeset 1206 3a05355982a9
parent 10 06bc494ca11e
child 1264 076a3cde30d5
permissions -rw-r--r--
6597471: unused imports in javax.tools.JavaCompiler 6597531: unused imports and unused private const. in com.sun.tools.javac.Server.java Reviewed-by: mcimadamore Contributed-by: davide.angelocola@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * <p><b>This is NOT part of any API supported by Sun Microsystems.
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
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    private static void fail(Class service, String msg)
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
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private static void fail(Class service, URL u, int line, String msg)
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
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     * @param  url
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
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    private static boolean parse(Class service, URL u) throws ServiceConfigurationError {
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
}