Skip to content

Commit

Permalink
add loadClass(...,defaultValue)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 23, 2024
1 parent 971590d commit 07f3772
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.util.HashMap;
import java.util.Map;

public class CombinedClassLoader extends ClassLoader {
import lucee.commons.lang.ClassLoaderDefault;
import lucee.commons.lang.ClassUtil;

public class CombinedClassLoader extends ClassLoader implements ClassLoaderDefault {

private final ClassLoader loader;
private final ClassLoader core;
Expand All @@ -31,6 +34,53 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
}
}

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
try {
// Try loading with the primary (OSGi) class loader first
return core.loadClass(name);
}
catch (ClassNotFoundException e) {
// If not found, delegate to the secondary (main) class loader
return loader.loadClass(name);
}
}

@Override
public Class<?> loadClass(String name, boolean resolve, Class<?> defaultValue) {
Class<?> c;
if (core instanceof ClassLoaderDefault) {
c = ((ClassLoaderDefault) core).loadClass(name, resolve, defaultValue);
if (c != null) return c;
}
else {
if (ClassUtil.isClassAvailable(core, name)) {
try {
return core.loadClass(name);
}
catch (ClassNotFoundException e) {

}
}
}

if (loader instanceof ClassLoaderDefault) {
c = ((ClassLoaderDefault) loader).loadClass(name, resolve, defaultValue);
if (c != null) return c;
}
else {
if (ClassUtil.isClassAvailable(loader, name)) {
try {
return loader.loadClass(name);
}
catch (ClassNotFoundException e) {

}
}
}
return defaultValue;
}

@Override
public URL getResource(String name) {
URL resource = core.getResource(name);
Expand Down
29 changes: 28 additions & 1 deletion core/src/main/java/lucee/commons/lang/ArchiveClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

// TODO umbauen auf ZipInputStream oder ein wrapper schreiben fuer resorces der das file interface einhaelt

public final class ArchiveClassLoader extends ClassLoader implements Closeable {
public final class ArchiveClassLoader extends ClassLoader implements Closeable, ClassLoaderDefault {

private final ZipFile zip;
private final ClassLoader pcl;
Expand Down Expand Up @@ -116,6 +116,33 @@ protected synchronized Class loadClass(String name, boolean resolve) throws Clas
return c;
}

@Override
public Class<?> loadClass(String name, boolean resolve, Class<?> defaultValue) {
// First, check if the class has already been loaded
Class c = findLoadedClass(name);
if (c == null) {
c = findClassEL(name);
if (c == null) {
if (pcl instanceof ClassLoaderDefault) {
c = ((ClassLoaderDefault) pcl).loadClass(name, false, null);
if (c == null) return defaultValue;
}
else {
try {
c = pcl.loadClass(name);
}
catch (ClassNotFoundException e) {
return defaultValue;
}
}
}
}
if (resolve) {
resolveClass(c);
}
return c;
}

@Override
protected Class findClass(String name) throws ClassNotFoundException {
Class clazz = findClassEL(name);
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/lucee/commons/lang/ClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1029,4 +1029,9 @@ public static Object newInstance(Class clazz)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, PageException {
return Reflector.getConstructorInstance(clazz, EMPTY_OBJ, true).invoke();
}

public static boolean isClassAvailable(ClassLoader loader, String className) {
String resourcePath = className.replace('.', '/').concat(".class");
return loader.getResource(resourcePath) != null;
}
}
34 changes: 32 additions & 2 deletions core/src/main/java/lucee/commons/lang/MemoryClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* ClassLoader that loads classes in memory that are not stored somewhere physically
*/
public final class MemoryClassLoader extends ClassLoader implements ExtendableClassLoader {
public final class MemoryClassLoader extends ClassLoader implements ExtendableClassLoader, ClassLoaderDefault {
static {
boolean res = registerAsParallelCapable();
}
Expand Down Expand Up @@ -77,6 +77,36 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
return c;
}

@Override
public Class<?> loadClass(String name, boolean resolve, Class<?> defaultValue) {
Class<?> c = findLoadedClass(name);
if (c == null) {
synchronized (SystemUtil.createToken("MemoryClassLoader", name)) {
c = findLoadedClass(name);
if (c == null) {
if (pcl instanceof ClassLoaderDefault) {
c = ((ClassLoaderDefault) pcl).loadClass(name, resolve, null);
if (c == null) return defaultValue;
resolve = false;
}
else {
try {
c = pcl.loadClass(name);// if(name.indexOf("sub")!=-1)print.ds(name);
}
catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
return defaultValue;
}
}
if (resolve && c != null) {
resolveClass(c);
}
}
}
}
return c;
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
throw new ClassNotFoundException("class " + name + " is invalid or doesn't exist");
Expand All @@ -88,7 +118,7 @@ public Class<?> loadClass(String name, byte[] barr) throws UnmodifiableClassExce

Class<?> clazz = null;
try {
clazz = loadClass(name);
clazz = loadClass(name, false);
}
catch (ClassNotFoundException cnf) {
LogUtil.warn("memory-classloader", cnf);
Expand Down
73 changes: 61 additions & 12 deletions core/src/main/java/lucee/commons/lang/PhysicalClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import org.apache.felix.framework.BundleWiringImpl.BundleClassLoader;

import lucee.print;
import lucee.commons.digest.HashUtil;
import lucee.commons.io.CharsetUtil;
import lucee.commons.io.IOUtil;
Expand Down Expand Up @@ -62,7 +63,7 @@
/**
* Directory ClassLoader
*/
public final class PhysicalClassLoader extends URLClassLoader implements ExtendableClassLoader {
public final class PhysicalClassLoader extends URLClassLoader implements ExtendableClassLoader, ClassLoaderDefault {

static {
boolean res = registerAsParallelCapable();
Expand Down Expand Up @@ -201,32 +202,59 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
return loadClass(name, resolve, true);
}

private Class<?> loadClass(String name, boolean resolve, boolean loadFromFS) throws ClassNotFoundException {
public boolean isClassAvailable(ClassLoader loader, String className) {
if (allLoadedClasses.containsKey(className)) return true;
if (unavaiClasses.containsKey(className)) return false;

String resourcePath = className.replace('.', '/').concat(".class");
return loader.getResource(resourcePath) != null;
}

@Override
public Class<?> loadClass(String name, boolean resolve, Class<?> defaultValue) {
return loadClass(name, resolve, true, defaultValue);
}

private Class<?> loadClass(String name, boolean resolve, boolean loadFromFS, Class<?> defaultValue) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);

if (c == null) {
synchronized (SystemUtil.createToken("PhysicalClassLoader:load", name)) {
c = findLoadedClass(name);
if (c == null) {
try {
c = super.loadClass(name, resolve);
ClassLoader pcl = getParent();
if (pcl instanceof ClassLoaderDefault) {
print.e("-" + pcl + ">" + name);
c = ((ClassLoaderDefault) pcl).loadClass(name, resolve, null);
}
catch (Exception e) {
LogUtil.warn("physical-classloader", e);
else {
print.e("=" + pcl + ">" + name);
try {
c = super.loadClass(name, resolve);
}
catch (Exception e) {
}
}

if (addionalClassLoader != null) {
if (c == null && addionalClassLoader != null) {
try {
c = addionalClassLoader.loadClass(name);
}
catch (Exception e) {
LogUtil.warn("physical-classloader", e);
}
}

if (c == null) {
if (loadFromFS) c = findClass(name);
else throw new ClassNotFoundException(name);
if (loadFromFS) {
try {
c = findClass(name);
}
catch (ClassNotFoundException e) {
return defaultValue;
}
}
else return defaultValue;
}
}
}
Expand All @@ -235,6 +263,12 @@ private Class<?> loadClass(String name, boolean resolve, boolean loadFromFS) thr
return c;
}

private Class<?> loadClass(String name, boolean resolve, boolean loadFromFS) throws ClassNotFoundException {
Class<?> c = loadClass(name, resolve, loadFromFS, null);
if (c == null) throw new ClassNotFoundException(name);
return c;
}

@Override
public Class<?> loadClass(String name, byte[] barr) throws UnmodifiableClassException {
// Class<?> clazz = null;
Expand Down Expand Up @@ -273,8 +307,7 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
throw new ClassNotFoundException("Class [" + name + "] is invalid or doesn't exist");
}

byte[] barr = read(name);
return _loadClass(name, barr, false);
return _loadClass(name, read(name), false);
}
}

Expand All @@ -294,6 +327,22 @@ private byte[] read(String name) throws ClassNotFoundException {
return baos.toByteArray();
}

private byte[] read(String name, byte[] defaultValue) {
Resource res = directory.getRealResource(name.replace('.', '/').concat(".class"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
IOUtil.copy(res, baos, false);
}
catch (IOException e) {
this.unavaiClasses.put(name, "");
return defaultValue;
}
finally {
IOUtil.closeEL(baos);
}
return baos.toByteArray();
}

private Class<?> rename(Class<?> clazz, byte[] barr) {
String newName = clazz.getName() + "$" + uid();
return _loadClass(newName, ClassRenamer.rename(barr, newName), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import lucee.commons.io.log.Log;
import lucee.commons.io.res.Resource;
import lucee.commons.io.res.util.ResourceUtil;
import lucee.commons.lang.ClassLoaderDefault;
import lucee.commons.lang.ExtendableClassLoader;

/**
* Directory ClassLoader
*/
public final class DynamicClassLoader extends ClassLoader implements ExtendableClassLoader {
public final class DynamicClassLoader extends ClassLoader implements ExtendableClassLoader, ClassLoaderDefault {

static {
boolean res = registerAsParallelCapable();
Expand Down Expand Up @@ -152,21 +153,40 @@ public Class<?> loadClass(String className, byte[] barr) throws UnmodifiableClas
return clazz;
}

private Class<?> loadClass(String name, boolean resolve, boolean loadFromFS) throws ClassNotFoundException {
@Override
public Class<?> loadClass(String name, boolean resolve, Class<?> defaultValue) {
return loadClass(name, resolve, true, defaultValue);
}

private Class<?> loadClass(String name, boolean resolve, boolean loadFromFS, Class<?> defaultValue) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
synchronized (SystemUtil.createToken("DynamicClassLoader:load", name)) {
c = findLoadedClass(name);
if (c == null) {
try {
c = getParent().loadClass(name);
ClassLoader pcl = getParent();
if (pcl instanceof ClassLoaderDefault) {
c = ((ClassLoaderDefault) pcl).loadClass(name, resolve, null);
}
catch (Exception e) {
else {
try {
c = pcl.loadClass(name);
}
catch (Exception e) {
}
}

if (c == null) {
if (loadFromFS) c = findClass(name);
else throw new ClassNotFoundException(name);
if (loadFromFS) {
try {
c = findClass(name);
}
catch (ClassNotFoundException e) {
return defaultValue;
}
}
else return defaultValue;
}
}
}
Expand All @@ -175,6 +195,12 @@ private Class<?> loadClass(String name, boolean resolve, boolean loadFromFS) thr
return c;
}

private Class<?> loadClass(String name, boolean resolve, boolean loadFromFS) throws ClassNotFoundException {
Class<?> c = loadClass(name, resolve, loadFromFS, null);
if (c == null) throw new ClassNotFoundException(name);
return c;
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {// if(name.indexOf("sub")!=-1)print.ds(name);
byte[] barr = read(name);
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.2.0.248-SNAPSHOT"/>
<property name="version" value="6.2.0.249-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.2.0.248-SNAPSHOT</version>
<version>6.2.0.249-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 07f3772

Please sign in to comment.