43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.compiler.serviceprovider;
|
|
24 |
|
46459
|
25 |
import static org.graalvm.compiler.serviceprovider.JDK9Method.Java8OrEarlier;
|
|
26 |
import static org.graalvm.compiler.serviceprovider.JDK9Method.addOpens;
|
|
27 |
import static org.graalvm.compiler.serviceprovider.JDK9Method.getModule;
|
|
28 |
import static org.graalvm.compiler.serviceprovider.JDK9Method.getPackages;
|
|
29 |
import static org.graalvm.compiler.serviceprovider.JDK9Method.isOpenTo;
|
|
30 |
|
|
31 |
import java.lang.reflect.Method;
|
43972
|
32 |
import java.util.Iterator;
|
|
33 |
import java.util.ServiceConfigurationError;
|
|
34 |
import java.util.ServiceLoader;
|
46459
|
35 |
import java.util.Set;
|
43972
|
36 |
|
|
37 |
import jdk.vm.ci.services.JVMCIPermission;
|
|
38 |
import jdk.vm.ci.services.Services;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* A mechanism for accessing service providers that abstracts over whether Graal is running on
|
|
42 |
* JVMCI-8 or JVMCI-9. In JVMCI-8, a JVMCI specific mechanism is used to lookup services via the
|
46459
|
43 |
* hidden JVMCI class loader. In JVMCI-9, the standard {@link ServiceLoader} mechanism is used.
|
43972
|
44 |
*/
|
|
45 |
public final class GraalServices {
|
|
46 |
|
|
47 |
private GraalServices() {
|
|
48 |
}
|
|
49 |
|
46459
|
50 |
/**
|
|
51 |
* Opens all JVMCI packages to the module of a given class. This relies on JVMCI already having
|
|
52 |
* opened all its packages to the module defining {@link GraalServices}.
|
|
53 |
*
|
|
54 |
* @param other all JVMCI packages will be opened to the module defining this class
|
|
55 |
*/
|
|
56 |
public static void openJVMCITo(Class<?> other) {
|
49451
|
57 |
Object jvmci = getModule(Services.class);
|
|
58 |
Object otherModule = getModule(other);
|
46459
|
59 |
if (jvmci != otherModule) {
|
49451
|
60 |
Set<String> packages = getPackages(jvmci);
|
46459
|
61 |
for (String pkg : packages) {
|
49451
|
62 |
boolean opened = isOpenTo(jvmci, pkg, otherModule);
|
46459
|
63 |
if (!opened) {
|
49451
|
64 |
try {
|
|
65 |
addOpens.invoke(jvmci, pkg, otherModule);
|
|
66 |
} catch (Throwable throwable) {
|
|
67 |
throw new InternalError(throwable);
|
|
68 |
}
|
46459
|
69 |
}
|
|
70 |
}
|
|
71 |
}
|
|
72 |
}
|
43972
|
73 |
|
|
74 |
/**
|
|
75 |
* Gets an {@link Iterable} of the providers available for a given service.
|
|
76 |
*
|
|
77 |
* @throws SecurityException if on JDK8 and a security manager is present and it denies
|
|
78 |
* {@link JVMCIPermission}
|
|
79 |
*/
|
|
80 |
public static <S> Iterable<S> load(Class<S> service) {
|
|
81 |
assert !service.getName().startsWith("jdk.vm.ci") : "JVMCI services must be loaded via " + Services.class.getName();
|
|
82 |
if (Java8OrEarlier) {
|
46459
|
83 |
return load8(service);
|
43972
|
84 |
}
|
46459
|
85 |
Iterable<S> iterable = ServiceLoader.load(service);
|
43972
|
86 |
return new Iterable<S>() {
|
|
87 |
@Override
|
|
88 |
public Iterator<S> iterator() {
|
|
89 |
Iterator<S> iterator = iterable.iterator();
|
|
90 |
return new Iterator<S>() {
|
|
91 |
@Override
|
|
92 |
public boolean hasNext() {
|
|
93 |
return iterator.hasNext();
|
|
94 |
}
|
|
95 |
|
|
96 |
@Override
|
|
97 |
public S next() {
|
|
98 |
S provider = iterator.next();
|
46459
|
99 |
// Allow Graal extensions to access JVMCI
|
|
100 |
openJVMCITo(provider.getClass());
|
43972
|
101 |
return provider;
|
|
102 |
}
|
|
103 |
|
|
104 |
@Override
|
|
105 |
public void remove() {
|
|
106 |
iterator.remove();
|
|
107 |
}
|
|
108 |
};
|
|
109 |
}
|
|
110 |
};
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
46459
|
114 |
* {@code Services.load(Class)} is only defined in JVMCI-8.
|
|
115 |
*/
|
|
116 |
private static volatile Method loadMethod;
|
|
117 |
|
|
118 |
@SuppressWarnings("unchecked")
|
|
119 |
private static <S> Iterable<S> load8(Class<S> service) throws InternalError {
|
|
120 |
try {
|
|
121 |
if (loadMethod == null) {
|
|
122 |
loadMethod = Services.class.getMethod("load", Class.class);
|
|
123 |
}
|
|
124 |
return (Iterable<S>) loadMethod.invoke(null, service);
|
|
125 |
} catch (Exception e) {
|
|
126 |
throw new InternalError(e);
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
43972
|
131 |
* Gets the provider for a given service for which at most one provider must be available.
|
|
132 |
*
|
|
133 |
* @param service the service whose provider is being requested
|
|
134 |
* @param required specifies if an {@link InternalError} should be thrown if no provider of
|
|
135 |
* {@code service} is available
|
|
136 |
* @return the requested provider if available else {@code null}
|
|
137 |
* @throws SecurityException if on JDK8 and a security manager is present and it denies
|
|
138 |
* {@link JVMCIPermission}
|
|
139 |
*/
|
|
140 |
public static <S> S loadSingle(Class<S> service, boolean required) {
|
|
141 |
assert !service.getName().startsWith("jdk.vm.ci") : "JVMCI services must be loaded via " + Services.class.getName();
|
46459
|
142 |
Iterable<S> providers = load(service);
|
43972
|
143 |
S singleProvider = null;
|
|
144 |
try {
|
|
145 |
for (Iterator<S> it = providers.iterator(); it.hasNext();) {
|
|
146 |
singleProvider = it.next();
|
|
147 |
if (it.hasNext()) {
|
46459
|
148 |
S other = it.next();
|
|
149 |
throw new InternalError(String.format("Multiple %s providers found: %s, %s", service.getName(), singleProvider.getClass().getName(), other.getClass().getName()));
|
43972
|
150 |
}
|
|
151 |
}
|
|
152 |
} catch (ServiceConfigurationError e) {
|
|
153 |
// If the service is required we will bail out below.
|
|
154 |
}
|
|
155 |
if (singleProvider == null) {
|
|
156 |
if (required) {
|
|
157 |
throw new InternalError(String.format("No provider for %s found", service.getName()));
|
|
158 |
}
|
|
159 |
}
|
|
160 |
return singleProvider;
|
|
161 |
}
|
|
162 |
}
|