Path: blob/master/cdsadapter/com.ibm.cds/src/com/ibm/cds/CDSBundleFile.java
12910 views
/*******************************************************************************1* Copyright (c) 2006, 2018 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/2122package com.ibm.cds;232425import java.net.MalformedURLException;26import java.net.URL;2728import org.eclipse.osgi.storage.bundlefile.BundleEntry;29import org.eclipse.osgi.storage.bundlefile.BundleFile;30import org.eclipse.osgi.storage.bundlefile.BundleFileWrapper;3132import com.ibm.oti.shared.SharedClassURLHelper;3334/**35* Wraps an actual BundleFile object for purposes of loading classes from the36* shared classes cache.37*/38public class CDSBundleFile extends BundleFileWrapper {39private URL url; // the URL to the content of the real bundle file40private SharedClassURLHelper urlHelper; // the url helper set by the classloader41private boolean primed = false;4243/**44* The constructor45* @param wrapped the real bundle file46*/47public CDSBundleFile(BundleFile wrapped) {48super(wrapped);49// get the url to the content of the real bundle file50try {51this.url = new URL("file", "", wrapped.getBaseFile().getAbsolutePath());52} catch (MalformedURLException e) {53// do nothing54}55}5657public CDSBundleFile(BundleFile bundleFile, SharedClassURLHelper urlHelper) {58this(bundleFile);59this.urlHelper = urlHelper;60}6162/*63* (non-Javadoc)64* @see org.eclipse.osgi.storage.bundlefile.BundleFile#getEntry(java.lang.String)65*66* If path is not for a class then just use the wrapped bundle file to answer the call.67* If the path is for a class, it returns a CDSBundleEntry object.68* If the path is for a class, it will look for the magic cookie in the69* shared classes cache. If found, the bytes representing the magic cookie are stored in CDSBundleEntry object.70*/71public BundleEntry getEntry(String path) {72String classFileExt = ".class";73BundleEntry wrappedEntry = super.getEntry(path);74if (wrappedEntry == null) {75return null;76}77if ((false == primed) || (false == path.endsWith(classFileExt))) {78return wrappedEntry;79}8081byte[] classbytes = getClassBytes(path.substring(0, path.length()-classFileExt.length()));82BundleEntry be = new CDSBundleEntry(path, classbytes, wrappedEntry);83return be;84}8586/**87* Returns the file url to the content of the actual bundle file88* @return the file url to the content of the actual bundle file89*/90URL getURL() {91return url;92}9394/**95* Returns the url helper for this bundle file. This is set by the96* class loading hook97* @return the url helper for this bundle file98*/99SharedClassURLHelper getURLHelper() {100return urlHelper;101}102103/**104* Sets the url helper for this bundle file. This is called by the105* class loading hook.106* @param urlHelper the url helper107*/108void setURLHelper(SharedClassURLHelper urlHelper) {109this.urlHelper = urlHelper;110this.primed = false; // always unprime when a new urlHelper is set111}112113/**114* Sets the primed flag for the bundle file. This is called by the115* class loading hook after the first class has been loaded from disk for116* this bundle file.117* @param primed the primed flag118*/119void setPrimed(boolean primed) {120this.primed = primed;121}122123/**124* Searches in the shared classes cache for the specified class name.125* @param name the name of the class126* @return the magic cookie to the shared class or null if the class is not in the cache.127*/128private byte[] getClassBytes(String name) {129if (urlHelper == null || url == null)130return null;131return urlHelper.findSharedClass(url, name);132}133}134135136137