CacheFSInfo.java revision 3827:44bdefe64114
1/* 2 * Copyright (c) 2005, 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.file; 27 28import java.io.IOException; 29import java.nio.file.Path; 30import java.util.List; 31import java.util.Map; 32import java.util.concurrent.ConcurrentHashMap; 33 34import com.sun.tools.javac.util.Context; 35import com.sun.tools.javac.util.Context.Factory; 36 37/** 38 * Caching implementation of FSInfo. 39 * 40 * <p><b>This is NOT part of any supported API. 41 * If you write code that depends on this, you do so at your own risk. 42 * This code and its internal interfaces are subject to change or 43 * deletion without notice.</b> 44 */ 45public class CacheFSInfo extends FSInfo { 46 47 /** 48 * Register a Context.Factory to create a CacheFSInfo. 49 */ 50 public static void preRegister(Context context) { 51 context.put(FSInfo.class, (Factory<FSInfo>)c -> { 52 FSInfo instance = new CacheFSInfo(); 53 c.put(FSInfo.class, instance); 54 return instance; 55 }); 56 } 57 58 public void clearCache() { 59 cache.clear(); 60 } 61 62 @Override 63 public Path getCanonicalFile(Path file) { 64 Entry e = getEntry(file); 65 return e.canonicalFile; 66 } 67 68 @Override 69 public boolean exists(Path file) { 70 Entry e = getEntry(file); 71 return e.exists; 72 } 73 74 @Override 75 public boolean isDirectory(Path file) { 76 Entry e = getEntry(file); 77 return e.isDirectory; 78 } 79 80 @Override 81 public boolean isFile(Path file) { 82 Entry e = getEntry(file); 83 return e.isFile; 84 } 85 86 @Override 87 public List<Path> getJarClassPath(Path file) throws IOException { 88 // don't bother to lock the cache, because it is thread-safe, and 89 // because the worst that can happen would be to create two identical 90 // jar class paths together and have one overwrite the other. 91 Entry e = getEntry(file); 92 if (e.jarClassPath == null) 93 e.jarClassPath = super.getJarClassPath(file); 94 return e.jarClassPath; 95 } 96 97 private Entry getEntry(Path file) { 98 // don't bother to lock the cache, because it is thread-safe, and 99 // because the worst that can happen would be to create two identical 100 // entries together and have one overwrite the other. 101 Entry e = cache.get(file); 102 if (e == null) { 103 e = new Entry(); 104 e.canonicalFile = super.getCanonicalFile(file); 105 e.exists = super.exists(file); 106 e.isDirectory = super.isDirectory(file); 107 e.isFile = super.isFile(file); 108 cache.put(file, e); 109 } 110 return e; 111 } 112 113 // could also be a Map<File,SoftReference<Entry>> ? 114 private final Map<Path,Entry> cache = new ConcurrentHashMap<>(); 115 116 private static class Entry { 117 Path canonicalFile; 118 boolean exists; 119 boolean isFile; 120 boolean isDirectory; 121 List<Path> jarClassPath; 122 } 123} 124