1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/file/CacheFSInfo.java Wed Sep 03 10:46:25 2008 -0700 1.3 @@ -0,0 +1,122 @@ 1.4 +/* 1.5 + * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.file; 1.30 + 1.31 +import java.io.File; 1.32 +import java.io.IOException; 1.33 +import java.util.List; 1.34 + 1.35 +import com.sun.tools.javac.util.Context; 1.36 +import java.util.Map; 1.37 +import java.util.concurrent.ConcurrentHashMap; 1.38 + 1.39 +/** 1.40 + * Caching implementation of FSInfo 1.41 + */ 1.42 +public class CacheFSInfo extends FSInfo { 1.43 + 1.44 + /** 1.45 + * Register a Context.Factory to create a singleton CacheFSInfo. 1.46 + */ 1.47 + public static void preRegister(final Context context) { 1.48 + context.put(FSInfo.class, new Context.Factory<FSInfo>() { 1.49 + public FSInfo make() { 1.50 + if (singleton == null) 1.51 + singleton = new CacheFSInfo(); 1.52 + context.put(FSInfo.class, singleton); 1.53 + return singleton; 1.54 + } 1.55 + }); 1.56 + } 1.57 + 1.58 + static CacheFSInfo singleton; 1.59 + 1.60 + public void clearCache() { 1.61 + cache.clear(); 1.62 + } 1.63 + 1.64 + @Override 1.65 + public File getCanonicalFile(File file) { 1.66 + Entry e = getEntry(file); 1.67 + return e.canonicalFile; 1.68 + } 1.69 + 1.70 + @Override 1.71 + public boolean exists(File file) { 1.72 + Entry e = getEntry(file); 1.73 + return e.exists; 1.74 + } 1.75 + 1.76 + @Override 1.77 + public boolean isDirectory(File file) { 1.78 + Entry e = getEntry(file); 1.79 + return e.isDirectory; 1.80 + } 1.81 + 1.82 + @Override 1.83 + public boolean isFile(File file) { 1.84 + Entry e = getEntry(file); 1.85 + return e.isFile; 1.86 + } 1.87 + 1.88 + @Override 1.89 + public List<File> getJarClassPath(File file) throws IOException { 1.90 + // don't bother to lock the cache, because it is thread-safe, and 1.91 + // because the worst that can happen would be to create two identical 1.92 + // jar class paths together and have one overwrite the other. 1.93 + Entry e = getEntry(file); 1.94 + if (e.jarClassPath == null) 1.95 + e.jarClassPath = super.getJarClassPath(file); 1.96 + return e.jarClassPath; 1.97 + } 1.98 + 1.99 + private Entry getEntry(File file) { 1.100 + // don't bother to lock the cache, because it is thread-safe, and 1.101 + // because the worst that can happen would be to create two identical 1.102 + // entries together and have one overwrite the other. 1.103 + Entry e = cache.get(file); 1.104 + if (e == null) { 1.105 + e = new Entry(); 1.106 + e.canonicalFile = super.getCanonicalFile(file); 1.107 + e.exists = super.exists(file); 1.108 + e.isDirectory = super.isDirectory(file); 1.109 + e.isFile = super.isFile(file); 1.110 + cache.put(file, e); 1.111 + } 1.112 + return e; 1.113 + } 1.114 + 1.115 + // could also be a Map<File,SoftReference<Entry>> ? 1.116 + private Map<File,Entry> cache = new ConcurrentHashMap<File,Entry>(); 1.117 + 1.118 + private static class Entry { 1.119 + File canonicalFile; 1.120 + boolean exists; 1.121 + boolean isFile; 1.122 + boolean isDirectory; 1.123 + List<File> jarClassPath; 1.124 + } 1.125 +}