11/*
2- * Copyright 2013-2017 the original author or authors.
2+ * Copyright 2013-2018 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717package org .springframework .integration .config ;
1818
1919import java .lang .reflect .Method ;
20- import java .util .LinkedHashMap ;
21- import java .util .Map ;
2220import java .util .Map .Entry ;
2321
24- import org .springframework .beans .BeansException ;
25- import org .springframework .beans .factory .BeanFactoryUtils ;
2622import 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 ;
3123import org .springframework .context .expression .BeanFactoryResolver ;
3224import org .springframework .context .expression .MapAccessor ;
3325import org .springframework .core .convert .ConversionService ;
3628import org .springframework .expression .TypeConverter ;
3729import org .springframework .expression .TypeLocator ;
3830import org .springframework .expression .spel .support .StandardEvaluationContext ;
39- import org .springframework .expression .spel .support .StandardTypeConverter ;
4031import org .springframework .integration .context .IntegrationContextUtils ;
4132import org .springframework .integration .expression .SpelPropertyAccessorRegistrar ;
42- import org .springframework .integration .support .utils .IntegrationUtils ;
43- import org .springframework .util .Assert ;
4433
4534/**
4635 * <p>
7362 *
7463 * @since 3.0
7564 */
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 > {
8467
8568 private volatile TypeLocator typeLocator ;
8669
8770 private BeanResolver beanResolver ;
8871
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-
12072 public void setTypeLocator (TypeLocator typeLocator ) {
12173 this .typeLocator = typeLocator ;
12274 }
12375
76+ @ Override
77+ public boolean isSingleton () {
78+ return false ;
79+ }
12480
12581 @ Override
12682 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 ());
17485 }
175-
176- this .initialized = true ;
86+ initialize (IntegrationContextUtils .INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME );
17787 }
17888
17989 @ Override
@@ -184,15 +94,15 @@ public StandardEvaluationContext getObject() throws Exception {
18494 }
18595
18696 evaluationContext .setBeanResolver (this .beanResolver );
187- evaluationContext .setTypeConverter (this . typeConverter );
97+ evaluationContext .setTypeConverter (getTypeConverter () );
18898
189- for (PropertyAccessor propertyAccessor : this . propertyAccessors .values ()) {
99+ for (PropertyAccessor propertyAccessor : getPropertyAccessors () .values ()) {
190100 evaluationContext .addPropertyAccessor (propertyAccessor );
191101 }
192102
193103 evaluationContext .addPropertyAccessor (new MapAccessor ());
194104
195- for (Entry <String , Method > functionEntry : this . functions .entrySet ()) {
105+ for (Entry <String , Method > functionEntry : getFunctions () .entrySet ()) {
196106 evaluationContext .registerFunction (functionEntry .getKey (), functionEntry .getValue ());
197107 }
198108
@@ -204,9 +114,4 @@ public Class<?> getObjectType() {
204114 return StandardEvaluationContext .class ;
205115 }
206116
207- @ Override
208- public boolean isSingleton () {
209- return false ;
210- }
211-
212117}
0 commit comments