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 org.graalvm.compiler.serviceprovider;
  24 
  25 import java.util.Iterator;
  26 import java.util.ServiceConfigurationError;
  27 import java.util.ServiceLoader;
  28 
  29 import jdk.vm.ci.services.JVMCIPermission;
  30 import jdk.vm.ci.services.Services;
  31 
  32 /**
  33  * A mechanism for accessing service providers that abstracts over whether Graal is running on
  34  * JVMCI-8 or JVMCI-9. In JVMCI-8, a JVMCI specific mechanism is used to lookup services via the
  35  * hidden JVMCI class loader. in JVMCI-9, the standard {@link ServiceLoader} mechanism is used.
  36  */
  37 public final class GraalServices {
  38 
  39     private GraalServices() {
  40     }
  41 
  42     public static final boolean Java8OrEarlier = System.getProperty("java.specification.version").compareTo("1.9") < 0;
  43 
  44     /**
  45      * Gets an {@link Iterable} of the providers available for a given service.
  46      *
  47      * @throws SecurityException if on JDK8 and a security manager is present and it denies
  48      *             {@link JVMCIPermission}
  49      */
  50     public static <S> Iterable<S> load(Class<S> service) {
  51         assert !service.getName().startsWith("jdk.vm.ci") : "JVMCI services must be loaded via " + Services.class.getName();
  52         if (Java8OrEarlier) {
  53             return Services.load(service);
  54         }
  55         ServiceLoader<S> iterable = ServiceLoader.load(service);
  56         return new Iterable<S>() {
  57             @Override
  58             public Iterator<S> iterator() {
  59                 Iterator<S> iterator = iterable.iterator();
  60                 return new Iterator<S>() {
  61                     @Override
  62                     public boolean hasNext() {
  63                         return iterator.hasNext();
  64                     }
  65 
  66                     @Override
  67                     public S next() {
  68                         S provider = iterator.next();
  69                         // Allow Graal extensions to access JVMCI assuming they have JVMCIPermission
  70                         Services.exportJVMCITo(provider.getClass());
  71                         return provider;
  72                     }
  73 
  74                     @Override
  75                     public void remove() {
  76                         iterator.remove();
  77                     }
  78                 };
  79             }
  80         };
  81     }
  82 
  83     /**
  84      * Gets the provider for a given service for which at most one provider must be available.
  85      *
  86      * @param service the service whose provider is being requested
  87      * @param required specifies if an {@link InternalError} should be thrown if no provider of
  88      *            {@code service} is available
  89      * @return the requested provider if available else {@code null}
  90      * @throws SecurityException if on JDK8 and a security manager is present and it denies
  91      *             {@link JVMCIPermission}
  92      */
  93     public static <S> S loadSingle(Class<S> service, boolean required) {
  94         assert !service.getName().startsWith("jdk.vm.ci") : "JVMCI services must be loaded via " + Services.class.getName();
  95         if (Java8OrEarlier) {
  96             return Services.loadSingle(service, required);
  97         }
  98         Iterable<S> providers = ServiceLoader.load(service);
  99         S singleProvider = null;
 100         try {
 101             for (Iterator<S> it = providers.iterator(); it.hasNext();) {
 102                 singleProvider = it.next();
 103                 if (it.hasNext()) {
 104                     throw new InternalError(String.format("Multiple %s providers found", service.getName()));
 105                 }
 106             }
 107         } catch (ServiceConfigurationError e) {
 108             // If the service is required we will bail out below.
 109         }
 110         if (singleProvider == null) {
 111             if (required) {
 112                 throw new InternalError(String.format("No provider for %s found", service.getName()));
 113             }
 114         } else {
 115             // Allow Graal extensions to access JVMCI assuming they have JVMCIPermission
 116             Services.exportJVMCITo(singleProvider.getClass());
 117         }
 118         return singleProvider;
 119     }
 120 }