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