1 /* |
1 /* |
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
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. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
33 import java.nio.file.FileSystems; |
33 import java.nio.file.FileSystems; |
34 import java.nio.file.FileSystemNotFoundException; |
34 import java.nio.file.FileSystemNotFoundException; |
35 import java.nio.file.Files; |
35 import java.nio.file.Files; |
36 import java.nio.file.Path; |
36 import java.nio.file.Path; |
37 import java.nio.file.ProviderNotFoundException; |
37 import java.nio.file.ProviderNotFoundException; |
38 import java.nio.file.spi.FileSystemProvider; |
|
39 import java.util.Collections; |
38 import java.util.Collections; |
40 import java.util.HashMap; |
39 import java.util.HashMap; |
41 import java.util.LinkedHashMap; |
40 import java.util.LinkedHashMap; |
42 import java.util.LinkedHashSet; |
41 import java.util.LinkedHashSet; |
43 import java.util.Map; |
42 import java.util.Map; |
93 * The jrt: file system. |
92 * The jrt: file system. |
94 */ |
93 */ |
95 private final FileSystem jrtfs; |
94 private final FileSystem jrtfs; |
96 |
95 |
97 /** |
96 /** |
98 * The set of module directories within the jrt: file system. |
|
99 */ |
|
100 private final Set<Path> jrtModules; |
|
101 |
|
102 /** |
|
103 * A lazily evaluated set of entries about the contents of the jrt: file system. |
97 * A lazily evaluated set of entries about the contents of the jrt: file system. |
104 */ |
98 */ |
105 private final Map<RelativeDirectory, SoftReference<Entry>> entries; |
99 private final Map<RelativeDirectory, SoftReference<Entry>> entries; |
106 |
100 |
107 /** |
101 /** |
181 /** |
175 /** |
182 * Create and initialize the index. |
176 * Create and initialize the index. |
183 */ |
177 */ |
184 private JRTIndex() throws IOException { |
178 private JRTIndex() throws IOException { |
185 jrtfs = FileSystems.getFileSystem(URI.create("jrt:/")); |
179 jrtfs = FileSystems.getFileSystem(URI.create("jrt:/")); |
186 jrtModules = new LinkedHashSet<>(); |
|
187 Path root = jrtfs.getPath("/"); |
|
188 try (DirectoryStream<Path> stream = Files.newDirectoryStream(root)) { |
|
189 for (Path entry: stream) { |
|
190 if (Files.isDirectory(entry)) |
|
191 jrtModules.add(entry); |
|
192 } |
|
193 } |
|
194 entries = new HashMap<>(); |
180 entries = new HashMap<>(); |
195 } |
181 } |
196 |
182 |
197 public CtSym getCtSym(CharSequence packageName) throws IOException { |
183 public CtSym getCtSym(CharSequence packageName) throws IOException { |
198 return getEntry(RelativeDirectory.forPackage(packageName)).ctSym; |
184 return getEntry(RelativeDirectory.forPackage(packageName)).ctSym; |
202 SoftReference<Entry> ref = entries.get(rd); |
188 SoftReference<Entry> ref = entries.get(rd); |
203 Entry e = (ref == null) ? null : ref.get(); |
189 Entry e = (ref == null) ? null : ref.get(); |
204 if (e == null) { |
190 if (e == null) { |
205 Map<String, Path> files = new LinkedHashMap<>(); |
191 Map<String, Path> files = new LinkedHashMap<>(); |
206 Set<RelativeDirectory> subdirs = new LinkedHashSet<>(); |
192 Set<RelativeDirectory> subdirs = new LinkedHashSet<>(); |
207 for (Path module: jrtModules) { |
193 Path dir; |
208 Path p = rd.getFile(module); |
194 if (rd.path.isEmpty()) { |
209 if (!Files.exists(p)) |
195 dir = jrtfs.getPath("/modules"); |
210 continue; |
196 } else { |
211 try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) { |
197 Path pkgs = jrtfs.getPath("/packages"); |
212 for (Path entry: stream) { |
198 dir = pkgs.resolve(rd.getPath().replaceAll("/$", "").replace("/", ".")); |
213 String name = entry.getFileName().toString(); |
199 } |
214 if (Files.isRegularFile(entry)) { |
200 if (Files.exists(dir)) { |
215 // TODO: consider issue of files with same name in different modules |
201 try (DirectoryStream<Path> modules = Files.newDirectoryStream(dir)) { |
216 files.put(name, entry); |
202 for (Path module: modules) { |
217 } else if (Files.isDirectory(entry)) { |
203 Path p = rd.getFile(module); |
218 subdirs.add(new RelativeDirectory(rd, name)); |
204 if (!Files.exists(p)) |
|
205 continue; |
|
206 try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) { |
|
207 for (Path entry: stream) { |
|
208 String name = entry.getFileName().toString(); |
|
209 if (Files.isRegularFile(entry)) { |
|
210 // TODO: consider issue of files with same name in different modules |
|
211 files.put(name, entry); |
|
212 } else if (Files.isDirectory(entry)) { |
|
213 subdirs.add(new RelativeDirectory(rd, name)); |
|
214 } |
|
215 } |
219 } |
216 } |
220 } |
217 } |
221 } |
218 } |
222 } |
219 } |
223 e = new Entry(Collections.unmodifiableMap(files), |
220 e = new Entry(Collections.unmodifiableMap(files), |