@@ -42,12 +42,13 @@ public class JavaFunctionBroker {
42
42
private volatile InvocationChainFactory invocationChainFactory ;
43
43
private volatile FunctionInstanceInjector functionInstanceInjector ;
44
44
private final Object oneTimeLogicInitializationLock = new Object ();
45
- private List <Middleware > baseMiddlewares = new ArrayList <>();
45
+ private List <Middleware > serviceLoadedMiddlewares = new ArrayList <>();
46
46
private final Map <String , InvocationChainFactory > functionFactories = new ConcurrentHashMap <>();
47
47
private final SdkParameterAnalyzer sdkParameterAnalyzer = new SdkParameterAnalyzer ();
48
48
private final WorkerObjectCache <CacheKey > workerObjectCache ;
49
49
private static final boolean JAVA_ENABLE_SDK_TYPES_FLAG =
50
50
Boolean .parseBoolean (System .getenv ("JAVA_ENABLE_SDK_TYPES" ));
51
+ private ClassLoader userContextClassLoader ;
51
52
52
53
private FunctionInstanceInjector newInstanceInjector () {
53
54
return new FunctionInstanceInjector () {
@@ -86,17 +87,16 @@ private void createInvocationChainFactory(FunctionDefinition functionDefinition,
86
87
SdkParameterAnalysisResult sdkParameterAnalysisResult =
87
88
this .sdkParameterAnalyzer .analyze (functionDefinition .getCandidate ().getMethod ());
88
89
89
- ClassLoader classLoader = this .classLoaderProvider .createClassLoader ();
90
- List <Middleware > functionMws = new ArrayList <>(this .baseMiddlewares );
90
+ List <Middleware > functionMws = new ArrayList <>(this .serviceLoadedMiddlewares );
91
91
boolean hasAnySdkTypes = sdkParameterAnalysisResult .hasAnySdkTypes ();
92
92
93
93
if (hasAnySdkTypes ) {
94
- functionMws .add (new SdkTypeMiddleware (classLoader ,
94
+ functionMws .add (new SdkTypeMiddleware (userContextClassLoader ,
95
95
sdkParameterAnalysisResult .getSdkTypesMetaData (),
96
96
this .sdkParameterAnalyzer .getRegistry ()));
97
97
}
98
98
99
- functionMws .add (getFunctionExecutionMiddleWare (classLoader ));
99
+ functionMws .add (getFunctionExecutionMiddleWare (userContextClassLoader ));
100
100
101
101
InvocationChainFactory factory = new InvocationChainFactory (functionMws );
102
102
String functionId = functionDefinition .getDescriptor ().getId ();
@@ -110,15 +110,16 @@ private void initializeOneTimeLogics() {
110
110
if (!oneTimeLogicInitialized ) {
111
111
synchronized (oneTimeLogicInitializationLock ) {
112
112
if (!oneTimeLogicInitialized ) {
113
+ userContextClassLoader = classLoaderProvider .createClassLoader ();
113
114
114
115
if (JAVA_ENABLE_SDK_TYPES_FLAG ) {
115
116
loadGlobalMiddlewares ();
116
117
} else {
117
118
initializeInvocationChainFactory ();
118
119
}
119
120
120
- initializeFunctionInstanceInjector ();
121
121
oneTimeLogicInitialized = true ;
122
+ initializeFunctionInstanceInjector ();
122
123
}
123
124
}
124
125
}
@@ -128,9 +129,9 @@ private void loadGlobalMiddlewares() {
128
129
ClassLoader prevContextClassLoader = Thread .currentThread ().getContextClassLoader ();
129
130
try {
130
131
//ServiceLoader will use thread context classloader to verify loaded class
131
- Thread .currentThread ().setContextClassLoader (classLoaderProvider . createClassLoader () );
132
+ Thread .currentThread ().setContextClassLoader (userContextClassLoader );
132
133
for (Middleware middleware : ServiceLoader .load (Middleware .class )) {
133
- this .baseMiddlewares .add (middleware );
134
+ this .serviceLoadedMiddlewares .add (middleware );
134
135
WorkerLogManager .getSystemLogger ().info ("Loading discovered middleware " + middleware .getClass ().getSimpleName ());
135
136
}
136
137
} finally {
@@ -139,20 +140,20 @@ private void loadGlobalMiddlewares() {
139
140
}
140
141
141
142
private void initializeInvocationChainFactory () {
142
- ArrayList <Middleware > middlewares = new ArrayList <>();
143
- ClassLoader prevContextClassLoader = Thread .currentThread ().getContextClassLoader ();
144
- ClassLoader newContextClassLoader = classLoaderProvider .createClassLoader ();
143
+ ClassLoader prevContextClassLoader = Thread .currentThread ().getContextClassLoader ();
145
144
try {
146
145
//ServiceLoader will use thread context classloader to verify loaded class
147
- Thread .currentThread ().setContextClassLoader (newContextClassLoader );
146
+ Thread .currentThread ().setContextClassLoader (userContextClassLoader );
148
147
for (Middleware middleware : ServiceLoader .load (Middleware .class )) {
149
- middlewares .add (middleware );
148
+ this . serviceLoadedMiddlewares .add (middleware );
150
149
WorkerLogManager .getSystemLogger ().info ("Load middleware " + middleware .getClass ().getSimpleName ());
151
150
}
152
151
} finally {
153
152
Thread .currentThread ().setContextClassLoader (prevContextClassLoader );
154
153
}
155
- middlewares .add (getFunctionExecutionMiddleWare (newContextClassLoader ));
154
+
155
+ ArrayList <Middleware > middlewares = new ArrayList <>(this .serviceLoadedMiddlewares );
156
+ middlewares .add (getFunctionExecutionMiddleWare (userContextClassLoader ));
156
157
this .invocationChainFactory = new InvocationChainFactory (middlewares );
157
158
}
158
159
0 commit comments