1 /* 2 * Copyright (c) 2014, 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.lang.reflect.Method; 26 import java.util.Map; 27 28 /** 29 * Provides utilities needed by JVMCI clients. 30 */ 31 public final class Services { 32 33 // This class must be compilable and executable on JDK 8 since it's used in annotation 34 // processors while building JDK 9 so use of API added in JDK 9 is made via reflection. 35 36 private Services() { 37 } 38 39 @SuppressWarnings("unchecked") 40 private static Map<String, String> initSavedProperties() throws InternalError { 41 try { 42 Class<?> vmClass = Class.forName("jdk.internal.misc.VM"); 43 Method m = vmClass.getMethod("getSavedProperties"); 44 return (Map<String, String>) m.invoke(null); 45 } catch (Exception e) { 46 throw new InternalError(e); 47 } 48 } 49 50 static final Map<String, String> SAVED_PROPERTIES = initSavedProperties(); 51 static final boolean JVMCI_ENABLED = Boolean.parseBoolean(SAVED_PROPERTIES.get("jdk.internal.vm.ci.enabled")); 52 53 /** 54 * Checks that JVMCI is enabled in the VM and throws an error if it isn't. 55 */ 56 static void checkJVMCIEnabled() { 57 if (!JVMCI_ENABLED) { 58 throw new Error("The EnableJVMCI VM option must be true (i.e., -XX:+EnableJVMCI) to use JVMCI"); 59 } 60 } 61 62 /** 63 * Gets an unmodifiable copy of the system properties saved when {@link System} is initialized. 64 */ 65 public static Map<String, String> getSavedProperties() { 66 checkJVMCIEnabled(); 67 SecurityManager sm = System.getSecurityManager(); 68 if (sm != null) { 69 sm.checkPermission(new JVMCIPermission()); 70 } 71 return SAVED_PROPERTIES; 72 } 73 74 /** 75 * Causes the JVMCI subsystem to be initialized if it isn't already initialized. 76 */ 77 public static void initializeJVMCI() { 78 checkJVMCIEnabled(); 79 try { 80 Class.forName("jdk.vm.ci.runtime.JVMCI"); 81 } catch (ClassNotFoundException e) { 82 throw new InternalError(e); 83 } 84 } 85 }