Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/cdsadapter/com.ibm.cds/src/com/ibm/cds/CDSBundleFile.java
12910 views
1
/*******************************************************************************
2
* Copyright (c) 2006, 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
23
package com.ibm.cds;
24
25
26
import java.net.MalformedURLException;
27
import java.net.URL;
28
29
import org.eclipse.osgi.storage.bundlefile.BundleEntry;
30
import org.eclipse.osgi.storage.bundlefile.BundleFile;
31
import org.eclipse.osgi.storage.bundlefile.BundleFileWrapper;
32
33
import com.ibm.oti.shared.SharedClassURLHelper;
34
35
/**
36
* Wraps an actual BundleFile object for purposes of loading classes from the
37
* shared classes cache.
38
*/
39
public class CDSBundleFile extends BundleFileWrapper {
40
private URL url; // the URL to the content of the real bundle file
41
private SharedClassURLHelper urlHelper; // the url helper set by the classloader
42
private boolean primed = false;
43
44
/**
45
* The constructor
46
* @param wrapped the real bundle file
47
*/
48
public CDSBundleFile(BundleFile wrapped) {
49
super(wrapped);
50
// get the url to the content of the real bundle file
51
try {
52
this.url = new URL("file", "", wrapped.getBaseFile().getAbsolutePath());
53
} catch (MalformedURLException e) {
54
// do nothing
55
}
56
}
57
58
public CDSBundleFile(BundleFile bundleFile, SharedClassURLHelper urlHelper) {
59
this(bundleFile);
60
this.urlHelper = urlHelper;
61
}
62
63
/*
64
* (non-Javadoc)
65
* @see org.eclipse.osgi.storage.bundlefile.BundleFile#getEntry(java.lang.String)
66
*
67
* If path is not for a class then just use the wrapped bundle file to answer the call.
68
* If the path is for a class, it returns a CDSBundleEntry object.
69
* If the path is for a class, it will look for the magic cookie in the
70
* shared classes cache. If found, the bytes representing the magic cookie are stored in CDSBundleEntry object.
71
*/
72
public BundleEntry getEntry(String path) {
73
String classFileExt = ".class";
74
BundleEntry wrappedEntry = super.getEntry(path);
75
if (wrappedEntry == null) {
76
return null;
77
}
78
if ((false == primed) || (false == path.endsWith(classFileExt))) {
79
return wrappedEntry;
80
}
81
82
byte[] classbytes = getClassBytes(path.substring(0, path.length()-classFileExt.length()));
83
BundleEntry be = new CDSBundleEntry(path, classbytes, wrappedEntry);
84
return be;
85
}
86
87
/**
88
* Returns the file url to the content of the actual bundle file
89
* @return the file url to the content of the actual bundle file
90
*/
91
URL getURL() {
92
return url;
93
}
94
95
/**
96
* Returns the url helper for this bundle file. This is set by the
97
* class loading hook
98
* @return the url helper for this bundle file
99
*/
100
SharedClassURLHelper getURLHelper() {
101
return urlHelper;
102
}
103
104
/**
105
* Sets the url helper for this bundle file. This is called by the
106
* class loading hook.
107
* @param urlHelper the url helper
108
*/
109
void setURLHelper(SharedClassURLHelper urlHelper) {
110
this.urlHelper = urlHelper;
111
this.primed = false; // always unprime when a new urlHelper is set
112
}
113
114
/**
115
* Sets the primed flag for the bundle file. This is called by the
116
* class loading hook after the first class has been loaded from disk for
117
* this bundle file.
118
* @param primed the primed flag
119
*/
120
void setPrimed(boolean primed) {
121
this.primed = primed;
122
}
123
124
/**
125
* Searches in the shared classes cache for the specified class name.
126
* @param name the name of the class
127
* @return the magic cookie to the shared class or null if the class is not in the cache.
128
*/
129
private byte[] getClassBytes(String name) {
130
if (urlHelper == null || url == null)
131
return null;
132
return urlHelper.findSharedClass(url, name);
133
}
134
}
135
136
137