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 */ 25 package com.sun.tools.javac.platform; 26 27 import java.io.IOException; 28 import java.nio.file.DirectoryStream; 29 import java.nio.file.FileSystem; 30 import java.nio.file.FileSystems; 31 import java.nio.file.Files; 32 import java.nio.file.Path; 33 import java.nio.file.Paths; 34 import java.util.ArrayList; 35 import java.util.Collection; 36 import java.util.Collections; 37 import java.util.HashMap; 38 import java.util.List; 39 import java.util.Map; 40 import java.util.Set; 41 import java.util.TreeSet; 42 import java.util.stream.Collectors; 43 44 import javax.annotation.processing.Processor; 45 46 import com.sun.source.util.Plugin; 47 import com.sun.tools.javac.jvm.Target; 48 49 /** PlatformProvider for JDK N. 50 * 51 * <p><b>This is NOT part of any supported API. 52 * If you write code that depends on this, you do so at your own risk. 53 * This code and its internal interfaces are subject to change or 54 * deletion without notice.</b> 55 */ 56 public class JDKPlatformProviderFactory implements PlatformProviderFactory { 57 58 @Override 59 public Iterable<PlatformProvider> createPlatformProviders() { 60 return SUPPORTED_JAVA_PLATFORM_VERSIONS.stream() 61 .map(version -> new PlatformProviderImpl(version)) 62 .collect(Collectors.toList()); 63 } 64 65 private static final String[] symbolFileLocation = { "lib", "ct.sym" }; 66 67 private static final Set<String> SUPPORTED_JAVA_PLATFORM_VERSIONS; 68 69 static { 70 SUPPORTED_JAVA_PLATFORM_VERSIONS = new TreeSet<>(); 71 Path ctSymFile = findCtSym(); 72 if (Files.exists(ctSymFile)) { 73 try (FileSystem fs = FileSystems.newFileSystem(ctSymFile, null); 74 DirectoryStream<Path> dir = 75 Files.newDirectoryStream(fs.getRootDirectories().iterator().next())) { 76 for (Path section : dir) { 77 for (char ver : section.getFileName().toString().toCharArray()) { 78 String verString = Character.toString(ver); 79 Target t = Target.lookup(verString); 80 81 if (t != null) { 82 SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(t)); 83 } 84 } 85 } 86 } catch (IOException ex) { 87 //XXX: can handle somehow? Print a warning? 88 } 89 } 90 SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(Target.DEFAULT)); 91 } 92 93 private static String targetNumericVersion(Target target) { 94 return Integer.toString(target.ordinal() - Target.JDK1_1.ordinal() + 1); 95 } 96 97 static class PlatformProviderImpl implements PlatformProvider { 98 99 private final Map<Path, FileSystem> ctSym2FileSystem = new HashMap<>(); 100 private final String version; 101 102 PlatformProviderImpl(String version) { 103 this.version = version; 104 } 105 106 @Override 107 public String getName() { 108 return version; 109 } 110 111 @Override 112 public Collection<Path> getPlatformPath() { 113 if (Target.lookup(version) == Target.DEFAULT) { 114 return null; 115 } 116 117 List<Path> paths = new ArrayList<>(); 118 Path file = findCtSym(); 119 // file == ${jdk.home}/lib/ct.sym 120 if (Files.exists(file)) { 121 FileSystem fs = ctSym2FileSystem.get(file); 122 if (fs == null) { 123 try { 124 ctSym2FileSystem.put(file, fs = FileSystems.newFileSystem(file, null)); 125 } catch (IOException ex) { 126 throw new IllegalStateException(ex); 127 } 128 } 129 Path root = fs.getRootDirectories().iterator().next(); 130 try (DirectoryStream<Path> dir = Files.newDirectoryStream(root)) { 131 for (Path section : dir) { 132 if (section.getFileName().toString().contains(version)) { 133 paths.add(section); 134 } 135 } 136 } catch (IOException ex) { 137 throw new IllegalStateException(ex); 138 } 139 } else { 140 throw new IllegalStateException("Cannot find ct.sym!"); 141 } 142 return paths; 143 } 144 145 @Override 146 public String getSourceVersion() { 147 return version; 148 } 149 150 @Override 151 public String getTargetVersion() { 152 return version; 153 } 154 155 @Override 156 public List<PluginInfo<Processor>> getAnnotationProcessors() { 157 return Collections.emptyList(); 158 } 159 160 @Override 161 public List<PluginInfo<Plugin>> getPlugins() { 162 return Collections.emptyList(); 163 } 164 165 @Override 166 public List<String> getAdditionalOptions() { 167 return Collections.emptyList(); 168 } 169 170 @Override 171 public void close() throws IOException { 172 for (FileSystem fs : ctSym2FileSystem.values()) { 173 fs.close(); 174 } 175 ctSym2FileSystem.clear(); 176 } 177 178 } 179 180 static Path findCtSym() { 181 String javaHome = System.getProperty("java.home"); 182 Path file = Paths.get(javaHome); 183 if (file.getFileName().equals(Paths.get("jre"))) 184 file = file.getParent(); 185 // file == ${jdk.home} 186 for (String name : symbolFileLocation) 187 file = file.resolve(name); 188 return file; 189 } 190 191 }