/
ClassLoaderCache.java
121 lines (102 loc) · 3.29 KB
/
ClassLoaderCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.loader;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
/**
* A class/resource cache used by {@link LaunchedURLClassLoader} .
*
* @author Bingjie Lv
* @since 2.7.0
*
*/
public class ClassLoaderCache {
private boolean enableCache = Boolean.getBoolean("loader.cache.enable");
private final int cacheSize = Integer.getInteger("loader.cache.size", 3000);
private Map<String, ClassNotFoundException> classNotFoundExceptionCache;
private Map<String, Optional<URL>> resourceUrlCache;
private Map<String, Optional<Enumeration<URL>>> resourcesUrlCache;
public ClassLoaderCache() {
this.classNotFoundExceptionCache = createCache(this.cacheSize);
this.resourceUrlCache = createCache(this.cacheSize);
this.resourcesUrlCache = createCache(this.cacheSize);
}
public void fastClassNotFoundException(String name) throws ClassNotFoundException {
if (!this.enableCache) {
return;
}
ClassNotFoundException classNotFoundException = this.classNotFoundExceptionCache.get(name);
if (classNotFoundException != null) {
throw classNotFoundException;
}
}
public void cacheClassNotFoundException(String name, ClassNotFoundException exception) {
if (!this.enableCache) {
return;
}
this.classNotFoundExceptionCache.put(name, exception);
}
public Optional<URL> getResourceCache(String name) {
if (!this.enableCache) {
return null;
}
return this.resourceUrlCache.get(name);
}
public URL cacheResourceUrl(String name, URL url) {
if (!this.enableCache) {
return url;
}
this.resourceUrlCache.put(name, (url != null) ? Optional.of(url) : Optional.empty());
return url;
}
public Optional<Enumeration<URL>> getResourcesCache(String name) {
if (!this.enableCache) {
return null;
}
return this.resourcesUrlCache.get(name);
}
public Enumeration<URL> cacheResourceUrls(String name, Enumeration<URL> urlEnumeration) {
if (!this.enableCache) {
return urlEnumeration;
}
if (!urlEnumeration.hasMoreElements()) {
this.resourcesUrlCache.put(name, Optional.of(urlEnumeration));
}
return urlEnumeration;
}
public void clearCache() {
if (this.enableCache) {
this.classNotFoundExceptionCache.clear();
this.resourceUrlCache.clear();
this.resourcesUrlCache.clear();
}
}
public void setEnableCache(boolean enableCache) {
this.enableCache = enableCache;
}
protected <K, V> Map<K, V> createCache(int maxSize) {
return Collections.synchronizedMap(new LinkedHashMap<K, V>(maxSize, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() >= maxSize;
}
});
}
}