aoqi@0: /* aoqi@0: * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.javac.file; aoqi@0: aoqi@0: import com.sun.tools.javac.file.RelativePath.RelativeDirectory; aoqi@0: import com.sun.tools.javac.util.Context; aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.Iterator; aoqi@0: import java.util.List; aoqi@0: import java.util.Map; aoqi@0: aoqi@0: aoqi@0: /** A cache for ZipFileIndex objects. */ aoqi@0: public class ZipFileIndexCache { aoqi@0: aoqi@0: private final Map map = aoqi@0: new HashMap(); aoqi@0: aoqi@0: /** Get a shared instance of the cache. */ aoqi@0: private static ZipFileIndexCache sharedInstance; aoqi@0: public synchronized static ZipFileIndexCache getSharedInstance() { aoqi@0: if (sharedInstance == null) aoqi@0: sharedInstance = new ZipFileIndexCache(); aoqi@0: return sharedInstance; aoqi@0: } aoqi@0: aoqi@0: /** Get a context-specific instance of a cache. */ aoqi@0: public static ZipFileIndexCache instance(Context context) { aoqi@0: ZipFileIndexCache instance = context.get(ZipFileIndexCache.class); aoqi@0: if (instance == null) aoqi@0: context.put(ZipFileIndexCache.class, instance = new ZipFileIndexCache()); aoqi@0: return instance; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a list of all ZipFileIndex entries aoqi@0: * aoqi@0: * @return A list of ZipFileIndex entries, or an empty list aoqi@0: */ aoqi@0: public List getZipFileIndexes() { aoqi@0: return getZipFileIndexes(false); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a list of all ZipFileIndex entries aoqi@0: * aoqi@0: * @param openedOnly If true it returns a list of only opened ZipFileIndex entries, otherwise aoqi@0: * all ZipFileEntry(s) are included into the list. aoqi@0: * @return A list of ZipFileIndex entries, or an empty list aoqi@0: */ aoqi@0: public synchronized List getZipFileIndexes(boolean openedOnly) { aoqi@0: List zipFileIndexes = new ArrayList(); aoqi@0: aoqi@0: zipFileIndexes.addAll(map.values()); aoqi@0: aoqi@0: if (openedOnly) { aoqi@0: for(ZipFileIndex elem : zipFileIndexes) { aoqi@0: if (!elem.isOpen()) { aoqi@0: zipFileIndexes.remove(elem); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return zipFileIndexes; aoqi@0: } aoqi@0: aoqi@0: public synchronized ZipFileIndex getZipFileIndex(File zipFile, aoqi@0: RelativeDirectory symbolFilePrefix, aoqi@0: boolean useCache, String cacheLocation, aoqi@0: boolean writeIndex) throws IOException { aoqi@0: ZipFileIndex zi = getExistingZipIndex(zipFile); aoqi@0: aoqi@0: if (zi == null || (zi != null && zipFile.lastModified() != zi.zipFileLastModified)) { aoqi@0: zi = new ZipFileIndex(zipFile, symbolFilePrefix, writeIndex, aoqi@0: useCache, cacheLocation); aoqi@0: map.put(zipFile, zi); aoqi@0: } aoqi@0: return zi; aoqi@0: } aoqi@0: aoqi@0: public synchronized ZipFileIndex getExistingZipIndex(File zipFile) { aoqi@0: return map.get(zipFile); aoqi@0: } aoqi@0: aoqi@0: public synchronized void clearCache() { aoqi@0: map.clear(); aoqi@0: } aoqi@0: aoqi@0: public synchronized void clearCache(long timeNotUsed) { aoqi@0: Iterator cachedFileIterator = map.keySet().iterator(); aoqi@0: while (cachedFileIterator.hasNext()) { aoqi@0: File cachedFile = cachedFileIterator.next(); aoqi@0: ZipFileIndex cachedZipIndex = map.get(cachedFile); aoqi@0: if (cachedZipIndex != null) { aoqi@0: long timeToTest = cachedZipIndex.lastReferenceTimeStamp + timeNotUsed; aoqi@0: if (timeToTest < cachedZipIndex.lastReferenceTimeStamp || // Overflow... aoqi@0: System.currentTimeMillis() > timeToTest) { aoqi@0: map.remove(cachedFile); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public synchronized void removeFromCache(File file) { aoqi@0: map.remove(file); aoqi@0: } aoqi@0: aoqi@0: /** Sets already opened list of ZipFileIndexes from an outside client aoqi@0: * of the compiler. This functionality should be used in a non-batch clients of the compiler. aoqi@0: */ aoqi@0: public synchronized void setOpenedIndexes(Listindexes) throws IllegalStateException { aoqi@0: if (map.isEmpty()) { aoqi@0: String msg = aoqi@0: "Setting opened indexes should be called only when the ZipFileCache is empty. " aoqi@0: + "Call JavacFileManager.flush() before calling this method."; aoqi@0: throw new IllegalStateException(msg); aoqi@0: } aoqi@0: aoqi@0: for (ZipFileIndex zfi : indexes) { aoqi@0: map.put(zfi.zipFile, zfi); aoqi@0: } aoqi@0: } aoqi@0: }