< prev index next >

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

Print this page




   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.lang.reflect.InvocationTargetException;
  26 import java.lang.reflect.Method;
  27 import java.util.Formatter;
  28 import java.util.Iterator;

  29 import java.util.ServiceConfigurationError;
  30 import java.util.ServiceLoader;
  31 import java.util.Set;
  32 
  33 /**
  34  * A mechanism for accessing service providers via JVMCI.


  35  */
  36 public final class Services {
  37 
  38     private Services() {
  39     }
  40 































  41     private static int getJavaSpecificationVersion() {
  42         String value = System.getProperty("java.specification.version");
  43         if (value.startsWith("1.")) {
  44             value = value.substring(2);
  45         }
  46         return Integer.parseInt(value);
  47     }
  48 
  49     /**
  50      * The integer value corresponding to the value of the {@code java.specification.version} system
  51      * property after any leading {@code "1."} has been stripped.
  52      */
  53     public static final int JAVA_SPECIFICATION_VERSION = getJavaSpecificationVersion();
  54 
  55     // Use reflection so that this compiles on Java 8
  56     private static final Method getModule;
  57     private static final Method getPackages;
  58     private static final Method addUses;
  59     private static final Method isExported;
  60     private static final Method addExports;


 168             for (Iterator<S> it = providers.iterator(); it.hasNext();) {
 169                 singleProvider = it.next();
 170                 if (it.hasNext()) {
 171                     throw new InternalError(String.format("Multiple %s providers found", service.getName()));
 172                 }
 173             }
 174         } catch (ServiceConfigurationError e) {
 175             // If the service is required we will bail out below.
 176         }
 177         if (singleProvider == null && required) {
 178             String javaHome = System.getProperty("java.home");
 179             String vmName = System.getProperty("java.vm.name");
 180             Formatter errorMessage = new Formatter();
 181             errorMessage.format("The VM does not expose required service %s.%n", service.getName());
 182             errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
 183             errorMessage.format("Currently used VM configuration is: %s", vmName);
 184             throw new UnsupportedOperationException(errorMessage.toString());
 185         }
 186         return singleProvider;
 187     }

 188 }


   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.lang.reflect.InvocationTargetException;
  26 import java.lang.reflect.Method;
  27 import java.util.Formatter;
  28 import java.util.Iterator;
  29 import java.util.Map;
  30 import java.util.ServiceConfigurationError;
  31 import java.util.ServiceLoader;
  32 import java.util.Set;
  33 
  34 /**
  35  * Provides utilities needed by JVMCI clients.
  36  *
  37  * This class must be compilable on JDK 8 and so use of API added in JDK 9 is made via reflection.
  38  */
  39 public final class Services {
  40 
  41     private Services() {
  42     }
  43 
  44     /**
  45      * Gets an unmodifiable copy of the system properties saved when {@link System} is initialized.
  46      */
  47     @SuppressWarnings("unchecked")
  48     public static Map<String, String> getSavedProperties() {
  49         SecurityManager sm = System.getSecurityManager();
  50         if (sm != null) {
  51             sm.checkPermission(new JVMCIPermission());
  52         }
  53         try {
  54             Class<?> vmClass = Class.forName("jdk.internal.misc.VM");
  55             Method m = vmClass.getMethod("getSavedProperties");
  56             return (Map<String, String>) m.invoke(null);
  57         } catch (Exception e) {
  58             throw new InternalError(e);
  59         }
  60     }
  61 
  62     /**
  63      * Causes the JVMCI subsystem to be initialized if it isn't already initialized.
  64      */
  65     public static void initializeJVMCI() {
  66         try {
  67             Class.forName("jdk.vm.ci.runtime.JVMCI");
  68         } catch (ClassNotFoundException e) {
  69             throw new InternalError(e);
  70         }
  71     }
  72 
  73     // EVERYTHING BELOW HERE TO BE REMOVED AFTER NECESSARY GRAAL UPDATE
  74 
  75     private static int getJavaSpecificationVersion() {
  76         String value = System.getProperty("java.specification.version");
  77         if (value.startsWith("1.")) {
  78             value = value.substring(2);
  79         }
  80         return Integer.parseInt(value);
  81     }
  82 
  83     /**
  84      * The integer value corresponding to the value of the {@code java.specification.version} system
  85      * property after any leading {@code "1."} has been stripped.
  86      */
  87     public static final int JAVA_SPECIFICATION_VERSION = getJavaSpecificationVersion();
  88 
  89     // Use reflection so that this compiles on Java 8
  90     private static final Method getModule;
  91     private static final Method getPackages;
  92     private static final Method addUses;
  93     private static final Method isExported;
  94     private static final Method addExports;


 202             for (Iterator<S> it = providers.iterator(); it.hasNext();) {
 203                 singleProvider = it.next();
 204                 if (it.hasNext()) {
 205                     throw new InternalError(String.format("Multiple %s providers found", service.getName()));
 206                 }
 207             }
 208         } catch (ServiceConfigurationError e) {
 209             // If the service is required we will bail out below.
 210         }
 211         if (singleProvider == null && required) {
 212             String javaHome = System.getProperty("java.home");
 213             String vmName = System.getProperty("java.vm.name");
 214             Formatter errorMessage = new Formatter();
 215             errorMessage.format("The VM does not expose required service %s.%n", service.getName());
 216             errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
 217             errorMessage.format("Currently used VM configuration is: %s", vmName);
 218             throw new UnsupportedOperationException(errorMessage.toString());
 219         }
 220         return singleProvider;
 221     }
 222 
 223 }
< prev index next >