Wed, 03 Sep 2008 10:46:25 -0700
6743107: clean up use of static caches in file manager
Reviewed-by: mcimadamore
jjg@106 | 1 | /* |
jjg@106 | 2 | * Copyright 2005-2008 Sun Microsystems, Inc. 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 |
jjg@106 | 7 | * published by the Free Software Foundation. Sun designates this |
jjg@106 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@106 | 9 | * by Sun 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 | * |
jjg@106 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@106 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@106 | 23 | * have any 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@106 | 31 | |
jjg@106 | 32 | import com.sun.tools.javac.util.Context; |
jjg@106 | 33 | import java.util.Map; |
jjg@106 | 34 | import java.util.concurrent.ConcurrentHashMap; |
jjg@106 | 35 | |
jjg@106 | 36 | /** |
jjg@106 | 37 | * Caching implementation of FSInfo |
jjg@106 | 38 | */ |
jjg@106 | 39 | public class CacheFSInfo extends FSInfo { |
jjg@106 | 40 | |
jjg@106 | 41 | /** |
jjg@106 | 42 | * Register a Context.Factory to create a singleton CacheFSInfo. |
jjg@106 | 43 | */ |
jjg@106 | 44 | public static void preRegister(final Context context) { |
jjg@106 | 45 | context.put(FSInfo.class, new Context.Factory<FSInfo>() { |
jjg@106 | 46 | public FSInfo make() { |
jjg@106 | 47 | if (singleton == null) |
jjg@106 | 48 | singleton = new CacheFSInfo(); |
jjg@106 | 49 | context.put(FSInfo.class, singleton); |
jjg@106 | 50 | return singleton; |
jjg@106 | 51 | } |
jjg@106 | 52 | }); |
jjg@106 | 53 | } |
jjg@106 | 54 | |
jjg@106 | 55 | static CacheFSInfo singleton; |
jjg@106 | 56 | |
jjg@106 | 57 | public void clearCache() { |
jjg@106 | 58 | cache.clear(); |
jjg@106 | 59 | } |
jjg@106 | 60 | |
jjg@106 | 61 | @Override |
jjg@106 | 62 | public File getCanonicalFile(File file) { |
jjg@106 | 63 | Entry e = getEntry(file); |
jjg@106 | 64 | return e.canonicalFile; |
jjg@106 | 65 | } |
jjg@106 | 66 | |
jjg@106 | 67 | @Override |
jjg@106 | 68 | public boolean exists(File file) { |
jjg@106 | 69 | Entry e = getEntry(file); |
jjg@106 | 70 | return e.exists; |
jjg@106 | 71 | } |
jjg@106 | 72 | |
jjg@106 | 73 | @Override |
jjg@106 | 74 | public boolean isDirectory(File file) { |
jjg@106 | 75 | Entry e = getEntry(file); |
jjg@106 | 76 | return e.isDirectory; |
jjg@106 | 77 | } |
jjg@106 | 78 | |
jjg@106 | 79 | @Override |
jjg@106 | 80 | public boolean isFile(File file) { |
jjg@106 | 81 | Entry e = getEntry(file); |
jjg@106 | 82 | return e.isFile; |
jjg@106 | 83 | } |
jjg@106 | 84 | |
jjg@106 | 85 | @Override |
jjg@106 | 86 | public List<File> getJarClassPath(File file) throws IOException { |
jjg@106 | 87 | // don't bother to lock the cache, because it is thread-safe, and |
jjg@106 | 88 | // because the worst that can happen would be to create two identical |
jjg@106 | 89 | // jar class paths together and have one overwrite the other. |
jjg@106 | 90 | Entry e = getEntry(file); |
jjg@106 | 91 | if (e.jarClassPath == null) |
jjg@106 | 92 | e.jarClassPath = super.getJarClassPath(file); |
jjg@106 | 93 | return e.jarClassPath; |
jjg@106 | 94 | } |
jjg@106 | 95 | |
jjg@106 | 96 | private Entry getEntry(File file) { |
jjg@106 | 97 | // don't bother to lock the cache, because it is thread-safe, and |
jjg@106 | 98 | // because the worst that can happen would be to create two identical |
jjg@106 | 99 | // entries together and have one overwrite the other. |
jjg@106 | 100 | Entry e = cache.get(file); |
jjg@106 | 101 | if (e == null) { |
jjg@106 | 102 | e = new Entry(); |
jjg@106 | 103 | e.canonicalFile = super.getCanonicalFile(file); |
jjg@106 | 104 | e.exists = super.exists(file); |
jjg@106 | 105 | e.isDirectory = super.isDirectory(file); |
jjg@106 | 106 | e.isFile = super.isFile(file); |
jjg@106 | 107 | cache.put(file, e); |
jjg@106 | 108 | } |
jjg@106 | 109 | return e; |
jjg@106 | 110 | } |
jjg@106 | 111 | |
jjg@106 | 112 | // could also be a Map<File,SoftReference<Entry>> ? |
jjg@106 | 113 | private Map<File,Entry> cache = new ConcurrentHashMap<File,Entry>(); |
jjg@106 | 114 | |
jjg@106 | 115 | private static class Entry { |
jjg@106 | 116 | File canonicalFile; |
jjg@106 | 117 | boolean exists; |
jjg@106 | 118 | boolean isFile; |
jjg@106 | 119 | boolean isDirectory; |
jjg@106 | 120 | List<File> jarClassPath; |
jjg@106 | 121 | } |
jjg@106 | 122 | } |