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.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; 95 96 static { 97 if (JAVA_SPECIFICATION_VERSION >= 9) { 98 try { 99 getModule = Class.class.getMethod("getModule"); 100 Class<?> moduleClass = getModule.getReturnType(); 101 getPackages = moduleClass.getMethod("getPackages"); 102 addUses = moduleClass.getMethod("addUses", Class.class); 103 isExported = moduleClass.getMethod("isExported", String.class, moduleClass); 104 addExports = moduleClass.getMethod("addExports", String.class, moduleClass); 105 } catch (NoSuchMethodException | SecurityException e) { 106 throw new InternalError(e); 107 } 108 } else { 109 getModule = null; 110 getPackages = null; 111 addUses = null; 112 isExported = null; 113 addExports = null; 114 } 115 } 116 117 @SuppressWarnings("unchecked") 118 static <T> T invoke(Method method, Object receiver, Object... args) { 119 try { 120 return (T) method.invoke(receiver, args); 121 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 122 throw new InternalError(e); 123 } 124 } 125 126 /** 127 * Performs any required security checks and dynamic reconfiguration to allow the module of a 128 * given class to access the classes in the JVMCI module. 129 * 130 * Note: This API uses {@link Class} instead of {@code Module} to provide backwards 131 * compatibility for JVMCI clients compiled against a JDK release earlier than 9. 132 * 133 * @param requestor a class requesting access to the JVMCI module for its module 134 * @throws SecurityException if a security manager is present and it denies 135 * {@link JVMCIPermission} 136 */ 137 public static void exportJVMCITo(Class<?> requestor) { 138 SecurityManager sm = System.getSecurityManager(); 139 if (sm != null) { 140 sm.checkPermission(new JVMCIPermission()); 141 } 142 if (JAVA_SPECIFICATION_VERSION >= 9) { 143 Object jvmci = invoke(getModule, Services.class); 144 Object requestorModule = invoke(getModule, requestor); 145 if (jvmci != requestorModule) { 146 Set<String> packages = invoke(getPackages, jvmci); 147 for (String pkg : packages) { 148 // Export all JVMCI packages dynamically instead 149 // of requiring a long list of --add-exports 150 // options on the JVM command line. 151 boolean exported = invoke(isExported, jvmci, pkg, requestorModule); 152 if (!exported) { 153 invoke(addExports, jvmci, pkg, requestorModule); 154 } 155 } 156 } 157 } 158 } 159 160 /** 161 * Gets an {@link Iterable} of the JVMCI providers available for a given service. 162 * 163 * @throws SecurityException if a security manager is present and it denies 164 * {@link JVMCIPermission} 165 */ 166 public static <S> Iterable<S> load(Class<S> service) { 167 SecurityManager sm = System.getSecurityManager(); 168 if (sm != null) { 169 sm.checkPermission(new JVMCIPermission()); 170 } 171 if (JAVA_SPECIFICATION_VERSION >= 9) { 172 Object jvmci = invoke(getModule, Services.class); 173 invoke(addUses, jvmci, service); 174 } 175 176 // Restrict JVMCI clients to be on the class path or module path 177 return ServiceLoader.load(service, ClassLoader.getSystemClassLoader()); 178 } 179 180 /** 181 * Gets the JVMCI provider for a given service for which at most one provider must be available. 182 * 183 * @param service the service whose provider is being requested 184 * @param required specifies if an {@link InternalError} should be thrown if no provider of 185 * {@code service} is available 186 * @throws SecurityException if a security manager is present and it denies 187 * {@link JVMCIPermission} 188 */ 189 public static <S> S loadSingle(Class<S> service, boolean required) { 190 SecurityManager sm = System.getSecurityManager(); 191 if (sm != null) { 192 sm.checkPermission(new JVMCIPermission()); 193 } 194 if (JAVA_SPECIFICATION_VERSION >= 9) { 195 Object jvmci = invoke(getModule, Services.class); 196 invoke(addUses, jvmci, service); 197 } 198 // Restrict JVMCI clients to be on the class path or module path 199 Iterable<S> providers = ServiceLoader.load(service, ClassLoader.getSystemClassLoader()); 200 S singleProvider = null; 201 try { 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 }