001    /*
002     * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.javac.file;
027    
028    import java.io.File;
029    import java.io.IOException;
030    import java.util.List;
031    
032    import com.sun.tools.javac.util.Context;
033    import java.util.Map;
034    import java.util.concurrent.ConcurrentHashMap;
035    
036    /**
037     * Caching implementation of FSInfo
038     */
039    public class CacheFSInfo extends FSInfo {
040    
041        /**
042         * Register a Context.Factory to create a singleton CacheFSInfo.
043         */
044        public static void preRegister(final Context context) {
045            context.put(FSInfo.class, new Context.Factory<FSInfo>() {
046                public FSInfo make() {
047                    if (singleton == null)
048                        singleton = new CacheFSInfo();
049                    context.put(FSInfo.class, singleton);
050                    return singleton;
051                }
052            });
053        }
054    
055        static CacheFSInfo singleton;
056    
057        public void clearCache() {
058            cache.clear();
059        }
060    
061        @Override
062        public File getCanonicalFile(File file) {
063            Entry e = getEntry(file);
064            return e.canonicalFile;
065        }
066    
067        @Override
068        public boolean exists(File file) {
069            Entry e = getEntry(file);
070            return e.exists;
071        }
072    
073        @Override
074        public boolean isDirectory(File file) {
075            Entry e = getEntry(file);
076            return e.isDirectory;
077        }
078    
079        @Override
080        public boolean isFile(File file) {
081            Entry e = getEntry(file);
082            return e.isFile;
083        }
084    
085        @Override
086        public List<File> getJarClassPath(File file) throws IOException {
087            // don't bother to lock the cache, because it is thread-safe, and
088            // because the worst that can happen would be to create two identical
089            // jar class paths together and have one overwrite the other.
090            Entry e = getEntry(file);
091            if (e.jarClassPath == null)
092                e.jarClassPath = super.getJarClassPath(file);
093            return e.jarClassPath;
094        }
095    
096        private Entry getEntry(File file) {
097            // don't bother to lock the cache, because it is thread-safe, and
098            // because the worst that can happen would be to create two identical
099            // entries together and have one overwrite the other.
100            Entry e = cache.get(file);
101            if (e == null) {
102                e = new Entry();
103                e.canonicalFile = super.getCanonicalFile(file);
104                e.exists = super.exists(file);
105                e.isDirectory = super.isDirectory(file);
106                e.isFile = super.isFile(file);
107                cache.put(file, e);
108            }
109            return e;
110        }
111    
112        // could also be a Map<File,SoftReference<Entry>> ?
113        private Map<File,Entry> cache = new ConcurrentHashMap<File,Entry>();
114    
115        private static class Entry {
116            File canonicalFile;
117            boolean exists;
118            boolean isFile;
119            boolean isDirectory;
120            List<File> jarClassPath;
121        }
122    }