--- /dev/null 2017-01-22 10:16:57.869617664 -0800 +++ new/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.serviceprovider/src/org/graalvm/compiler/serviceprovider/GraalServices.java 2017-02-15 17:09:32.039223967 -0800 @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.graalvm.compiler.serviceprovider; + +import java.util.Iterator; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; + +import jdk.vm.ci.services.JVMCIPermission; +import jdk.vm.ci.services.Services; + +/** + * A mechanism for accessing service providers that abstracts over whether Graal is running on + * JVMCI-8 or JVMCI-9. In JVMCI-8, a JVMCI specific mechanism is used to lookup services via the + * hidden JVMCI class loader. in JVMCI-9, the standard {@link ServiceLoader} mechanism is used. + */ +public final class GraalServices { + + private GraalServices() { + } + + public static final boolean Java8OrEarlier = System.getProperty("java.specification.version").compareTo("1.9") < 0; + + /** + * Gets an {@link Iterable} of the providers available for a given service. + * + * @throws SecurityException if on JDK8 and a security manager is present and it denies + * {@link JVMCIPermission} + */ + public static Iterable load(Class service) { + assert !service.getName().startsWith("jdk.vm.ci") : "JVMCI services must be loaded via " + Services.class.getName(); + if (Java8OrEarlier) { + return Services.load(service); + } + ServiceLoader iterable = ServiceLoader.load(service); + return new Iterable() { + @Override + public Iterator iterator() { + Iterator iterator = iterable.iterator(); + return new Iterator() { + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public S next() { + S provider = iterator.next(); + // Allow Graal extensions to access JVMCI assuming they have JVMCIPermission + Services.exportJVMCITo(provider.getClass()); + return provider; + } + + @Override + public void remove() { + iterator.remove(); + } + }; + } + }; + } + + /** + * Gets the provider for a given service for which at most one provider must be available. + * + * @param service the service whose provider is being requested + * @param required specifies if an {@link InternalError} should be thrown if no provider of + * {@code service} is available + * @return the requested provider if available else {@code null} + * @throws SecurityException if on JDK8 and a security manager is present and it denies + * {@link JVMCIPermission} + */ + public static S loadSingle(Class service, boolean required) { + assert !service.getName().startsWith("jdk.vm.ci") : "JVMCI services must be loaded via " + Services.class.getName(); + if (Java8OrEarlier) { + return Services.loadSingle(service, required); + } + Iterable providers = ServiceLoader.load(service); + S singleProvider = null; + try { + for (Iterator it = providers.iterator(); it.hasNext();) { + singleProvider = it.next(); + if (it.hasNext()) { + throw new InternalError(String.format("Multiple %s providers found", service.getName())); + } + } + } catch (ServiceConfigurationError e) { + // If the service is required we will bail out below. + } + if (singleProvider == null) { + if (required) { + throw new InternalError(String.format("No provider for %s found", service.getName())); + } + } else { + // Allow Graal extensions to access JVMCI assuming they have JVMCIPermission + Services.exportJVMCITo(singleProvider.getClass()); + } + return singleProvider; + } +}