Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -62,7 +61,7 @@ public final class InstalledFileLocatorImpl extends InstalledFileLocator {
private final File[] dirs;
public InstalledFileLocatorImpl() {
List<File> _dirs = computeDirs();
dirs = _dirs.toArray(new File[0]);
dirs = _dirs.toArray(File[]::new);
}

private static void addDir(List<File> _dirs, String d) {
Expand Down Expand Up @@ -99,41 +98,42 @@ private static void addDir(List<File> _dirs, String d) {
*/
public static synchronized void prepareCache() {
assert fileCache == null;
fileCache = new HashMap<String,Map<File,Set<String>>>();
clusterCache = new HashMap<String,List<File>>();
fileCache = new HashMap<>();
clusterCache = new HashMap<>();

try {
InputStream is = Stamps.getModulesJARs().asStream("all-files.dat");
if (is == null) {
return;
}
DataInputStream dis = new DataInputStream(is);
int filesSize = dis.readInt();
for (int i = 0; i < filesSize; i++) {
String key = dis.readUTF();
Map<File,Set<String>> fileToKids = new HashMap<File, Set<String>>();
int filesToKids = dis.readInt();
for (int j = 0; j < filesToKids; j++) {
final String read = RelPaths.readRelativePath(dis);
File f = new File(read);
int kidsSize = dis.readInt();
List<String> kids = new ArrayList<String>(kidsSize);
for (int k = 0; k < kidsSize; k++) {
kids.add(dis.readUTF());
try (DataInputStream dis = new DataInputStream(is)) {
int filesSize = dis.readInt();
for (int i = 0; i < filesSize; i++) {
String key = dis.readUTF();
Map<File, Set<String>> fileToKids = new HashMap<>();
int filesToKids = dis.readInt();
for (int j = 0; j < filesToKids; j++) {
final String read = RelPaths.readRelativePath(dis);
File f = new File(read);
int kidsSize = dis.readInt();
List<String> kids = new ArrayList<>(kidsSize);
for (int k = 0; k < kidsSize; k++) {
kids.add(dis.readUTF());
}
fileToKids.put(f, new HashSet<>(kids));
}
fileToKids.put(f, new HashSet<String>(kids));
fileCache.put(key, fileToKids);
}
fileCache.put(key, fileToKids);
}
int clusterSize = dis.readInt();
for (int i = 0; i < clusterSize; i++) {
String key = dis.readUTF();
int valueSize = dis.readInt();
List<File> values = new ArrayList<File>(valueSize);
for (int j = 0; j < valueSize; j++) {
values.add(new File(RelPaths.readRelativePath(dis)));
int clusterSize = dis.readInt();
for (int i = 0; i < clusterSize; i++) {
String key = dis.readUTF();
int valueSize = dis.readInt();
List<File> values = new ArrayList<>(valueSize);
for (int j = 0; j < valueSize; j++) {
values.add(new File(RelPaths.readRelativePath(dis)));
}
clusterCache.put(key, values);
}
clusterCache.put(key, values);
}
} catch (IOException ex) {
LOG.log(Level.INFO, null, ex);
Expand Down Expand Up @@ -242,7 +242,7 @@ private Set<File> doLocate(String relativePath, boolean localized, boolean singl
} else if (files == null) {
files = f;
} else {
files = new LinkedHashSet<File>(files);
files = new LinkedHashSet<>(files);
files.addAll(f);
}
}
Expand All @@ -267,11 +267,11 @@ private Set<File> locateExactPath(String prefix, String name, boolean single, St
assert owned(codeNameBase, dir, path);
File f = makeFile(dir, path);
if (single) {
return Collections.singleton(f);
return Set.of(f);
} else if (files == null) {
files = Collections.singleton(f);
files = Set.of(f);
} else {
files = new LinkedHashSet<File>(files);
files = new LinkedHashSet<>(files);
files.add(f);
}
}
Expand All @@ -282,11 +282,11 @@ private Set<File> locateExactPath(String prefix, String name, boolean single, St
if (f.exists()) {
assert owned(codeNameBase, dir, path);
if (single) {
return Collections.singleton(f);
return Set.of(f);
} else if (files == null) {
files = Collections.singleton(f);
files = Set.of(f);
} else {
files = new LinkedHashSet<File>(files);
files = new LinkedHashSet<>(files);
files.add(f);
}
}
Expand All @@ -301,14 +301,14 @@ private List<File> clustersFor(String codeNameBase, String path) {
return Arrays.asList(dirs);
}
String codeNameBaseDashes = codeNameBase.replace('.', '-');
if (path.matches("(modules/(locale/)?)?" + codeNameBaseDashes + "(_[^/]+)?[.]jar")) { // NOI18N
if (isBaseInPath(codeNameBaseDashes, path)) {
// Called very commonly during startup; cannot afford to do exact check each time.
// Anyway if the module is there it is almost certainly installed in the same cluster.
return Arrays.asList(dirs);
}
List<File> clusters = clusterCache != null ? clusterCache.get(codeNameBase) : null;
if (clusters == null) {
clusters = new ArrayList<File>(1);
clusters = new ArrayList<>(1);
String rel = "update_tracking/" + codeNameBaseDashes + ".xml"; // NOI18N
for (File dir : dirs) {
File tracking = new File(dir, rel);
Expand All @@ -331,8 +331,29 @@ private List<File> clustersFor(String codeNameBase, String path) {
return clusters;
}

private static final Pattern TAIL = Pattern.compile("(_[^/]+)?[.]jar"); // NOI18N

// hot section: avoids recompiling the pattern by unrolling the prefix manually
private static boolean isBaseInPath(String codeNameBaseDashes, String path) {
// return path.matches("(modules/(locale/)?)?" + codeNameBaseDashes + "(_[^/]+)?[.]jar");
int pos = 0;
if (path.startsWith("modules/")) { // NOI18N
pos += "modules/".length(); // NOI18N
if (path.startsWith("locale/", pos)) { // NOI18N
pos += "locale/".length(); // NOI18N
}
}
if (path.startsWith(codeNameBaseDashes, pos)) {
pos += codeNameBaseDashes.length();
if (TAIL.matcher(path.substring(pos)).matches()) {
return true;
}
}
return false;
}

private static String[] prefixAndName(String relativePath) {
if (relativePath.length() == 0) {
if (relativePath.isEmpty()) {
throw new IllegalArgumentException("Cannot look up \"\" in InstalledFileLocator.locate"); // NOI18N
}
if (relativePath.charAt(0) == '/') {
Expand All @@ -359,7 +380,7 @@ private Map<File,Set<String>> fileCachePerPrefix(String prefix) {
assert Thread.holdsLock(InstalledFileLocatorImpl.class);
Map<File,Set<String>> fileCachePerPrefix = fileCache.get(prefix);
if (fileCachePerPrefix == null) {
fileCachePerPrefix = new HashMap<File,Set<String>>(dirs.length * 2);
fileCachePerPrefix = new HashMap<>(dirs.length * 2);
for (int i = 0; i < dirs.length; i++) {
File root = dirs[i];
File d;
Expand All @@ -375,7 +396,7 @@ private Map<File,Set<String>> fileCachePerPrefix(String prefix) {
if (isDir) {
String[] kids = d.list();
if (kids != null) {
fileCachePerPrefix.put(root, new HashSet<String>(Arrays.asList(kids)));
fileCachePerPrefix.put(root, new HashSet<>(Arrays.asList(kids)));
} else {
Util.err.log(Level.WARNING, "could not read files in {0} at {1}", new Object[] {d, findCaller()});
}
Expand Down Expand Up @@ -419,7 +440,7 @@ private static synchronized boolean owned(String codeNameBase, File dir, String
LOG.log(Level.FINE, "No update tracking found in {0}", dir);
return true;
}
ownershipByModule = new HashMap<String,Set<String>>();
ownershipByModule = new HashMap<>();
ownershipByModuleByCluster.put(dir, ownershipByModule);
}
Set<String> ownership = ownershipByModule.get(codeNameBase);
Expand All @@ -429,25 +450,20 @@ private static synchronized boolean owned(String codeNameBase, File dir, String
LOG.log(Level.WARNING, "no such module {0} at {1}", new Object[] {list, findCaller()});
return true;
}
ownership = new HashSet<String>();
ownership = new HashSet<>();
try {
// Could do a proper XML parse but likely too slow.
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Parsing {0} due to {1}", new Object[] {list, path});
}
Reader r = new FileReader(list);
try {
BufferedReader br = new BufferedReader(r);
try (BufferedReader br = new BufferedReader(new FileReader(list))) {
String line;
while ((line = br.readLine()) != null) {
Matcher m = FILE_PATTERN.matcher(line);
if (m.matches()) {
ownership.add(m.group(1));
}
}
br.close();
} finally {
r.close();
}
} catch (IOException x) {
LOG.log(Level.INFO, "could not parse " + list, x);
Expand Down Expand Up @@ -476,7 +492,7 @@ private static synchronized boolean owned(String codeNameBase, File dir, String
return true;
}
private static final Pattern FILE_PATTERN = Pattern.compile("\\s*<file.+name=[\"']([^\"']+)[\"'].*/>");
private static final Map<File,Map<String,Set<String>>> ownershipByModuleByCluster = new HashMap<File,Map<String,Set<String>>>();
private static final Map<File, Map<String, Set<String>>> ownershipByModuleByCluster = new HashMap<>();

private static String findCaller() {
for (StackTraceElement line : Thread.currentThread().getStackTrace()) {
Expand All @@ -492,7 +508,7 @@ private static synchronized void scheduleSave() {
}

static List<File> computeDirs() {
List<File> _dirs = new ArrayList<File>();
List<File> _dirs = new ArrayList<>();
addDir(_dirs, System.getProperty("netbeans.user")); // NOI18N
String nbdirs = System.getProperty("netbeans.dirs"); // #27151
if (nbdirs != null) {
Expand Down
Loading
Loading