From 0ed0a786a3bbe56c5821b9293410b56511aa7ac0 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Fri, 6 Mar 2015 00:16:59 +0600 Subject: [PATCH] WICKET-5850 Fix class loading issue in LazyInitProxyFactory in case of multimodule deployment (cherry picked from commit e00c54c) --- .../wicket/proxy/LazyInitProxyFactory.java | 62 ++++++++++++------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/wicket-ioc/src/main/java/org/apache/wicket/proxy/LazyInitProxyFactory.java b/wicket-ioc/src/main/java/org/apache/wicket/proxy/LazyInitProxyFactory.java index b4e821b179b..e1b33d69104 100644 --- a/wicket-ioc/src/main/java/org/apache/wicket/proxy/LazyInitProxyFactory.java +++ b/wicket-ioc/src/main/java/org/apache/wicket/proxy/LazyInitProxyFactory.java @@ -137,20 +137,7 @@ else if (type.isInterface()) try { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - if (Application.exists()) - { - IClassResolver classResolver = Application.get() - .getApplicationSettings() - .getClassResolver(); - - if (classResolver != null) - { - classLoader = classResolver.getClassLoader(); - } - } - - return Proxy.newProxyInstance(classLoader, + return Proxy.newProxyInstance(resolveClassLoader(), new Class[] { type, Serializable.class, ILazyInitProxy.class, IWriteReplace.class }, handler); } @@ -173,25 +160,33 @@ else if (type.isInterface()) CGLibInterceptor handler = new CGLibInterceptor(type, locator); Enhancer e = new Enhancer(); + e.setClassLoader(resolveClassLoader()); e.setInterfaces(new Class[] { Serializable.class, ILazyInitProxy.class, IWriteReplace.class }); e.setSuperclass(type); e.setCallback(handler); - e.setNamingPolicy(new DefaultNamingPolicy() - { - @Override - public String getClassName(final String prefix, final String source, - final Object key, final Predicate names) - { - return super.getClassName("WICKET_" + prefix, source, key, names); - } - }); + e.setNamingPolicy(WicketNamingPolicy.INSTANCE); return e.create(); } } - /** + private static ClassLoader resolveClassLoader() + { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if (Application.exists()) + { + IClassResolver classResolver = Application.get().getApplicationSettings() + .getClassResolver(); + if (classResolver != null) + { + classLoader = classResolver.getClassLoader(); + } + } + return classLoader; + } + + /** * This interface is used to make the proxy forward writeReplace() call to the handler instead * of invoking it on itself. This allows us to serialize the replacement object instead of the * proxy itself in case the proxy subclass is deserialized on a VM that does not have it @@ -524,4 +519,23 @@ public static boolean isWriteReplaceMethod(final Method method) return (method.getReturnType() == Object.class) && (method.getParameterTypes().length == 0) && method.getName().equals("writeReplace"); } + + private static final class WicketNamingPolicy extends DefaultNamingPolicy + { + + private static final WicketNamingPolicy INSTANCE = new WicketNamingPolicy(); + + private WicketNamingPolicy() + { + super(); + } + + @Override + public String getClassName(final String prefix, final String source, final Object key, + final Predicate names) + { + return super.getClassName("WICKET_" + prefix, source, key, names); + } + } + }