Skip to content

Commit

Permalink
Use optimistic locking for QuarkusClassLoader
Browse files Browse the repository at this point in the history
Relates to: quarkusio#42484
  • Loading branch information
geoand committed Sep 2, 2024
1 parent 8620343 commit d8a6927
Showing 1 changed file with 59 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -503,46 +503,52 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
boolean interrupted = Thread.interrupted();
try {
ClassPathResourceIndex classPathResourceIndex = getClassPathResourceIndex();
synchronized (getClassLoadingLock(name)) {
Class<?> c = findLoadedClass(name);
if (c != null) {
return c;
}
String resourceName = fromClassNameToResourceName(name);
if (classPathResourceIndex.isBanned(resourceName)) {
throw new ClassNotFoundException(name);
Class<?> c = findLoadedClass(name);
if (c != null) {
return c;
}
String resourceName = fromClassNameToResourceName(name);
if (classPathResourceIndex.isBanned(resourceName)) {
throw new ClassNotFoundException(name);
}
boolean parentFirst = parentFirst(resourceName, classPathResourceIndex);
if (parentFirst) {
try {
return parent.loadClass(name);
} catch (ClassNotFoundException ignore) {
log.tracef("Class %s not found in parent first load from %s", name, parent);
}
boolean parentFirst = parentFirst(resourceName, classPathResourceIndex);
if (parentFirst) {
}
ClassPathElement classPathElement = classPathResourceIndex.getFirstClassPathElement(resourceName);
if (classPathElement != null) {
final ClassPathResource classPathElementResource = classPathElement.getResource(resourceName);
if (classPathElementResource != null) { //can happen if the class loader was closed
byte[] data = classPathElementResource.getData();
definePackage(name, classPathElement);
Class<?> cl;
try {
return parent.loadClass(name);
} catch (ClassNotFoundException ignore) {
log.tracef("Class %s not found in parent first load from %s", name, parent);
}
}
ClassPathElement classPathElement = classPathResourceIndex.getFirstClassPathElement(resourceName);
if (classPathElement != null) {
final ClassPathResource classPathElementResource = classPathElement.getResource(resourceName);
if (classPathElementResource != null) { //can happen if the class loader was closed
byte[] data = classPathElementResource.getData();
definePackage(name, classPathElement);
Class<?> cl = defineClass(name, data, 0, data.length,
cl = defineClass(name, data, 0, data.length,
protectionDomains.computeIfAbsent(classPathElement,
ClassPathElement::getProtectionDomain));
if (Driver.class.isAssignableFrom(cl)) {
driverLoaded = true;
} catch (LinkageError e) {
cl = findLoadedClass(name);
if (cl == null) {
throw e;
}
return cl;
}
if (Driver.class.isAssignableFrom(cl)) {
driverLoaded = true;
}
return cl;
}
}

if (!parentFirst) {
return parent.loadClass(name);
}

throw new ClassNotFoundException(name);
if (!parentFirst) {
return parent.loadClass(name);
}

throw new ClassNotFoundException(name);

} finally {
if (interrupted) {
//restore interrupt state
Expand All @@ -556,21 +562,31 @@ private void definePackage(String name, ClassPathElement classPathElement) {
//we can't use getPackage here
//if can return a package from the parent
if ((pkgName != null) && definedPackages.get(pkgName) == null) {
synchronized (getClassLoadingLock(pkgName)) {
if (definedPackages.get(pkgName) == null) {
ManifestAttributes manifest = classPathElement.getManifestAttributes();
if (manifest != null) {
definedPackages.put(pkgName, definePackage(pkgName, manifest.getSpecificationTitle(),
manifest.getSpecificationVersion(),
manifest.getSpecificationVendor(),
manifest.getImplementationTitle(),
manifest.getImplementationVersion(),
manifest.getImplementationVendor(), null));
return;
ManifestAttributes manifest = classPathElement.getManifestAttributes();
if (manifest != null) {
try {
definedPackages.put(pkgName, definePackage(pkgName, manifest.getSpecificationTitle(),
manifest.getSpecificationVersion(),
manifest.getSpecificationVendor(),
manifest.getImplementationTitle(),
manifest.getImplementationVersion(),
manifest.getImplementationVendor(), null));
} catch (IllegalArgumentException e) {
var loaded = definedPackages.get(pkgName);
if (loaded == null) {
throw e;
}
}
return;
}

// this could certainly be improved to use the actual manifest
definedPackages.put(pkgName, definePackage(pkgName, null, null, null, null, null, null, null));
// this could certainly be improved to use the actual manifest
try {
definedPackages.put(pkgName, definePackage(pkgName, null, null, null, null, null, null, null));
} catch (IllegalArgumentException e) {
var loaded = definedPackages.get(pkgName);
if (loaded == null) {
throw e;
}
}
}
Expand Down

0 comments on commit d8a6927

Please sign in to comment.