1 /* |
|
2 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.javac.file; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.nio.file.Files; |
|
30 import java.nio.file.Path; |
|
31 import java.util.ArrayList; |
|
32 import java.util.HashMap; |
|
33 import java.util.List; |
|
34 import java.util.Map; |
|
35 |
|
36 import com.sun.tools.javac.file.RelativePath.RelativeDirectory; |
|
37 import com.sun.tools.javac.util.Context; |
|
38 |
|
39 |
|
40 /** A cache for ZipFileIndex objects. */ |
|
41 public class ZipFileIndexCache { |
|
42 |
|
43 private final Map<Path, ZipFileIndex> map = new HashMap<>(); |
|
44 |
|
45 /** Get a shared instance of the cache. */ |
|
46 private static ZipFileIndexCache sharedInstance; |
|
47 public synchronized static ZipFileIndexCache getSharedInstance() { |
|
48 if (sharedInstance == null) |
|
49 sharedInstance = new ZipFileIndexCache(); |
|
50 return sharedInstance; |
|
51 } |
|
52 |
|
53 /** Get a context-specific instance of a cache. */ |
|
54 public static ZipFileIndexCache instance(Context context) { |
|
55 ZipFileIndexCache instance = context.get(ZipFileIndexCache.class); |
|
56 if (instance == null) |
|
57 context.put(ZipFileIndexCache.class, instance = new ZipFileIndexCache()); |
|
58 return instance; |
|
59 } |
|
60 |
|
61 /** |
|
62 * Returns a list of all ZipFileIndex entries |
|
63 * |
|
64 * @return A list of ZipFileIndex entries, or an empty list |
|
65 */ |
|
66 public List<ZipFileIndex> getZipFileIndexes() { |
|
67 return getZipFileIndexes(false); |
|
68 } |
|
69 |
|
70 /** |
|
71 * Returns a list of all ZipFileIndex entries |
|
72 * |
|
73 * @param openedOnly If true it returns a list of only opened ZipFileIndex entries, otherwise |
|
74 * all ZipFileEntry(s) are included into the list. |
|
75 * @return A list of ZipFileIndex entries, or an empty list |
|
76 */ |
|
77 public synchronized List<ZipFileIndex> getZipFileIndexes(boolean openedOnly) { |
|
78 List<ZipFileIndex> zipFileIndexes = new ArrayList<>(); |
|
79 |
|
80 zipFileIndexes.addAll(map.values()); |
|
81 |
|
82 if (openedOnly) { |
|
83 for(ZipFileIndex elem : zipFileIndexes) { |
|
84 if (!elem.isOpen()) { |
|
85 zipFileIndexes.remove(elem); |
|
86 } |
|
87 } |
|
88 } |
|
89 |
|
90 return zipFileIndexes; |
|
91 } |
|
92 |
|
93 public synchronized ZipFileIndex getZipFileIndex(Path zipFile, |
|
94 RelativeDirectory symbolFilePrefix, |
|
95 boolean useCache, String cacheLocation, |
|
96 boolean writeIndex) throws IOException { |
|
97 ZipFileIndex zi = getExistingZipIndex(zipFile); |
|
98 |
|
99 if (zi == null || (zi != null && Files.getLastModifiedTime(zipFile).toMillis() != zi.zipFileLastModified)) { |
|
100 zi = new ZipFileIndex(zipFile, symbolFilePrefix, writeIndex, |
|
101 useCache, cacheLocation); |
|
102 map.put(zipFile, zi); |
|
103 } |
|
104 return zi; |
|
105 } |
|
106 |
|
107 public synchronized ZipFileIndex getExistingZipIndex(Path zipFile) { |
|
108 return map.get(zipFile); |
|
109 } |
|
110 |
|
111 public synchronized void clearCache() { |
|
112 map.clear(); |
|
113 } |
|
114 |
|
115 public synchronized void clearCache(long timeNotUsed) { |
|
116 for (Path cachedFile : map.keySet()) { |
|
117 ZipFileIndex cachedZipIndex = map.get(cachedFile); |
|
118 if (cachedZipIndex != null) { |
|
119 long timeToTest = cachedZipIndex.lastReferenceTimeStamp + timeNotUsed; |
|
120 if (timeToTest < cachedZipIndex.lastReferenceTimeStamp || // Overflow... |
|
121 System.currentTimeMillis() > timeToTest) { |
|
122 map.remove(cachedFile); |
|
123 } |
|
124 } |
|
125 } |
|
126 } |
|
127 |
|
128 public synchronized void removeFromCache(Path file) { |
|
129 map.remove(file); |
|
130 } |
|
131 |
|
132 /** Sets already opened list of ZipFileIndexes from an outside client |
|
133 * of the compiler. This functionality should be used in a non-batch clients of the compiler. |
|
134 */ |
|
135 public synchronized void setOpenedIndexes(List<ZipFileIndex>indexes) throws IllegalStateException { |
|
136 if (map.isEmpty()) { |
|
137 String msg = |
|
138 "Setting opened indexes should be called only when the ZipFileCache is empty. " |
|
139 + "Call JavacFileManager.flush() before calling this method."; |
|
140 throw new IllegalStateException(msg); |
|
141 } |
|
142 |
|
143 for (ZipFileIndex zfi : indexes) { |
|
144 map.put(zfi.zipFile, zfi); |
|
145 } |
|
146 } |
|
147 } |
|