1
1
/*
2
- * Copyright 2013-2017 the original author or authors.
2
+ * Copyright 2013-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .integration .config ;
18
18
19
19
import java .lang .reflect .Method ;
20
- import java .util .LinkedHashMap ;
21
- import java .util .Map ;
22
20
import java .util .Map .Entry ;
23
21
24
- import org .springframework .beans .BeansException ;
25
- import org .springframework .beans .factory .BeanFactoryUtils ;
26
22
import org .springframework .beans .factory .FactoryBean ;
27
- import org .springframework .beans .factory .InitializingBean ;
28
- import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
29
- import org .springframework .context .ApplicationContext ;
30
- import org .springframework .context .ApplicationContextAware ;
31
23
import org .springframework .context .expression .BeanFactoryResolver ;
32
24
import org .springframework .context .expression .MapAccessor ;
33
25
import org .springframework .core .convert .ConversionService ;
36
28
import org .springframework .expression .TypeConverter ;
37
29
import org .springframework .expression .TypeLocator ;
38
30
import org .springframework .expression .spel .support .StandardEvaluationContext ;
39
- import org .springframework .expression .spel .support .StandardTypeConverter ;
40
31
import org .springframework .integration .context .IntegrationContextUtils ;
41
32
import org .springframework .integration .expression .SpelPropertyAccessorRegistrar ;
42
- import org .springframework .integration .support .utils .IntegrationUtils ;
43
- import org .springframework .util .Assert ;
44
33
45
34
/**
46
35
* <p>
73
62
*
74
63
* @since 3.0
75
64
*/
76
- public class IntegrationEvaluationContextFactoryBean implements FactoryBean <StandardEvaluationContext >,
77
- ApplicationContextAware , InitializingBean {
78
-
79
- private volatile Map <String , PropertyAccessor > propertyAccessors = new LinkedHashMap <String , PropertyAccessor >();
80
-
81
- private volatile Map <String , Method > functions = new LinkedHashMap <String , Method >();
82
-
83
- private TypeConverter typeConverter = new StandardTypeConverter ();
65
+ public class IntegrationEvaluationContextFactoryBean extends AbstractEvaluationContextFactoryBean
66
+ implements FactoryBean <StandardEvaluationContext > {
84
67
85
68
private volatile TypeLocator typeLocator ;
86
69
87
70
private BeanResolver beanResolver ;
88
71
89
- private ApplicationContext applicationContext ;
90
-
91
- private volatile boolean initialized ;
92
-
93
- @ Override
94
- public void setApplicationContext (ApplicationContext applicationContext ) throws BeansException {
95
- this .applicationContext = applicationContext ;
96
- }
97
-
98
- public void setPropertyAccessors (Map <String , PropertyAccessor > accessors ) {
99
- Assert .isTrue (!this .initialized , "'propertyAccessors' can't be changed after initialization." );
100
- Assert .notNull (accessors , "'accessors' must not be null." );
101
- Assert .noNullElements (accessors .values ().toArray (), "'accessors' cannot have null values." );
102
- this .propertyAccessors = new LinkedHashMap <String , PropertyAccessor >(accessors );
103
- }
104
-
105
- public Map <String , PropertyAccessor > getPropertyAccessors () {
106
- return this .propertyAccessors ;
107
- }
108
-
109
- public void setFunctions (Map <String , Method > functionsArg ) {
110
- Assert .isTrue (!this .initialized , "'functions' can't be changed after initialization." );
111
- Assert .notNull (functionsArg , "'functions' must not be null." );
112
- Assert .noNullElements (functionsArg .values ().toArray (), "'functions' cannot have null values." );
113
- this .functions = new LinkedHashMap <String , Method >(functionsArg );
114
- }
115
-
116
- public Map <String , Method > getFunctions () {
117
- return this .functions ;
118
- }
119
-
120
72
public void setTypeLocator (TypeLocator typeLocator ) {
121
73
this .typeLocator = typeLocator ;
122
74
}
123
75
76
+ @ Override
77
+ public boolean isSingleton () {
78
+ return false ;
79
+ }
124
80
125
81
@ Override
126
82
public void afterPropertiesSet () throws Exception {
127
- if (this .applicationContext != null ) {
128
- this .beanResolver = new BeanFactoryResolver (this .applicationContext );
129
- ConversionService conversionService = IntegrationUtils .getConversionService (this .applicationContext );
130
- if (conversionService != null ) {
131
- this .typeConverter = new StandardTypeConverter (conversionService );
132
- }
133
-
134
- Map <String , SpelFunctionFactoryBean > functionFactoryBeanMap =
135
- BeanFactoryUtils .beansOfTypeIncludingAncestors (this .applicationContext , SpelFunctionFactoryBean .class );
136
- for (SpelFunctionFactoryBean spelFunctionFactoryBean : functionFactoryBeanMap .values ()) {
137
- if (!this .functions .containsKey (spelFunctionFactoryBean .getFunctionName ())) {
138
- this .functions .put (spelFunctionFactoryBean .getFunctionName (), spelFunctionFactoryBean .getObject ());
139
- }
140
- }
141
-
142
- try {
143
- SpelPropertyAccessorRegistrar propertyAccessorRegistrar =
144
- this .applicationContext .getBean (SpelPropertyAccessorRegistrar .class );
145
- for (Entry <String , PropertyAccessor > entry : propertyAccessorRegistrar .getPropertyAccessors ().entrySet ()) {
146
- if (!this .propertyAccessors .containsKey (entry .getKey ())) {
147
- this .propertyAccessors .put (entry .getKey (), entry .getValue ());
148
- }
149
- }
150
- }
151
- catch (NoSuchBeanDefinitionException e ) {
152
- // There is no 'SpelPropertyAccessorRegistrar' bean in the application context.
153
- }
154
-
155
- ApplicationContext parent = this .applicationContext .getParent ();
156
-
157
- if (parent != null && parent .containsBean (IntegrationContextUtils .INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME )) {
158
- IntegrationEvaluationContextFactoryBean parentFactoryBean =
159
- parent .getBean ("&" + IntegrationContextUtils .INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME ,
160
- IntegrationEvaluationContextFactoryBean .class );
161
-
162
- for (Entry <String , PropertyAccessor > entry : parentFactoryBean .getPropertyAccessors ().entrySet ()) {
163
- if (!this .propertyAccessors .containsKey (entry .getKey ())) {
164
- this .propertyAccessors .put (entry .getKey (), entry .getValue ());
165
- }
166
- }
167
-
168
- for (Entry <String , Method > entry : parentFactoryBean .getFunctions ().entrySet ()) {
169
- if (!this .functions .containsKey (entry .getKey ())) {
170
- this .functions .put (entry .getKey (), entry .getValue ());
171
- }
172
- }
173
- }
83
+ if (getApplicationContext () != null ) {
84
+ this .beanResolver = new BeanFactoryResolver (getApplicationContext ());
174
85
}
175
-
176
- this .initialized = true ;
86
+ initialize (IntegrationContextUtils .INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME );
177
87
}
178
88
179
89
@ Override
@@ -184,15 +94,15 @@ public StandardEvaluationContext getObject() throws Exception {
184
94
}
185
95
186
96
evaluationContext .setBeanResolver (this .beanResolver );
187
- evaluationContext .setTypeConverter (this . typeConverter );
97
+ evaluationContext .setTypeConverter (getTypeConverter () );
188
98
189
- for (PropertyAccessor propertyAccessor : this . propertyAccessors .values ()) {
99
+ for (PropertyAccessor propertyAccessor : getPropertyAccessors () .values ()) {
190
100
evaluationContext .addPropertyAccessor (propertyAccessor );
191
101
}
192
102
193
103
evaluationContext .addPropertyAccessor (new MapAccessor ());
194
104
195
- for (Entry <String , Method > functionEntry : this . functions .entrySet ()) {
105
+ for (Entry <String , Method > functionEntry : getFunctions () .entrySet ()) {
196
106
evaluationContext .registerFunction (functionEntry .getKey (), functionEntry .getValue ());
197
107
}
198
108
@@ -204,9 +114,4 @@ public Class<?> getObjectType() {
204
114
return StandardEvaluationContext .class ;
205
115
}
206
116
207
- @ Override
208
- public boolean isSingleton () {
209
- return false ;
210
- }
211
-
212
117
}
0 commit comments