JDKPlatformProvider.java revision 2973:0e8fa3249327
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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25package com.sun.tools.javac.platform; 26 27import java.io.IOException; 28import java.nio.file.DirectoryStream; 29import java.nio.file.FileSystem; 30import java.nio.file.FileSystems; 31import java.nio.file.Files; 32import java.nio.file.Path; 33import java.nio.file.Paths; 34import java.util.ArrayList; 35import java.util.Collection; 36import java.util.Collections; 37import java.util.HashMap; 38import java.util.List; 39import java.util.Map; 40import java.util.Set; 41import java.util.TreeSet; 42 43import javax.annotation.processing.Processor; 44 45import com.sun.source.util.Plugin; 46import com.sun.tools.javac.jvm.Target; 47 48/** PlatformProvider for JDK N. 49 * 50 * <p><b>This is NOT part of any supported API. 51 * If you write code that depends on this, you do so at your own risk. 52 * This code and its internal interfaces are subject to change or 53 * deletion without notice.</b> 54 */ 55public class JDKPlatformProvider implements PlatformProvider { 56 57 @Override 58 public Iterable<String> getSupportedPlatformNames() { 59 return SUPPORTED_JAVA_PLATFORM_VERSIONS; 60 } 61 62 @Override 63 public PlatformDescription getPlatform(String platformName, String options) { 64 return new PlatformDescriptionImpl(platformName); 65 } 66 67 private static final String[] symbolFileLocation = { "lib", "ct.sym" }; 68 69 private static final Set<String> SUPPORTED_JAVA_PLATFORM_VERSIONS; 70 71 static { 72 SUPPORTED_JAVA_PLATFORM_VERSIONS = new TreeSet<>(); 73 Path ctSymFile = findCtSym(); 74 if (Files.exists(ctSymFile)) { 75 try (FileSystem fs = FileSystems.newFileSystem(ctSymFile, null); 76 DirectoryStream<Path> dir = 77 Files.newDirectoryStream(fs.getRootDirectories().iterator().next())) { 78 for (Path section : dir) { 79 for (char ver : section.getFileName().toString().toCharArray()) { 80 String verString = Character.toString(ver); 81 Target t = Target.lookup(verString); 82 83 if (t != null) { 84 SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(t)); 85 } 86 } 87 } 88 } catch (IOException ex) { 89 } 90 } 91 SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(Target.DEFAULT)); 92 } 93 94 private static String targetNumericVersion(Target target) { 95 return Integer.toString(target.ordinal() - Target.JDK1_1.ordinal() + 1); 96 } 97 98 static class PlatformDescriptionImpl implements PlatformDescription { 99 100 private final Map<Path, FileSystem> ctSym2FileSystem = new HashMap<>(); 101 private final String version; 102 103 PlatformDescriptionImpl(String version) { 104 this.version = version; 105 } 106 107 @Override 108 public Collection<Path> getPlatformPath() { 109 if (Target.lookup(version) == Target.DEFAULT) { 110 return null; 111 } 112 113 List<Path> paths = new ArrayList<>(); 114 Path file = findCtSym(); 115 // file == ${jdk.home}/lib/ct.sym 116 if (Files.exists(file)) { 117 FileSystem fs = ctSym2FileSystem.get(file); 118 if (fs == null) { 119 try { 120 ctSym2FileSystem.put(file, fs = FileSystems.newFileSystem(file, null)); 121 } catch (IOException ex) { 122 throw new IllegalStateException(ex); 123 } 124 } 125 Path root = fs.getRootDirectories().iterator().next(); 126 try (DirectoryStream<Path> dir = Files.newDirectoryStream(root)) { 127 for (Path section : dir) { 128 if (section.getFileName().toString().contains(version)) { 129 paths.add(section); 130 } 131 } 132 } catch (IOException ex) { 133 throw new IllegalStateException(ex); 134 } 135 } else { 136 throw new IllegalStateException("Cannot find ct.sym!"); 137 } 138 return paths; 139 } 140 141 @Override 142 public String getSourceVersion() { 143 return version; 144 } 145 146 @Override 147 public String getTargetVersion() { 148 return version; 149 } 150 151 @Override 152 public List<PluginInfo<Processor>> getAnnotationProcessors() { 153 return Collections.emptyList(); 154 } 155 156 @Override 157 public List<PluginInfo<Plugin>> getPlugins() { 158 return Collections.emptyList(); 159 } 160 161 @Override 162 public List<String> getAdditionalOptions() { 163 return Collections.emptyList(); 164 } 165 166 @Override 167 public void close() throws IOException { 168 for (FileSystem fs : ctSym2FileSystem.values()) { 169 fs.close(); 170 } 171 ctSym2FileSystem.clear(); 172 } 173 174 } 175 176 static Path findCtSym() { 177 String javaHome = System.getProperty("java.home"); 178 Path file = Paths.get(javaHome); 179 // file == ${jdk.home} 180 for (String name : symbolFileLocation) 181 file = file.resolve(name); 182 return file; 183 } 184 185} 186