JDKPlatformProvider.java revision 3170:dc017a37aac5
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
26package com.sun.tools.javac.platform;
27
28import java.io.IOException;
29import java.nio.file.DirectoryStream;
30import java.nio.file.FileSystem;
31import java.nio.file.FileSystems;
32import java.nio.file.Files;
33import java.nio.file.Path;
34import java.nio.file.Paths;
35import java.util.ArrayList;
36import java.util.Collection;
37import java.util.Collections;
38import java.util.HashMap;
39import java.util.List;
40import java.util.Map;
41import java.util.Set;
42import java.util.TreeSet;
43
44import javax.annotation.processing.Processor;
45
46import com.sun.source.util.Plugin;
47import 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 */
56public class JDKPlatformProvider implements PlatformProvider {
57
58    @Override
59    public Iterable<String> getSupportedPlatformNames() {
60        return SUPPORTED_JAVA_PLATFORM_VERSIONS;
61    }
62
63    @Override
64    public PlatformDescription getPlatform(String platformName, String options) {
65        return new PlatformDescriptionImpl(platformName);
66    }
67
68    private static final String[] symbolFileLocation = { "lib", "ct.sym" };
69
70    private static final Set<String> SUPPORTED_JAVA_PLATFORM_VERSIONS;
71
72    static {
73        SUPPORTED_JAVA_PLATFORM_VERSIONS = new TreeSet<>();
74        Path ctSymFile = findCtSym();
75        if (Files.exists(ctSymFile)) {
76            try (FileSystem fs = FileSystems.newFileSystem(ctSymFile, null);
77                 DirectoryStream<Path> dir =
78                         Files.newDirectoryStream(fs.getRootDirectories().iterator().next())) {
79                for (Path section : dir) {
80                    for (char ver : section.getFileName().toString().toCharArray()) {
81                        String verString = Character.toString(ver);
82                        Target t = Target.lookup(verString);
83
84                        if (t != null) {
85                            SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(t));
86                        }
87                    }
88                }
89            } catch (IOException ex) {
90            }
91        }
92        SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(Target.DEFAULT));
93    }
94
95    private static String targetNumericVersion(Target target) {
96        return Integer.toString(target.ordinal() - Target.JDK1_1.ordinal() + 1);
97    }
98
99    static class PlatformDescriptionImpl implements PlatformDescription {
100
101        private final Map<Path, FileSystem> ctSym2FileSystem = new HashMap<>();
102        private final String version;
103
104        PlatformDescriptionImpl(String version) {
105            this.version = version;
106        }
107
108        @Override
109        public Collection<Path> getPlatformPath() {
110            if (Target.lookup(version) == Target.DEFAULT) {
111                return null;
112            }
113
114            List<Path> paths = new ArrayList<>();
115            Path file = findCtSym();
116            // file == ${jdk.home}/lib/ct.sym
117            if (Files.exists(file)) {
118                FileSystem fs = ctSym2FileSystem.get(file);
119                if (fs == null) {
120                    try {
121                        ctSym2FileSystem.put(file, fs = FileSystems.newFileSystem(file, null));
122                    } catch (IOException ex) {
123                        throw new IllegalStateException(ex);
124                    }
125                }
126                Path root = fs.getRootDirectories().iterator().next();
127                try (DirectoryStream<Path> dir = Files.newDirectoryStream(root)) {
128                    for (Path section : dir) {
129                        if (section.getFileName().toString().contains(version)) {
130                            paths.add(section);
131                        }
132                    }
133                } catch (IOException ex) {
134                    throw new IllegalStateException(ex);
135                }
136            } else {
137                throw new IllegalStateException("Cannot find ct.sym!");
138            }
139            return paths;
140        }
141
142        @Override
143        public String getSourceVersion() {
144            return version;
145        }
146
147        @Override
148        public String getTargetVersion() {
149            return version;
150        }
151
152        @Override
153        public List<PluginInfo<Processor>> getAnnotationProcessors() {
154            return Collections.emptyList();
155        }
156
157        @Override
158        public List<PluginInfo<Plugin>> getPlugins() {
159            return Collections.emptyList();
160        }
161
162        @Override
163        public List<String> getAdditionalOptions() {
164            return Collections.emptyList();
165        }
166
167        @Override
168        public void close() throws IOException {
169            for (FileSystem fs : ctSym2FileSystem.values()) {
170                fs.close();
171            }
172            ctSym2FileSystem.clear();
173        }
174
175    }
176
177    static Path findCtSym() {
178        String javaHome = System.getProperty("java.home");
179        Path file = Paths.get(javaHome);
180        // file == ${jdk.home}
181        for (String name : symbolFileLocation)
182            file = file.resolve(name);
183        return file;
184    }
185
186}
187