--- /dev/null 2017-01-22 10:16:57.869617664 -0800 +++ new/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/src/jdk/vm/ci/services/JVMCIServiceLocator.java 2017-02-15 16:56:20.347679898 -0800 @@ -0,0 +1,83 @@ +/* + * 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 jdk.vm.ci.services; + +import java.util.ArrayList; +import java.util.List; + +/** + * Service-provider class for the runtime to locate providers of JVMCI services where the latter are + * not in packages exported by the JVMCI module. As part of instantiating + * {@link JVMCIServiceLocator}, all JVMCI packages will be {@linkplain Services#exportJVMCITo(Class) + * exported} to the module defining the class of the instantiated object. + * + * While the {@link #getProvider(Class)} method can be used directly, it's usually easier to use + * {@link #getProviders(Class)}. + */ +public abstract class JVMCIServiceLocator { + + private static Void checkPermission() { + SecurityManager sm = System.getSecurityManager(); + if (sm != null) { + sm.checkPermission(new JVMCIPermission()); + } + return null; + } + + @SuppressWarnings("unused") + private JVMCIServiceLocator(Void ignore) { + } + + /** + * Creates a capability for accessing JVMCI. Once successfully instantiated, JVMCI exports all + * its packages to the module defining the type of this object. + * + * @throws SecurityException if a security manager has been installed and it denies + * {@link JVMCIPermission} + */ + protected JVMCIServiceLocator() { + this(checkPermission()); + Services.exportJVMCITo(getClass()); + } + + /** + * Gets the provider of the service defined by {@code service} or {@code null} if this object + * does not have a provider for {@code service}. + */ + public abstract S getProvider(Class service); + + /** + * Gets the providers of the service defined by {@code service} by querying the + * {@link JVMCIServiceLocator} providers obtained by {@link Services#load(Class)}. + */ + public static List getProviders(Class service) { + List providers = new ArrayList<>(); + for (JVMCIServiceLocator access : Services.load(JVMCIServiceLocator.class)) { + S provider = access.getProvider(service); + if (provider != null) { + providers.add(provider); + } + } + return providers; + } +}