Skip to content

Commit

Permalink
JPMS improvements: Experiments with GF Embedded
Browse files Browse the repository at this point in the history
Discovered a flow in the solution for ProxyServiceImpl
  • Loading branch information
OndroMih committed Sep 2, 2024
1 parent 7cd0dc3 commit f507d40
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private static boolean useMethodHandles(final ClassLoader loader, final Class<?>
}
// The bootstrap CL used by embedded glassfish doesn't remember generated classes
// Further ClassLoader.findClass calls will fail.
if (anchorClass == null || loader.getParent() == null || loader.getClass() == ASURLClassLoader.class) {
if (anchorClass == null || loader.getClass() == ASURLClassLoader.class) {
return false;
}
// Use MethodHandles.Lookup only if the anchor run-time Package defined by CL.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public class ProxyServicesImpl implements ProxyServices {

private final ClassLoaderHierarchy classLoaderHierarchy;

private Map<ClassLoader, ClassPool> classPoolMap = Collections.synchronizedMap(new WeakHashMap<>());
// probably shouldn't be static but moved to a singleton service
private static Map<ClassLoader, ClassPool> classPoolMap = Collections.synchronizedMap(new WeakHashMap<>());

/**
* @param services immediately used to find a {@link ClassLoaderHierarchy} service
Expand All @@ -59,13 +60,16 @@ public ProxyServicesImpl(final ServiceLocator services) {
classLoaderHierarchy = services.getService(ClassLoaderHierarchy.class);
}

/* This solution is not ideal - it creates the new class in the CL of the originalClass
(most often server CL or bootstrap CL), while the previous solution created it in the app classloader.
*/
@Override
public Class<?> defineClass(final Class<?> originalClass, final String className, final byte[] classBytes,
final int off, final int len) throws ClassFormatError {
try {
final String originalPackageName = originalClass.getPackageName();
String modifiedClassName = originalClass.getName() + "_GlassFishWeldProxy";
final ClassLoader originalClassCL = getClassLoaderforBean(originalClass);
final ClassLoader originalClassCL = originalClass.getClassLoader();
final ClassPool classPool = classPoolMap.computeIfAbsent(originalClassCL, cl -> new ClassPool());
while (classPool.getOrNull(modifiedClassName) != null) {
modifiedClassName += "_";
Expand Down

0 comments on commit f507d40

Please sign in to comment.