< prev index next >

src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/src/jdk/vm/ci/services/JVMCIServiceLocator.java

Print this page




   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 }


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