Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/shareClassTests/DataHelperTests/src/CustomClassloaders/DataCachingClassLoader.java
6005 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2018 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package CustomClassloaders;
23
24
import java.io.BufferedInputStream;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.net.URL;
28
import java.net.URLClassLoader;
29
import java.nio.ByteBuffer;
30
import java.io.ByteArrayOutputStream;
31
32
import com.ibm.oti.shared.Shared;
33
import com.ibm.oti.shared.SharedDataHelper;
34
import com.ibm.oti.shared.SharedDataHelperFactory;
35
36
/**
37
* Uses the shared cache to store some resource data. In this implementation the getResourceAsStream() method
38
* is wired to use the shared cache.
39
*/
40
public class DataCachingClassLoader extends URLClassLoader {
41
42
private static final boolean debug = true; // ==true means logging messages come out as this loader executes
43
44
// Classloader users can check the counters, reset them with 'resetCounters()'
45
public int cacheStatsFindCount; /* Number of times we have called findSharedData() */
46
public int cacheStatsStoreCount; /* Number of times we have called storeSharedData() */
47
public int cacheStatsCacheHitCount; /* Number of times we have successfully found the data in the cache on findSharedData() */
48
public int cacheStatsCacheMissCount; /* Number of times we have not found the data in the cache on findSharedData() */
49
50
// Control whether the cache should be used
51
public boolean storeInCache = true;
52
public boolean findInCache = true;
53
54
private SharedDataHelper sdHelper;
55
56
// ---
57
58
public DataCachingClassLoader(URL[] urls, ClassLoader parent) {
59
super(urls, parent);
60
SharedDataHelperFactory helperFactory = Shared.getSharedDataHelperFactory();
61
if (helperFactory==null) throw new RuntimeException("No SharedDataHelperFactory found, are you running -Xshareclasses?");
62
sdHelper = (helperFactory==null?null:helperFactory.getDataHelper(this));
63
resetCounters();
64
}
65
66
@Override
67
public InputStream getResourceAsStream(String name) {
68
if (findInCache && sdHelper!=null) {
69
ByteBuffer bBuffer = sdHelper.findSharedData(name);
70
if (bBuffer!=null) {
71
log("Returning an entry found in the cache");
72
return newInputStream(bBuffer);
73
}
74
}
75
InputStream realResource = super.getResourceAsStream(name);
76
if (realResource!=null && storeInCache && sdHelper!=null) {
77
log("Caching the data with token '"+name+"'");
78
BufferedInputStream bis = new BufferedInputStream(realResource);
79
ByteArrayOutputStream baos = new ByteArrayOutputStream();
80
byte[] data = new byte[256];
81
int readBytes = -1;
82
try {
83
while ( (readBytes=bis.read(data))!=-1) {
84
baos.write(data,0,readBytes);
85
}
86
} catch (IOException e) {
87
e.printStackTrace();
88
}
89
byte[] dataBytes = baos.toByteArray();
90
ByteBuffer bBuffer = ByteBuffer.allocateDirect(dataBytes.length);
91
bBuffer.put(dataBytes);
92
bBuffer = sdHelper.storeSharedData(name, bBuffer);
93
if (bBuffer==null) throw new RuntimeException("storeSharedData('"+name+"',...) has failed!");
94
return newInputStream(bBuffer);
95
} else {
96
return realResource;
97
}
98
99
}
100
101
102
/** Return an InputStream that wraps a ByteBuffer */
103
public static InputStream newInputStream(final ByteBuffer buf) {
104
return new InputStream() {
105
public synchronized int read() throws IOException {
106
if (!buf.hasRemaining()) {
107
return -1;
108
}
109
return buf.get();
110
}
111
112
public synchronized int read(byte[] bytes, int off, int len) throws IOException {
113
if (!buf.hasRemaining()) {
114
return -1;
115
}
116
// Read only what's left
117
len = Math.min(len, buf.remaining());
118
buf.get(bytes, off, len);
119
return len;
120
}
121
};
122
}
123
124
public void resetCounters() {
125
cacheStatsCacheHitCount = 0;
126
cacheStatsCacheMissCount = 0;
127
cacheStatsFindCount = 0;
128
cacheStatsStoreCount = 0;
129
}
130
131
// -- operations that work directly on the cache
132
133
public InputStream findInCache(String token) {
134
if (sdHelper!=null) {
135
ByteBuffer bBuffer = sdHelper.findSharedData(token);
136
if (bBuffer!=null) return newInputStream(bBuffer);
137
}
138
return null;
139
}
140
141
public void markStale(String token) {
142
if (sdHelper!=null) {
143
sdHelper.storeSharedData(token, null);
144
}
145
}
146
147
// --- helpers
148
private void log(String msg) {
149
if (debug) System.out.println("DataCachingClassLoader: "+msg);
150
}
151
152
public void storeNull(String token) {
153
if (sdHelper!=null) {
154
sdHelper.storeSharedData(token, null);
155
}
156
}
157
158
public boolean forceStore(String token,String data) {
159
byte[] dataBytes = data.getBytes();
160
ByteBuffer bBuffer = ByteBuffer.allocateDirect(dataBytes.length);
161
bBuffer.put(dataBytes);
162
bBuffer = sdHelper.storeSharedData(token, bBuffer);
163
return (bBuffer!=null);
164
}
165
}
166
167