31
31
32
32
import org .springframework .beans .BeanUtils ;
33
33
import org .springframework .beans .BeansException ;
34
+ import org .springframework .beans .factory .BeanClassLoaderAware ;
34
35
import org .springframework .beans .factory .FactoryBean ;
35
36
import org .springframework .beans .factory .InitializingBean ;
36
37
import org .springframework .context .ApplicationContext ;
57
58
* @see AbstractHttpServiceRegistrar
58
59
*/
59
60
public final class HttpServiceProxyRegistryFactoryBean
60
- implements ApplicationContextAware , InitializingBean , FactoryBean <HttpServiceProxyRegistry > {
61
+ implements ApplicationContextAware , BeanClassLoaderAware , InitializingBean ,
62
+ FactoryBean <HttpServiceProxyRegistry > {
61
63
62
64
private static final Map <HttpServiceGroup .ClientType , HttpServiceGroupAdapter <?>> groupAdapters =
63
65
GroupAdapterInitializer .initGroupAdapters ();
64
66
65
67
66
- private final Set < ProxyHttpServiceGroup > groupSet ;
68
+ private final GroupsMetadata groupsMetadata ;
67
69
68
70
private @ Nullable ApplicationContext applicationContext ;
69
71
72
+ private @ Nullable ClassLoader beanClassLoader ;
73
+
70
74
private @ Nullable HttpServiceProxyRegistry proxyRegistry ;
71
75
72
76
73
77
HttpServiceProxyRegistryFactoryBean (GroupsMetadata groupsMetadata ) {
74
- this .groupSet = groupsMetadata .groups ().stream ()
75
- .map (group -> {
76
- HttpServiceGroupAdapter <?> adapter = groupAdapters .get (group .clientType ());
77
- Assert .state (adapter != null , "No HttpServiceGroupAdapter for type " + group .clientType ());
78
- return new ProxyHttpServiceGroup (group , adapter );
79
- })
80
- .collect (Collectors .toSet ());
78
+ this .groupsMetadata = groupsMetadata ;
81
79
}
82
80
83
81
@@ -86,6 +84,11 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
86
84
this .applicationContext = applicationContext ;
87
85
}
88
86
87
+ @ Override
88
+ public void setBeanClassLoader (ClassLoader beanClassLoader ) {
89
+ this .beanClassLoader = beanClassLoader ;
90
+ }
91
+
89
92
@ Override
90
93
public Class <?> getObjectType () {
91
94
return HttpServiceProxyRegistry .class ;
@@ -95,18 +98,25 @@ public Class<?> getObjectType() {
95
98
@ Override
96
99
public void afterPropertiesSet () {
97
100
Assert .notNull (this .applicationContext , "ApplicationContext not initialized" );
101
+ Assert .notNull (this .beanClassLoader , "BeanClassLoader not initialized" );
102
+
103
+ // Create the groups from the metadata
104
+ Set <ProxyHttpServiceGroup > groups = this .groupsMetadata .groups (this .beanClassLoader )
105
+ .stream ()
106
+ .map (ProxyHttpServiceGroup ::new )
107
+ .collect (Collectors .toSet ());
98
108
99
109
// Apply group configurers
100
110
groupAdapters .forEach ((clientType , groupAdapter ) ->
101
111
this .applicationContext .getBeanProvider (groupAdapter .getConfigurerType ())
102
112
.orderedStream ()
103
- .forEach (configurer -> configurer .configureGroups (new DefaultGroups <>(clientType ))));
113
+ .forEach (configurer -> configurer .configureGroups (new DefaultGroups <>(groups , clientType ))));
104
114
105
115
// Create proxies
106
- Map <String , Map <Class <?>, Object >> groupProxyMap = this . groupSet .stream ()
116
+ Map <String , Map <Class <?>, Object >> proxies = groups .stream ()
107
117
.collect (Collectors .toMap (ProxyHttpServiceGroup ::name , ProxyHttpServiceGroup ::createProxies ));
108
118
109
- this .proxyRegistry = new DefaultHttpServiceProxyRegistry (groupProxyMap );
119
+ this .proxyRegistry = new DefaultHttpServiceProxyRegistry (proxies );
110
120
}
111
121
112
122
@@ -159,12 +169,17 @@ private static final class ProxyHttpServiceGroup implements HttpServiceGroup {
159
169
160
170
private BiConsumer <HttpServiceGroup , HttpServiceProxyFactory .Builder > proxyFactoryConfigurer = (group , builder ) -> {};
161
171
172
+ ProxyHttpServiceGroup (HttpServiceGroup group ) {
173
+ this (group , getHttpServiceGroupAdapter (group .clientType ()));
174
+ }
175
+
162
176
ProxyHttpServiceGroup (HttpServiceGroup group , HttpServiceGroupAdapter <?> groupAdapter ) {
163
177
this .declaredGroup = group ;
164
178
this .groupAdapter = groupAdapter ;
165
179
this .clientBuilder = groupAdapter .createClientBuilder ();
166
180
}
167
181
182
+
168
183
@ Override
169
184
public String name () {
170
185
return this .declaredGroup .name ();
@@ -208,17 +223,26 @@ private <CB> HttpExchangeAdapter initExchangeAdapter() {
208
223
public String toString () {
209
224
return getClass ().getSimpleName () + "[id=" + name () + "]" ;
210
225
}
226
+
227
+ private static HttpServiceGroupAdapter <?> getHttpServiceGroupAdapter (HttpServiceGroup .ClientType clientType ) {
228
+ HttpServiceGroupAdapter <?> adapter = groupAdapters .get (clientType );
229
+ Assert .state (adapter != null , "No HttpServiceGroupAdapter for type " + clientType );
230
+ return adapter ;
231
+ }
211
232
}
212
233
213
234
214
235
/**
215
236
* Default implementation of Groups that helps to configure the set of declared groups.
216
237
*/
217
- private final class DefaultGroups <CB > implements HttpServiceGroupConfigurer .Groups <CB > {
238
+ private static final class DefaultGroups <CB > implements HttpServiceGroupConfigurer .Groups <CB > {
239
+
240
+ private final Set <ProxyHttpServiceGroup > groups ;
218
241
219
242
private Predicate <HttpServiceGroup > filter ;
220
243
221
- DefaultGroups (HttpServiceGroup .ClientType clientType ) {
244
+ DefaultGroups (Set <ProxyHttpServiceGroup > groups , HttpServiceGroup .ClientType clientType ) {
245
+ this .groups = groups ;
222
246
this .filter = group -> group .clientType ().equals (clientType );
223
247
}
224
248
@@ -255,7 +279,7 @@ public void configure(
255
279
BiConsumer <HttpServiceGroup , CB > clientConfigurer ,
256
280
BiConsumer <HttpServiceGroup , HttpServiceProxyFactory .Builder > proxyFactoryConfigurer ) {
257
281
258
- groupSet .stream ().filter (this .filter ).forEach (group ->
282
+ this . groups .stream ().filter (this .filter ).forEach (group ->
259
283
group .apply (clientConfigurer , proxyFactoryConfigurer ));
260
284
}
261
285
}
0 commit comments