@@ -36,13 +36,23 @@ public final class LoadClass implements PrivilegedAction<Class<?>> {
36
36
37
37
private final ClassLoader classLoader ;
38
38
39
+ /**
40
+ * when true, it will check the Thread Context ClassLoader when the class is not found in the provided one
41
+ */
42
+ private final boolean fallbackOnTCCL ;
43
+
39
44
public static LoadClass action (String className , ClassLoader classLoader ) {
40
- return new LoadClass ( className , classLoader );
45
+ return new LoadClass ( className , classLoader , true );
46
+ }
47
+
48
+ public static LoadClass action (String className , ClassLoader classLoader , boolean fallbackOnTCCL ) {
49
+ return new LoadClass ( className , classLoader , fallbackOnTCCL );
41
50
}
42
51
43
- private LoadClass (String className , ClassLoader classLoader ) {
52
+ private LoadClass (String className , ClassLoader classLoader , boolean fallbackOnTCCL ) {
44
53
this .className = className ;
45
54
this .classLoader = classLoader ;
55
+ this .fallbackOnTCCL = fallbackOnTCCL ;
46
56
}
47
57
48
58
@ Override
@@ -67,49 +77,62 @@ private Class<?> loadClassInValidatorNameSpace() {
67
77
catch ( RuntimeException e ) {
68
78
// ignore
69
79
}
70
- try {
71
- ClassLoader contextClassLoader = Thread .currentThread ().getContextClassLoader ();
72
- if ( contextClassLoader != null ) {
73
- return Class .forName ( className , false , contextClassLoader );
80
+ if ( fallbackOnTCCL ) {
81
+ try {
82
+ ClassLoader contextClassLoader = Thread .currentThread ().getContextClassLoader ();
83
+ if ( contextClassLoader != null ) {
84
+ return Class .forName ( className , false , contextClassLoader );
85
+ }
86
+ else {
87
+ throw log .getUnableToLoadClassException ( className );
88
+ }
74
89
}
75
- else {
76
- throw log .getUnableToLoadClassException ( className );
90
+ catch ( ClassNotFoundException e ) {
91
+ throw log .getUnableToLoadClassException ( className , e );
77
92
}
78
93
}
79
- catch ( ClassNotFoundException e ) {
80
- throw log .getUnableToLoadClassException ( className , e );
94
+ else {
95
+ throw log .getUnableToLoadClassException ( className );
81
96
}
82
97
}
83
98
84
99
private Class <?> loadNonValidatorClass () {
100
+ Exception exception = null ;
85
101
try {
86
102
if ( classLoader != null ) {
87
103
return Class .forName ( className , false , classLoader );
88
104
}
89
105
}
90
106
catch ( ClassNotFoundException e ) {
91
107
// ignore - try using the classloader of the caller first
108
+ exception = e ;
92
109
}
93
110
catch ( RuntimeException e ) {
94
111
// ignore
112
+ exception = e ;
95
113
}
96
- try {
97
- ClassLoader contextClassLoader = Thread .currentThread ().getContextClassLoader ();
98
- if ( contextClassLoader != null ) {
99
- return Class .forName ( className , false , contextClassLoader );
114
+ if ( fallbackOnTCCL ) {
115
+ try {
116
+ ClassLoader contextClassLoader = Thread .currentThread ().getContextClassLoader ();
117
+ if ( contextClassLoader != null ) {
118
+ return Class .forName ( className , false , contextClassLoader );
119
+ }
120
+ }
121
+ catch ( ClassNotFoundException e ) {
122
+ // ignore - try using the classloader of the caller first
123
+ }
124
+ catch ( RuntimeException e ) {
125
+ // ignore
126
+ }
127
+ try {
128
+ return Class .forName ( className , true , LoadClass .class .getClassLoader () );
129
+ }
130
+ catch ( ClassNotFoundException e ) {
131
+ throw log .getUnableToLoadClassException ( className , e );
100
132
}
101
133
}
102
- catch ( ClassNotFoundException e ) {
103
- // ignore - try using the classloader of the caller first
104
- }
105
- catch ( RuntimeException e ) {
106
- // ignore
107
- }
108
- try {
109
- return Class .forName ( className , true , LoadClass .class .getClassLoader () );
110
- }
111
- catch ( ClassNotFoundException e ) {
112
- throw log .getUnableToLoadClassException ( className , e );
134
+ else {
135
+ throw log .getUnableToLoadClassException ( className , exception );
113
136
}
114
137
}
115
138
}
0 commit comments