src/share/classes/com/sun/tools/javac/processing/ServiceProxy.java

Thu, 24 Jul 2008 19:06:57 +0100

author
mcimadamore
date
Thu, 24 Jul 2008 19:06:57 +0100
changeset 80
5c9cdeb740f2
parent 1
9a66ca7c79fa
child 104
5e89c4ca637c
permissions
-rw-r--r--

6717241: some diagnostic argument is prematurely converted into a String object
Summary: removed early toString() conversions applied to diagnostic arguments
Reviewed-by: jjg

     1 /*
     2  * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.processing;
    28 import java.io.BufferedReader;
    29 import java.io.FileNotFoundException;
    30 import java.io.IOException;
    31 import java.io.InputStream;
    32 import java.io.InputStreamReader;
    33 import java.net.MalformedURLException;
    34 import java.net.URL;
    35 import java.util.ArrayList;
    36 import java.util.Iterator;
    37 import java.util.List;
    38 import java.util.Set;
    40 /**
    41  * Utility class to determine if a service can be found on the
    42  * path that might be used to create a class loader.
    43  *
    44  * <p><b>This is NOT part of any API supported by Sun Microsystems.
    45  * If you write code that depends on this, you do so at your own risk.
    46  * This code and its internal interfaces are subject to change or
    47  * deletion without notice.</b>
    48  *
    49  */
    50 // based on sun.misc.Service
    51 class ServiceProxy {
    52     static class ServiceConfigurationError extends Error {
    53         static final long serialVersionUID = 7732091036771098303L;
    55         ServiceConfigurationError(String msg) {
    56             super(msg);
    57         }
    58     }
    60     private static final String prefix = "META-INF/services/";
    62     private static void fail(Class service, String msg)
    63             throws ServiceConfigurationError {
    64         throw new ServiceConfigurationError(service.getName() + ": " + msg);
    65     }
    67     private static void fail(Class service, URL u, int line, String msg)
    68             throws ServiceConfigurationError {
    69         fail(service, u + ":" + line + ": " + msg);
    70     }
    72     /**
    73      * Parse the content of the given URL as a provider-configuration file.
    74      *
    75      * @param  service
    76      *         The service class for which providers are being sought;
    77      *         used to construct error detail strings
    78      *
    79      * @param  url
    80      *         The URL naming the configuration file to be parsed
    81      *
    82      * @return true if the name of a service is found
    83      *
    84      * @throws ServiceConfigurationError
    85      *         If an I/O error occurs while reading from the given URL, or
    86      *         if a configuration-file format error is detected
    87      */
    88     private static boolean parse(Class service, URL u) throws ServiceConfigurationError {
    89         InputStream in = null;
    90         BufferedReader r = null;
    91         try {
    92             in = u.openStream();
    93             r = new BufferedReader(new InputStreamReader(in, "utf-8"));
    94             int lc = 1;
    95             String ln;
    96             while ((ln = r.readLine()) != null) {
    97                 int ci = ln.indexOf('#');
    98                 if (ci >= 0) ln = ln.substring(0, ci);
    99                 ln = ln.trim();
   100                 int n = ln.length();
   101                 if (n != 0) {
   102                     if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
   103                         fail(service, u, lc, "Illegal configuration-file syntax");
   104                     int cp = ln.codePointAt(0);
   105                     if (!Character.isJavaIdentifierStart(cp))
   106                         fail(service, u, lc, "Illegal provider-class name: " + ln);
   107                     for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
   108                         cp = ln.codePointAt(i);
   109                         if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
   110                             fail(service, u, lc, "Illegal provider-class name: " + ln);
   111                     }
   112                     return true;
   113                 }
   114             }
   115         } catch (FileNotFoundException x) {
   116             return false;
   117         } catch (IOException x) {
   118             fail(service, ": " + x);
   119         } finally {
   120             try {
   121                 if (r != null) r.close();
   122             } catch (IOException y) {
   123                 fail(service, ": " + y);
   124             }
   125             try {
   126                 if (in != null) in.close();
   127             } catch (IOException y) {
   128                 fail(service, ": " + y);
   129             }
   130         }
   131         return false;
   132     }
   134     /**
   135      * Return true if a description for at least one service is found in the
   136      * service configuration files in the given URLs.
   137      */
   138     public static boolean hasService(Class<?> service, URL[] urls)
   139             throws ServiceConfigurationError {
   140         for (URL url: urls) {
   141             try {
   142                 String fullName = prefix + service.getName();
   143                 URL u = new URL(url, fullName);
   144                 boolean found = parse(service, u);
   145                 if (found)
   146                     return true;
   147             } catch (MalformedURLException e) {
   148                 // should not happen; ignore it if it does
   149             }
   150         }
   151         return false;
   152     }
   153 }

mercurial