001/* 002 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.api.runtime; 024 025import java.util.*; 026 027import jdk.internal.jvmci.service.*; 028import sun.reflect.*; 029 030/** 031 * Access point for {@linkplain #getRuntime() retrieving} the single {@link GraalRuntime} instance. 032 */ 033public class Graal { 034 035 private static final GraalRuntime runtime = initializeRuntime(); 036 037 private static GraalRuntime initializeRuntime() { 038 GraalRuntimeAccess access = Services.loadSingle(GraalRuntimeAccess.class, false); 039 if (access != null) { 040 GraalRuntime rt = access.getRuntime(); 041 assert rt != null; 042 return rt; 043 } 044 return new InvalidGraalRuntime(); 045 } 046 047 /** 048 * Gets the singleton {@link GraalRuntime} instance available to the application. 049 */ 050 public static GraalRuntime getRuntime() { 051 return runtime; 052 } 053 054 /** 055 * Gets a capability provided by the {@link GraalRuntime} instance available to the application. 056 * 057 * @throws UnsupportedOperationException if the capability is not available 058 */ 059 @CallerSensitive 060 public static <T> T getRequiredCapability(Class<T> clazz) { 061 T t = getRuntime().getCapability(clazz); 062 if (t == null) { 063 String javaHome = System.getProperty("java.home"); 064 String vmName = System.getProperty("java.vm.name"); 065 Formatter errorMessage = new Formatter(); 066 if (runtime.getClass() == InvalidGraalRuntime.class) { 067 errorMessage.format("The VM does not support the Graal API.%n"); 068 } else { 069 errorMessage.format("The VM does not expose required Graal capability %s.%n", clazz.getName()); 070 } 071 errorMessage.format("Currently used Java home directory is %s.%n", javaHome); 072 errorMessage.format("Currently used VM configuration is: %s", vmName); 073 throw new UnsupportedOperationException(errorMessage.toString()); 074 } 075 return t; 076 } 077 078 private static final class InvalidGraalRuntime implements GraalRuntime { 079 080 @Override 081 public String getName() { 082 return ""; 083 } 084 085 @Override 086 public <T> T getCapability(Class<T> clazz) { 087 return null; 088 } 089 } 090}