src/share/classes/com/sun/tools/javac/file/CacheFSInfo.java

Thu, 10 Feb 2011 14:24:26 -0800

author
jjg
date
Thu, 10 Feb 2011 14:24:26 -0800
changeset 872
a19b1f4f23c9
parent 839
a8437c34fdc7
child 893
8f0dcb9499db
permissions
-rw-r--r--

7018098: CacheFSInfo persists too long
Reviewed-by: mcimadamore

jjg@106 1 /*
jjg@839 2 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
jjg@106 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@106 4 *
jjg@106 5 * This code is free software; you can redistribute it and/or modify it
jjg@106 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@106 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@106 10 *
jjg@106 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@106 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@106 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@106 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@106 15 * accompanied this code).
jjg@106 16 *
jjg@106 17 * You should have received a copy of the GNU General Public License version
jjg@106 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@106 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@106 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@106 24 */
jjg@106 25
jjg@106 26 package com.sun.tools.javac.file;
jjg@106 27
jjg@106 28 import java.io.File;
jjg@106 29 import java.io.IOException;
jjg@106 30 import java.util.List;
jjg@839 31 import java.util.Map;
jjg@839 32 import java.util.concurrent.ConcurrentHashMap;
jjg@106 33
jjg@106 34 import com.sun.tools.javac.util.Context;
jjg@106 35
jjg@106 36 /**
jjg@333 37 * Caching implementation of FSInfo.
jjg@333 38 *
jjg@581 39 * <p><b>This is NOT part of any supported API.
jjg@333 40 * If you write code that depends on this, you do so at your own risk.
jjg@333 41 * This code and its internal interfaces are subject to change or
jjg@333 42 * deletion without notice.</b>
jjg@106 43 */
jjg@106 44 public class CacheFSInfo extends FSInfo {
jjg@106 45
jjg@106 46 /**
jjg@106 47 * Register a Context.Factory to create a singleton CacheFSInfo.
jjg@106 48 */
jjg@106 49 public static void preRegister(final Context context) {
jjg@106 50 context.put(FSInfo.class, new Context.Factory<FSInfo>() {
jjg@106 51 public FSInfo make() {
jjg@872 52 FSInfo instance = new CacheFSInfo();
jjg@872 53 context.put(FSInfo.class, instance);
jjg@872 54 return instance;
jjg@106 55 }
jjg@106 56 });
jjg@106 57 }
jjg@106 58
jjg@106 59 public void clearCache() {
jjg@106 60 cache.clear();
jjg@106 61 }
jjg@106 62
jjg@106 63 @Override
jjg@106 64 public File getCanonicalFile(File file) {
jjg@106 65 Entry e = getEntry(file);
jjg@106 66 return e.canonicalFile;
jjg@106 67 }
jjg@106 68
jjg@106 69 @Override
jjg@106 70 public boolean exists(File file) {
jjg@106 71 Entry e = getEntry(file);
jjg@106 72 return e.exists;
jjg@106 73 }
jjg@106 74
jjg@106 75 @Override
jjg@106 76 public boolean isDirectory(File file) {
jjg@106 77 Entry e = getEntry(file);
jjg@106 78 return e.isDirectory;
jjg@106 79 }
jjg@106 80
jjg@106 81 @Override
jjg@106 82 public boolean isFile(File file) {
jjg@106 83 Entry e = getEntry(file);
jjg@106 84 return e.isFile;
jjg@106 85 }
jjg@106 86
jjg@106 87 @Override
jjg@106 88 public List<File> getJarClassPath(File file) throws IOException {
jjg@106 89 // don't bother to lock the cache, because it is thread-safe, and
jjg@106 90 // because the worst that can happen would be to create two identical
jjg@106 91 // jar class paths together and have one overwrite the other.
jjg@106 92 Entry e = getEntry(file);
jjg@106 93 if (e.jarClassPath == null)
jjg@106 94 e.jarClassPath = super.getJarClassPath(file);
jjg@106 95 return e.jarClassPath;
jjg@106 96 }
jjg@106 97
jjg@106 98 private Entry getEntry(File file) {
jjg@106 99 // don't bother to lock the cache, because it is thread-safe, and
jjg@106 100 // because the worst that can happen would be to create two identical
jjg@106 101 // entries together and have one overwrite the other.
jjg@106 102 Entry e = cache.get(file);
jjg@106 103 if (e == null) {
jjg@106 104 e = new Entry();
jjg@106 105 e.canonicalFile = super.getCanonicalFile(file);
jjg@106 106 e.exists = super.exists(file);
jjg@106 107 e.isDirectory = super.isDirectory(file);
jjg@106 108 e.isFile = super.isFile(file);
jjg@106 109 cache.put(file, e);
jjg@106 110 }
jjg@106 111 return e;
jjg@106 112 }
jjg@106 113
jjg@106 114 // could also be a Map<File,SoftReference<Entry>> ?
jjg@106 115 private Map<File,Entry> cache = new ConcurrentHashMap<File,Entry>();
jjg@106 116
jjg@106 117 private static class Entry {
jjg@106 118 File canonicalFile;
jjg@106 119 boolean exists;
jjg@106 120 boolean isFile;
jjg@106 121 boolean isDirectory;
jjg@106 122 List<File> jarClassPath;
jjg@106 123 }
jjg@106 124 }

mercurial