author | vromero |
Wed, 10 Feb 2016 15:11:40 -0800 | |
changeset 35810 | 9ee6e90d679c |
parent 25874 | 83c19f00452c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
14259 | 2 |
* Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.processing; |
|
27 |
||
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; |
|
35 |
||
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 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
40 |
* <p><b>This is NOT part of any supported API. |
10 | 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; |
|
50 |
||
51 |
ServiceConfigurationError(String msg) { |
|
52 |
super(msg); |
|
53 |
} |
|
54 |
} |
|
55 |
||
56 |
private static final String prefix = "META-INF/services/"; |
|
57 |
||
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
58 |
private static void fail(Class<?> service, String msg) |
10 | 59 |
throws ServiceConfigurationError { |
60 |
throw new ServiceConfigurationError(service.getName() + ": " + msg); |
|
61 |
} |
|
62 |
||
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
63 |
private static void fail(Class<?> service, URL u, int line, String msg) |
10 | 64 |
throws ServiceConfigurationError { |
65 |
fail(service, u + ":" + line + ": " + msg); |
|
66 |
} |
|
67 |
||
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 |
* |
|
14259 | 75 |
* @param u |
10 | 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 |
*/ |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
84 |
private static boolean parse(Class<?> service, URL u) throws ServiceConfigurationError { |
10 | 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 |
} |
|
129 |
||
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 |
} |