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 jdk.vm.ci.services;
  24 
  25 import java.util.ArrayList;
  26 import java.util.List;



  27 
  28 /**
  29  * Service-provider class for the runtime to locate providers of JVMCI services where the latter are
  30  * not in packages exported by the JVMCI module. As part of instantiating
  31  * {@link JVMCIServiceLocator}, all JVMCI packages will be {@linkplain Services#exportJVMCITo(Class)
  32  * exported} to the module defining the class of the instantiated object.
  33  *
  34  * While the {@link #getProvider(Class)} method can be used directly, it's usually easier to use
  35  * {@link #getProviders(Class)}.
  36  */
  37 public abstract class JVMCIServiceLocator {
  38 
  39     private static Void checkPermission() {
  40         SecurityManager sm = System.getSecurityManager();
  41         if (sm != null) {
  42             sm.checkPermission(new JVMCIPermission());
  43         }
  44         return null;
  45     }
  46 
  47     @SuppressWarnings("unused")
  48     private JVMCIServiceLocator(Void ignore) {
  49     }
  50 
  51     /**
  52      * Creates a capability for accessing JVMCI. Once successfully instantiated, JVMCI exports all
  53      * its packages to the module defining the type of this object.
  54      *
  55      * @throws SecurityException if a security manager has been installed and it denies
  56      *             {@link JVMCIPermission}
  57      */
  58     protected JVMCIServiceLocator() {
  59         this(checkPermission());
  60         Services.exportJVMCITo(getClass());
  61     }
  62 
  63     /**
  64      * Gets the provider of the service defined by {@code service} or {@code null} if this object
  65      * does not have a provider for {@code service}.
  66      */
  67     public abstract <S> S getProvider(Class<S> service);
  68 
  69     /**
  70      * Gets the providers of the service defined by {@code service} by querying the
  71      * {@link JVMCIServiceLocator} providers obtained by {@link Services#load(Class)}.



  72      */
  73     public static <S> List<S> getProviders(Class<S> service) {




  74         List<S> providers = new ArrayList<>();
  75         for (JVMCIServiceLocator access : Services.load(JVMCIServiceLocator.class)) {
  76             S provider = access.getProvider(service);
  77             if (provider != null) {
  78                 providers.add(provider);
  79             }
  80         }
  81         return providers;
  82     }
  83 }
--- EOF ---