1 /* 2 * Copyright (c) 2014, 2015, 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.options; 24 25 import java.io.*; 26 import java.util.*; 27 import java.util.jar.*; 28 import java.util.zip.*; 29 30 import jdk.vm.ci.options.OptionsParser.*; 31 32 /** 33 * Access to the {@link OptionDescriptors} declared by 34 * {@code META-INF/services/jdk.vm.ci.options.OptionDescriptors} files in {@code 35 * <jre>/lib/jvmci/*.jar}. 36 */ 37 class JVMCIJarsOptionDescriptorsProvider implements OptionDescriptorsProvider { 38 39 static final String OptionDescriptorsServiceFile = "META-INF/services/" + OptionDescriptors.class.getName(); 40 41 private final Iterator<File> jars; 42 private final List<OptionDescriptors> optionsDescriptorsList; 43 44 JVMCIJarsOptionDescriptorsProvider() { 45 List<File> jarsList = findJVMCIJars(); 46 this.jars = jarsList.iterator(); 47 this.optionsDescriptorsList = new ArrayList<>(jarsList.size() * 3); 48 } 49 50 /** 51 * Finds the list of JVMCI jars. 52 */ 53 private static List<File> findJVMCIJars() { 54 File javaHome = new File(System.getProperty("java.home")); 55 File lib = new File(javaHome, "lib"); 56 File jvmci = new File(lib, "jvmci"); 57 58 List<File> jarFiles = new ArrayList<>(); 59 if (jvmci.exists()) { 60 for (String fileName : jvmci.list()) { 61 if (fileName.endsWith(".jar")) { 62 File file = new File(jvmci, fileName); 63 if (file.isDirectory()) { 64 continue; 65 } 66 jarFiles.add(file); 67 } 68 } 69 } 70 return jarFiles; 71 } 72 73 public OptionDescriptor get(String name) { 74 // Look up loaded option descriptors first 75 for (OptionDescriptors optionDescriptors : optionsDescriptorsList) { 76 OptionDescriptor desc = optionDescriptors.get(name); 77 if (desc != null) { 78 return desc; 79 } 80 } 81 while (jars.hasNext()) { 82 File path = jars.next(); 83 try (JarFile jar = new JarFile(path)) { 84 ZipEntry entry = jar.getEntry(OptionDescriptorsServiceFile); 85 if (entry != null) { 86 BufferedReader br = new BufferedReader(new InputStreamReader(jar.getInputStream(entry))); 87 String line = null; 88 OptionDescriptor desc = null; 89 while ((line = br.readLine()) != null) { 90 OptionDescriptors options; 91 try { 92 options = (OptionDescriptors) Class.forName(line).newInstance(); 93 optionsDescriptorsList.add(options); 94 if (desc == null) { 95 desc = options.get(name); 96 } 97 } catch (Exception e) { 98 throw new InternalError("Error instantiating class " + line + " read from " + path, e); 99 } 100 } 101 if (desc != null) { 102 return desc; 103 } 104 } 105 } catch (IOException e) { 106 throw new InternalError("Error reading " + path, e); 107 } 108 } 109 return null; 110 } 111 }