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

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 0
959103a6100f
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.file;
aoqi@0 27
aoqi@0 28 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
aoqi@0 29 import com.sun.tools.javac.util.Context;
aoqi@0 30 import java.io.File;
aoqi@0 31 import java.io.IOException;
aoqi@0 32 import java.util.ArrayList;
aoqi@0 33 import java.util.HashMap;
aoqi@0 34 import java.util.Iterator;
aoqi@0 35 import java.util.List;
aoqi@0 36 import java.util.Map;
aoqi@0 37
aoqi@0 38
aoqi@0 39 /** A cache for ZipFileIndex objects. */
aoqi@0 40 public class ZipFileIndexCache {
aoqi@0 41
aoqi@0 42 private final Map<File, ZipFileIndex> map =
aoqi@0 43 new HashMap<File, ZipFileIndex>();
aoqi@0 44
aoqi@0 45 /** Get a shared instance of the cache. */
aoqi@0 46 private static ZipFileIndexCache sharedInstance;
aoqi@0 47 public synchronized static ZipFileIndexCache getSharedInstance() {
aoqi@0 48 if (sharedInstance == null)
aoqi@0 49 sharedInstance = new ZipFileIndexCache();
aoqi@0 50 return sharedInstance;
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 /** Get a context-specific instance of a cache. */
aoqi@0 54 public static ZipFileIndexCache instance(Context context) {
aoqi@0 55 ZipFileIndexCache instance = context.get(ZipFileIndexCache.class);
aoqi@0 56 if (instance == null)
aoqi@0 57 context.put(ZipFileIndexCache.class, instance = new ZipFileIndexCache());
aoqi@0 58 return instance;
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 /**
aoqi@0 62 * Returns a list of all ZipFileIndex entries
aoqi@0 63 *
aoqi@0 64 * @return A list of ZipFileIndex entries, or an empty list
aoqi@0 65 */
aoqi@0 66 public List<ZipFileIndex> getZipFileIndexes() {
aoqi@0 67 return getZipFileIndexes(false);
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 /**
aoqi@0 71 * Returns a list of all ZipFileIndex entries
aoqi@0 72 *
aoqi@0 73 * @param openedOnly If true it returns a list of only opened ZipFileIndex entries, otherwise
aoqi@0 74 * all ZipFileEntry(s) are included into the list.
aoqi@0 75 * @return A list of ZipFileIndex entries, or an empty list
aoqi@0 76 */
aoqi@0 77 public synchronized List<ZipFileIndex> getZipFileIndexes(boolean openedOnly) {
aoqi@0 78 List<ZipFileIndex> zipFileIndexes = new ArrayList<ZipFileIndex>();
aoqi@0 79
aoqi@0 80 zipFileIndexes.addAll(map.values());
aoqi@0 81
aoqi@0 82 if (openedOnly) {
aoqi@0 83 for(ZipFileIndex elem : zipFileIndexes) {
aoqi@0 84 if (!elem.isOpen()) {
aoqi@0 85 zipFileIndexes.remove(elem);
aoqi@0 86 }
aoqi@0 87 }
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 return zipFileIndexes;
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 public synchronized ZipFileIndex getZipFileIndex(File zipFile,
aoqi@0 94 RelativeDirectory symbolFilePrefix,
aoqi@0 95 boolean useCache, String cacheLocation,
aoqi@0 96 boolean writeIndex) throws IOException {
aoqi@0 97 ZipFileIndex zi = getExistingZipIndex(zipFile);
aoqi@0 98
aoqi@0 99 if (zi == null || (zi != null && zipFile.lastModified() != zi.zipFileLastModified)) {
aoqi@0 100 zi = new ZipFileIndex(zipFile, symbolFilePrefix, writeIndex,
aoqi@0 101 useCache, cacheLocation);
aoqi@0 102 map.put(zipFile, zi);
aoqi@0 103 }
aoqi@0 104 return zi;
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 public synchronized ZipFileIndex getExistingZipIndex(File zipFile) {
aoqi@0 108 return map.get(zipFile);
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 public synchronized void clearCache() {
aoqi@0 112 map.clear();
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 public synchronized void clearCache(long timeNotUsed) {
aoqi@0 116 Iterator<File> cachedFileIterator = map.keySet().iterator();
aoqi@0 117 while (cachedFileIterator.hasNext()) {
aoqi@0 118 File cachedFile = cachedFileIterator.next();
aoqi@0 119 ZipFileIndex cachedZipIndex = map.get(cachedFile);
aoqi@0 120 if (cachedZipIndex != null) {
aoqi@0 121 long timeToTest = cachedZipIndex.lastReferenceTimeStamp + timeNotUsed;
aoqi@0 122 if (timeToTest < cachedZipIndex.lastReferenceTimeStamp || // Overflow...
aoqi@0 123 System.currentTimeMillis() > timeToTest) {
aoqi@0 124 map.remove(cachedFile);
aoqi@0 125 }
aoqi@0 126 }
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 public synchronized void removeFromCache(File file) {
aoqi@0 131 map.remove(file);
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 /** Sets already opened list of ZipFileIndexes from an outside client
aoqi@0 135 * of the compiler. This functionality should be used in a non-batch clients of the compiler.
aoqi@0 136 */
aoqi@0 137 public synchronized void setOpenedIndexes(List<ZipFileIndex>indexes) throws IllegalStateException {
aoqi@0 138 if (map.isEmpty()) {
aoqi@0 139 String msg =
aoqi@0 140 "Setting opened indexes should be called only when the ZipFileCache is empty. "
aoqi@0 141 + "Call JavacFileManager.flush() before calling this method.";
aoqi@0 142 throw new IllegalStateException(msg);
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 for (ZipFileIndex zfi : indexes) {
aoqi@0 146 map.put(zfi.zipFile, zfi);
aoqi@0 147 }
aoqi@0 148 }
aoqi@0 149 }

mercurial