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

Wed, 12 Mar 2008 13:06:00 -0700

author
jjg
date
Wed, 12 Mar 2008 13:06:00 -0700
changeset 12
7366066839bb
parent 1
9a66ca7c79fa
child 104
5e89c4ca637c
permissions
-rw-r--r--

6668794: javac puts localized text in raw diagnostics
6668796: bad diagnostic "bad class file" given for source files
Summary: Replace internal use of localized text with JCDiagnostic fragments; fix diagnostic for bad source file
Reviewed-by: mcimadamore

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

mercurial