001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.processing;
027:
028: import java.io.BufferedReader;
029: import java.io.FileNotFoundException;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.InputStreamReader;
033: import java.net.MalformedURLException;
034: import java.net.URL;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037: import java.util.List;
038: import java.util.Set;
039:
040: /**
041: * Utility class to determine if a service can be found on the
042: * path that might be used to create a class loader.
043: *
044: * <p><b>This is NOT part of any API supported by Sun Microsystems.
045: * If you write code that depends on this, you do so at your own risk.
046: * This code and its internal interfaces are subject to change or
047: * deletion without notice.</b>
048: *
049: */
050: // based on sun.misc.Service
051: class ServiceProxy {
052: static class ServiceConfigurationError extends Error {
053: static final long serialVersionUID = 7732091036771098303L;
054:
055: ServiceConfigurationError(String msg) {
056: super (msg);
057: }
058: }
059:
060: private static final String prefix = "META-INF/services/";
061:
062: private static void fail(Class service, String msg)
063: throws ServiceConfigurationError {
064: throw new ServiceConfigurationError(service.getName() + ": "
065: + msg);
066: }
067:
068: private static void fail(Class service, URL u, int line, String msg)
069: throws ServiceConfigurationError {
070: fail(service, u + ":" + line + ": " + msg);
071: }
072:
073: /**
074: * Parse the content of the given URL as a provider-configuration file.
075: *
076: * @param service
077: * The service class for which providers are being sought;
078: * used to construct error detail strings
079: *
080: * @param url
081: * The URL naming the configuration file to be parsed
082: *
083: * @return true if the name of a service is found
084: *
085: * @throws ServiceConfigurationError
086: * If an I/O error occurs while reading from the given URL, or
087: * if a configuration-file format error is detected
088: */
089: private static boolean parse(Class service, URL u)
090: throws ServiceConfigurationError {
091: InputStream in = null;
092: BufferedReader r = null;
093: try {
094: in = u.openStream();
095: r = new BufferedReader(new InputStreamReader(in, "utf-8"));
096: int lc = 1;
097: String ln;
098: while ((ln = r.readLine()) != null) {
099: int ci = ln.indexOf('#');
100: if (ci >= 0)
101: ln = ln.substring(0, ci);
102: ln = ln.trim();
103: int n = ln.length();
104: if (n != 0) {
105: if ((ln.indexOf(' ') >= 0)
106: || (ln.indexOf('\t') >= 0))
107: fail(service, u, lc,
108: "Illegal configuration-file syntax");
109: int cp = ln.codePointAt(0);
110: if (!Character.isJavaIdentifierStart(cp))
111: fail(service, u, lc,
112: "Illegal provider-class name: " + ln);
113: for (int i = Character.charCount(cp); i < n; i += Character
114: .charCount(cp)) {
115: cp = ln.codePointAt(i);
116: if (!Character.isJavaIdentifierPart(cp)
117: && (cp != '.'))
118: fail(service, u, lc,
119: "Illegal provider-class name: "
120: + ln);
121: }
122: return true;
123: }
124: }
125: } catch (FileNotFoundException x) {
126: return false;
127: } catch (IOException x) {
128: fail(service, ": " + x);
129: } finally {
130: try {
131: if (r != null)
132: r.close();
133: } catch (IOException y) {
134: fail(service, ": " + y);
135: }
136: try {
137: if (in != null)
138: in.close();
139: } catch (IOException y) {
140: fail(service, ": " + y);
141: }
142: }
143: return false;
144: }
145:
146: /**
147: * Return true if a description for at least one service is found in the
148: * service configuration files in the given URLs.
149: */
150: public static boolean hasService(Class<?> service, URL[] urls)
151: throws ServiceConfigurationError {
152: for (URL url : urls) {
153: try {
154: String fullName = prefix + service.getName();
155: URL u = new URL(url, fullName);
156: boolean found = parse(service, u);
157: if (found)
158: return true;
159: } catch (MalformedURLException e) {
160: // should not happen; ignore it if it does
161: }
162: }
163: return false;
164: }
165: }
|