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

changeset 106
ceaa6549687a
child 333
7c2d6da61646
equal deleted inserted replaced
104:5e89c4ca637c 106:ceaa6549687a
1 /*
2 * Copyright 2005-2008 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package com.sun.tools.javac.file;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.List;
31
32 import com.sun.tools.javac.util.Context;
33 import java.util.Map;
34 import java.util.concurrent.ConcurrentHashMap;
35
36 /**
37 * Caching implementation of FSInfo
38 */
39 public class CacheFSInfo extends FSInfo {
40
41 /**
42 * Register a Context.Factory to create a singleton CacheFSInfo.
43 */
44 public static void preRegister(final Context context) {
45 context.put(FSInfo.class, new Context.Factory<FSInfo>() {
46 public FSInfo make() {
47 if (singleton == null)
48 singleton = new CacheFSInfo();
49 context.put(FSInfo.class, singleton);
50 return singleton;
51 }
52 });
53 }
54
55 static CacheFSInfo singleton;
56
57 public void clearCache() {
58 cache.clear();
59 }
60
61 @Override
62 public File getCanonicalFile(File file) {
63 Entry e = getEntry(file);
64 return e.canonicalFile;
65 }
66
67 @Override
68 public boolean exists(File file) {
69 Entry e = getEntry(file);
70 return e.exists;
71 }
72
73 @Override
74 public boolean isDirectory(File file) {
75 Entry e = getEntry(file);
76 return e.isDirectory;
77 }
78
79 @Override
80 public boolean isFile(File file) {
81 Entry e = getEntry(file);
82 return e.isFile;
83 }
84
85 @Override
86 public List<File> getJarClassPath(File file) throws IOException {
87 // don't bother to lock the cache, because it is thread-safe, and
88 // because the worst that can happen would be to create two identical
89 // jar class paths together and have one overwrite the other.
90 Entry e = getEntry(file);
91 if (e.jarClassPath == null)
92 e.jarClassPath = super.getJarClassPath(file);
93 return e.jarClassPath;
94 }
95
96 private Entry getEntry(File file) {
97 // don't bother to lock the cache, because it is thread-safe, and
98 // because the worst that can happen would be to create two identical
99 // 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 }

mercurial