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

Fri, 26 Jun 2009 18:51:39 -0700

author
jjg
date
Fri, 26 Jun 2009 18:51:39 -0700
changeset 308
03944ee4fac4
parent 184
905e151a185a
child 554
9d9f26857129
permissions
-rw-r--r--

6843077: JSR 308: Annotations on types
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.edu

     1 /*
     2  * Copyright 2006-2008 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;
    36 /**
    37  * Utility class to determine if a service can be found on the
    38  * path that might be used to create a class loader.
    39  *
    40  * <p><b>This is NOT part of any API supported by Sun Microsystems.
    41  * If you write code that depends on this, you do so at your own risk.
    42  * This code and its internal interfaces are subject to change or
    43  * deletion without notice.</b>
    44  *
    45  */
    46 // based on sun.misc.Service
    47 class ServiceProxy {
    48     static class ServiceConfigurationError extends Error {
    49         static final long serialVersionUID = 7732091036771098303L;
    51         ServiceConfigurationError(String msg) {
    52             super(msg);
    53         }
    54     }
    56     private static final String prefix = "META-INF/services/";
    58     private static void fail(Class<?> service, String msg)
    59             throws ServiceConfigurationError {
    60         throw new ServiceConfigurationError(service.getName() + ": " + msg);
    61     }
    63     private static void fail(Class<?> service, URL u, int line, String msg)
    64             throws ServiceConfigurationError {
    65         fail(service, u + ":" + line + ": " + msg);
    66     }
    68     /**
    69      * Parse the content of the given URL as a provider-configuration file.
    70      *
    71      * @param  service
    72      *         The service class for which providers are being sought;
    73      *         used to construct error detail strings
    74      *
    75      * @param  url
    76      *         The URL naming the configuration file to be parsed
    77      *
    78      * @return true if the name of a service is found
    79      *
    80      * @throws ServiceConfigurationError
    81      *         If an I/O error occurs while reading from the given URL, or
    82      *         if a configuration-file format error is detected
    83      */
    84     private static boolean parse(Class<?> service, URL u) throws ServiceConfigurationError {
    85         InputStream in = null;
    86         BufferedReader r = null;
    87         try {
    88             in = u.openStream();
    89             r = new BufferedReader(new InputStreamReader(in, "utf-8"));
    90             int lc = 1;
    91             String ln;
    92             while ((ln = r.readLine()) != null) {
    93                 int ci = ln.indexOf('#');
    94                 if (ci >= 0) ln = ln.substring(0, ci);
    95                 ln = ln.trim();
    96                 int n = ln.length();
    97                 if (n != 0) {
    98                     if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
    99                         fail(service, u, lc, "Illegal configuration-file syntax");
   100                     int cp = ln.codePointAt(0);
   101                     if (!Character.isJavaIdentifierStart(cp))
   102                         fail(service, u, lc, "Illegal provider-class name: " + ln);
   103                     for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
   104                         cp = ln.codePointAt(i);
   105                         if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
   106                             fail(service, u, lc, "Illegal provider-class name: " + ln);
   107                     }
   108                     return true;
   109                 }
   110             }
   111         } catch (FileNotFoundException x) {
   112             return false;
   113         } catch (IOException x) {
   114             fail(service, ": " + x);
   115         } finally {
   116             try {
   117                 if (r != null) r.close();
   118             } catch (IOException y) {
   119                 fail(service, ": " + y);
   120             }
   121             try {
   122                 if (in != null) in.close();
   123             } catch (IOException y) {
   124                 fail(service, ": " + y);
   125             }
   126         }
   127         return false;
   128     }
   130     /**
   131      * Return true if a description for at least one service is found in the
   132      * service configuration files in the given URLs.
   133      */
   134     public static boolean hasService(Class<?> service, URL[] urls)
   135             throws ServiceConfigurationError {
   136         for (URL url: urls) {
   137             try {
   138                 String fullName = prefix + service.getName();
   139                 URL u = new URL(url, fullName);
   140                 boolean found = parse(service, u);
   141                 if (found)
   142                     return true;
   143             } catch (MalformedURLException e) {
   144                 // should not happen; ignore it if it does
   145             }
   146         }
   147         return false;
   148     }
   149 }

mercurial