duke@1: /* duke@1: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.processing; duke@1: duke@1: import java.io.BufferedReader; duke@1: import java.io.FileNotFoundException; duke@1: import java.io.IOException; duke@1: import java.io.InputStream; duke@1: import java.io.InputStreamReader; duke@1: import java.net.MalformedURLException; duke@1: import java.net.URL; duke@1: duke@1: /** duke@1: * Utility class to determine if a service can be found on the duke@1: * path that might be used to create a class loader. duke@1: * duke@1: *

This is NOT part of any API supported by Sun Microsystems. duke@1: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: * duke@1: */ duke@1: // based on sun.misc.Service duke@1: class ServiceProxy { duke@1: static class ServiceConfigurationError extends Error { duke@1: static final long serialVersionUID = 7732091036771098303L; duke@1: duke@1: ServiceConfigurationError(String msg) { duke@1: super(msg); duke@1: } duke@1: } duke@1: duke@1: private static final String prefix = "META-INF/services/"; duke@1: duke@1: private static void fail(Class service, String msg) duke@1: throws ServiceConfigurationError { duke@1: throw new ServiceConfigurationError(service.getName() + ": " + msg); duke@1: } duke@1: duke@1: private static void fail(Class service, URL u, int line, String msg) duke@1: throws ServiceConfigurationError { duke@1: fail(service, u + ":" + line + ": " + msg); duke@1: } duke@1: duke@1: /** duke@1: * Parse the content of the given URL as a provider-configuration file. duke@1: * duke@1: * @param service duke@1: * The service class for which providers are being sought; duke@1: * used to construct error detail strings duke@1: * duke@1: * @param url duke@1: * The URL naming the configuration file to be parsed duke@1: * duke@1: * @return true if the name of a service is found duke@1: * duke@1: * @throws ServiceConfigurationError duke@1: * If an I/O error occurs while reading from the given URL, or duke@1: * if a configuration-file format error is detected duke@1: */ duke@1: private static boolean parse(Class service, URL u) throws ServiceConfigurationError { duke@1: InputStream in = null; duke@1: BufferedReader r = null; duke@1: try { duke@1: in = u.openStream(); duke@1: r = new BufferedReader(new InputStreamReader(in, "utf-8")); duke@1: int lc = 1; duke@1: String ln; duke@1: while ((ln = r.readLine()) != null) { duke@1: int ci = ln.indexOf('#'); duke@1: if (ci >= 0) ln = ln.substring(0, ci); duke@1: ln = ln.trim(); duke@1: int n = ln.length(); duke@1: if (n != 0) { duke@1: if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) duke@1: fail(service, u, lc, "Illegal configuration-file syntax"); duke@1: int cp = ln.codePointAt(0); duke@1: if (!Character.isJavaIdentifierStart(cp)) duke@1: fail(service, u, lc, "Illegal provider-class name: " + ln); duke@1: for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) { duke@1: cp = ln.codePointAt(i); duke@1: if (!Character.isJavaIdentifierPart(cp) && (cp != '.')) duke@1: fail(service, u, lc, "Illegal provider-class name: " + ln); duke@1: } duke@1: return true; duke@1: } duke@1: } duke@1: } catch (FileNotFoundException x) { duke@1: return false; duke@1: } catch (IOException x) { duke@1: fail(service, ": " + x); duke@1: } finally { duke@1: try { duke@1: if (r != null) r.close(); duke@1: } catch (IOException y) { duke@1: fail(service, ": " + y); duke@1: } duke@1: try { duke@1: if (in != null) in.close(); duke@1: } catch (IOException y) { duke@1: fail(service, ": " + y); duke@1: } duke@1: } duke@1: return false; duke@1: } duke@1: duke@1: /** duke@1: * Return true if a description for at least one service is found in the duke@1: * service configuration files in the given URLs. duke@1: */ duke@1: public static boolean hasService(Class service, URL[] urls) duke@1: throws ServiceConfigurationError { duke@1: for (URL url: urls) { duke@1: try { duke@1: String fullName = prefix + service.getName(); duke@1: URL u = new URL(url, fullName); duke@1: boolean found = parse(service, u); duke@1: if (found) duke@1: return true; duke@1: } catch (MalformedURLException e) { duke@1: // should not happen; ignore it if it does duke@1: } duke@1: } duke@1: return false; duke@1: } duke@1: }