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

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 104
5e89c4ca637c
child 184
905e151a185a
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

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

mercurial