1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/file/ZipFileIndexCache.java Mon Jan 24 16:38:56 2011 -0800 1.3 @@ -0,0 +1,149 @@ 1.4 +/* 1.5 + * Copyright (c) 2007, 2011, Oracle and/or its affiliates. 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. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.file; 1.30 + 1.31 +import com.sun.tools.javac.file.RelativePath.RelativeDirectory; 1.32 +import com.sun.tools.javac.util.Context; 1.33 +import java.io.File; 1.34 +import java.io.IOException; 1.35 +import java.util.ArrayList; 1.36 +import java.util.HashMap; 1.37 +import java.util.Iterator; 1.38 +import java.util.List; 1.39 +import java.util.Map; 1.40 + 1.41 + 1.42 +/** A cache for ZipFileIndex objects. */ 1.43 +public class ZipFileIndexCache { 1.44 + 1.45 + private final Map<File, ZipFileIndex> map = 1.46 + new HashMap<File, ZipFileIndex>(); 1.47 + 1.48 + /** Get a shared instance of the cache. */ 1.49 + private static ZipFileIndexCache sharedInstance; 1.50 + public synchronized static ZipFileIndexCache getSharedInstance() { 1.51 + if (sharedInstance == null) 1.52 + sharedInstance = new ZipFileIndexCache(); 1.53 + return sharedInstance; 1.54 + } 1.55 + 1.56 + /** Get a context-specific instance of a cache. */ 1.57 + public static ZipFileIndexCache instance(Context context) { 1.58 + ZipFileIndexCache instance = context.get(ZipFileIndexCache.class); 1.59 + if (instance == null) 1.60 + context.put(ZipFileIndexCache.class, instance = new ZipFileIndexCache()); 1.61 + return instance; 1.62 + } 1.63 + 1.64 + /** 1.65 + * Returns a list of all ZipFileIndex entries 1.66 + * 1.67 + * @return A list of ZipFileIndex entries, or an empty list 1.68 + */ 1.69 + public List<ZipFileIndex> getZipFileIndexes() { 1.70 + return getZipFileIndexes(false); 1.71 + } 1.72 + 1.73 + /** 1.74 + * Returns a list of all ZipFileIndex entries 1.75 + * 1.76 + * @param openedOnly If true it returns a list of only opened ZipFileIndex entries, otherwise 1.77 + * all ZipFileEntry(s) are included into the list. 1.78 + * @return A list of ZipFileIndex entries, or an empty list 1.79 + */ 1.80 + public synchronized List<ZipFileIndex> getZipFileIndexes(boolean openedOnly) { 1.81 + List<ZipFileIndex> zipFileIndexes = new ArrayList<ZipFileIndex>(); 1.82 + 1.83 + zipFileIndexes.addAll(map.values()); 1.84 + 1.85 + if (openedOnly) { 1.86 + for(ZipFileIndex elem : zipFileIndexes) { 1.87 + if (!elem.isOpen()) { 1.88 + zipFileIndexes.remove(elem); 1.89 + } 1.90 + } 1.91 + } 1.92 + 1.93 + return zipFileIndexes; 1.94 + } 1.95 + 1.96 + public synchronized ZipFileIndex getZipFileIndex(File zipFile, 1.97 + RelativeDirectory symbolFilePrefix, 1.98 + boolean useCache, String cacheLocation, 1.99 + boolean writeIndex) throws IOException { 1.100 + ZipFileIndex zi = getExistingZipIndex(zipFile); 1.101 + 1.102 + if (zi == null || (zi != null && zipFile.lastModified() != zi.zipFileLastModified)) { 1.103 + zi = new ZipFileIndex(zipFile, symbolFilePrefix, writeIndex, 1.104 + useCache, cacheLocation); 1.105 + map.put(zipFile, zi); 1.106 + } 1.107 + return zi; 1.108 + } 1.109 + 1.110 + public synchronized ZipFileIndex getExistingZipIndex(File zipFile) { 1.111 + return map.get(zipFile); 1.112 + } 1.113 + 1.114 + public synchronized void clearCache() { 1.115 + map.clear(); 1.116 + } 1.117 + 1.118 + public synchronized void clearCache(long timeNotUsed) { 1.119 + Iterator<File> cachedFileIterator = map.keySet().iterator(); 1.120 + while (cachedFileIterator.hasNext()) { 1.121 + File cachedFile = cachedFileIterator.next(); 1.122 + ZipFileIndex cachedZipIndex = map.get(cachedFile); 1.123 + if (cachedZipIndex != null) { 1.124 + long timeToTest = cachedZipIndex.lastReferenceTimeStamp + timeNotUsed; 1.125 + if (timeToTest < cachedZipIndex.lastReferenceTimeStamp || // Overflow... 1.126 + System.currentTimeMillis() > timeToTest) { 1.127 + map.remove(cachedFile); 1.128 + } 1.129 + } 1.130 + } 1.131 + } 1.132 + 1.133 + public synchronized void removeFromCache(File file) { 1.134 + map.remove(file); 1.135 + } 1.136 + 1.137 + /** Sets already opened list of ZipFileIndexes from an outside client 1.138 + * of the compiler. This functionality should be used in a non-batch clients of the compiler. 1.139 + */ 1.140 + public synchronized void setOpenedIndexes(List<ZipFileIndex>indexes) throws IllegalStateException { 1.141 + if (map.isEmpty()) { 1.142 + String msg = 1.143 + "Setting opened indexes should be called only when the ZipFileCache is empty. " 1.144 + + "Call JavacFileManager.flush() before calling this method."; 1.145 + throw new IllegalStateException(msg); 1.146 + } 1.147 + 1.148 + for (ZipFileIndex zfi : indexes) { 1.149 + map.put(zfi.zipFile, zfi); 1.150 + } 1.151 + } 1.152 +}