1 /*
   2  * Copyright (c) 2016, 2018, 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 jdk.vm.ci.services;
  24 
  25 import java.util.ArrayList;
  26 import java.util.List;
  27 import java.util.ServiceLoader;
  28 
  29 /**
  30  * Service-provider class for the runtime to locate providers of JVMCI services where the latter are
  31  * not in packages exported by the JVMCI module. As part of instantiating a
  32  * {@link JVMCIServiceLocator}, all JVMCI packages will be opened to the module defining the class
  33  * of the instantiated object.
  34  *
  35  * While the {@link #getProvider(Class)} method can be used directly, it's usually easier to use
  36  * {@link #getProviders(Class)}.
  37  */
  38 public abstract class JVMCIServiceLocator {
  39 
  40     private static Void checkPermission() {
  41         SecurityManager sm = System.getSecurityManager();
  42         if (sm != null) {
  43             sm.checkPermission(new JVMCIPermission());
  44         }
  45         return null;
  46     }
  47 
  48     @SuppressWarnings("unused")
  49     private JVMCIServiceLocator(Void ignore) {
  50     }
  51 
  52     /**
  53      * Creates a capability for accessing JVMCI. Once successfully instantiated, JVMCI opens all its
  54      * packages to the module defining the type of this object.
  55      *
  56      * @throws SecurityException if a security manager has been installed and it denies
  57      *             {@link JVMCIPermission}
  58      */
  59     protected JVMCIServiceLocator() {
  60         this(checkPermission());
  61         Services.checkJVMCIEnabled();
  62         Services.openJVMCITo(getClass().getModule());
  63     }
  64 
  65     /**
  66      * Gets the provider of the service defined by {@code service} or {@code null} if this object
  67      * does not have a provider for {@code service}.
  68      */
  69     protected abstract <S> S getProvider(Class<S> service);
  70 
  71     /**
  72      * Gets the providers of the service defined by {@code service} by querying the available
  73      * {@link JVMCIServiceLocator} providers.
  74      *
  75      * @throws SecurityException if a security manager is present and it denies
  76      *             {@link JVMCIPermission}
  77      */
  78     public static <S> List<S> getProviders(Class<S> service) {
  79         Services.checkJVMCIEnabled();
  80         SecurityManager sm = System.getSecurityManager();
  81         if (sm != null) {
  82             sm.checkPermission(new JVMCIPermission());
  83         }
  84         List<S> providers = new ArrayList<>();
  85         for (JVMCIServiceLocator access : ServiceLoader.load(JVMCIServiceLocator.class, ClassLoader.getSystemClassLoader())) {
  86             S provider = access.getProvider(service);
  87             if (provider != null) {
  88                 providers.add(provider);
  89             }
  90         }
  91         return providers;
  92     }
  93 }