comparison jvmci/jdk.vm.ci.options/src/jdk/vm/ci/options/JVMCIJarsOptionDescriptorsProvider.java @ 22672:1bbd4a7c274b

Rename jdk.internal.jvmci to jdk.vm.ci
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Thu, 08 Oct 2015 17:28:41 -0700
parents jvmci/jdk.internal.jvmci.options/src/jdk/internal/jvmci/options/JVMCIJarsOptionDescriptorsProvider.java@5cd42bb63fad
children
comparison
equal deleted inserted replaced
22671:97f30e4d0e95 22672:1bbd4a7c274b
1 /*
2 * Copyright (c) 2014, 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.BufferedReader;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.jar.JarFile;
34 import java.util.zip.ZipEntry;
35
36 import jdk.vm.ci.options.OptionsParser.OptionDescriptorsProvider;
37
38 /**
39 * Access to the {@link OptionDescriptors} declared by
40 * {@code META-INF/services/jdk.vm.ci.options.OptionDescriptors} files in
41 * {@code <jre>/lib/jvmci/*.jar}.
42 */
43 final class JVMCIJarsOptionDescriptorsProvider implements OptionDescriptorsProvider {
44
45 static final String OptionDescriptorsServiceFile = "META-INF/services/" + OptionDescriptors.class.getName();
46
47 private final Iterator<File> jars;
48 private final List<OptionDescriptors> optionsDescriptorsList;
49
50 /**
51 * Creates a {@link JVMCIJarsOptionDescriptorsProvider} if at least one JVMCI jar is available
52 * otherwise returns null.
53 */
54 static JVMCIJarsOptionDescriptorsProvider create() {
55 List<File> jarsList = findJVMCIJars();
56 if (jarsList.isEmpty()) {
57 return null;
58 }
59 return new JVMCIJarsOptionDescriptorsProvider(jarsList);
60 }
61
62 private JVMCIJarsOptionDescriptorsProvider(List<File> jarsList) {
63 this.jars = jarsList.iterator();
64 this.optionsDescriptorsList = new ArrayList<>(jarsList.size() * 3);
65 }
66
67 /**
68 * Finds the list of JVMCI jars.
69 */
70 private static List<File> findJVMCIJars() {
71 File javaHome = new File(System.getProperty("java.home"));
72 File lib = new File(javaHome, "lib");
73 File jvmci = new File(lib, "jvmci");
74 if (!jvmci.exists()) {
75 return Collections.emptyList();
76 }
77
78 List<File> jarFiles = new ArrayList<>();
79 for (String fileName : jvmci.list()) {
80 if (fileName.endsWith(".jar")) {
81 File file = new File(jvmci, fileName);
82 if (file.isDirectory()) {
83 continue;
84 }
85 jarFiles.add(file);
86 }
87 }
88 return jarFiles;
89 }
90
91 public OptionDescriptor get(String name) {
92 // Look up loaded option descriptors first
93 for (OptionDescriptors optionDescriptors : optionsDescriptorsList) {
94 OptionDescriptor desc = optionDescriptors.get(name);
95 if (desc != null) {
96 return desc;
97 }
98 }
99 while (jars.hasNext()) {
100 File path = jars.next();
101 try (JarFile jar = new JarFile(path)) {
102 ZipEntry entry = jar.getEntry(OptionDescriptorsServiceFile);
103 if (entry != null) {
104 BufferedReader br = new BufferedReader(new InputStreamReader(jar.getInputStream(entry)));
105 String line = null;
106 OptionDescriptor desc = null;
107 while ((line = br.readLine()) != null) {
108 OptionDescriptors options;
109 try {
110 options = (OptionDescriptors) Class.forName(line).newInstance();
111 optionsDescriptorsList.add(options);
112 if (desc == null) {
113 desc = options.get(name);
114 }
115 } catch (Exception e) {
116 throw new InternalError("Error instantiating class " + line + " read from " + path, e);
117 }
118 }
119 if (desc != null) {
120 return desc;
121 }
122 }
123 } catch (IOException e) {
124 throw new InternalError("Error reading " + path, e);
125 }
126 }
127 return null;
128 }
129 }