17
17
package org .springframework .integration .expression ;
18
18
19
19
import java .io .File ;
20
+ import java .io .FileNotFoundException ;
21
+ import java .io .IOException ;
20
22
21
23
import org .apache .commons .logging .Log ;
22
24
import org .apache .commons .logging .LogFactory ;
25
27
import org .springframework .context .expression .BeanFactoryResolver ;
26
28
import org .springframework .context .expression .MapAccessor ;
27
29
import org .springframework .core .convert .ConversionService ;
30
+ import org .springframework .core .io .Resource ;
28
31
import org .springframework .expression .EvaluationContext ;
29
32
import org .springframework .expression .Expression ;
30
33
import org .springframework .expression .ExpressionParser ;
39
42
import org .springframework .lang .Nullable ;
40
43
import org .springframework .messaging .Message ;
41
44
import org .springframework .util .Assert ;
45
+ import org .springframework .util .ResourceUtils ;
42
46
43
47
/**
44
48
* Utility class with static methods for helping with evaluation of SpEL expressions.
@@ -53,7 +57,7 @@ public final class ExpressionUtils {
53
57
54
58
private static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser ();
55
59
56
- private static final Log logger = LogFactory .getLog (ExpressionUtils .class );
60
+ private static final Log LOGGER = LogFactory .getLog (ExpressionUtils .class );
57
61
58
62
private ExpressionUtils () {
59
63
super ();
@@ -84,7 +88,7 @@ public static SimpleEvaluationContext createSimpleEvaluationContext() {
84
88
*/
85
89
public static StandardEvaluationContext createStandardEvaluationContext (@ Nullable BeanFactory beanFactory ) {
86
90
if (beanFactory == null ) {
87
- logger .warn ("Creating EvaluationContext with no beanFactory" , new RuntimeException ("No beanFactory" ));
91
+ LOGGER .warn ("Creating EvaluationContext with no beanFactory" , new RuntimeException ("No beanFactory" ));
88
92
}
89
93
return (StandardEvaluationContext ) doCreateContext (beanFactory , false );
90
94
}
@@ -98,7 +102,7 @@ public static StandardEvaluationContext createStandardEvaluationContext(@Nullabl
98
102
*/
99
103
public static SimpleEvaluationContext createSimpleEvaluationContext (@ Nullable BeanFactory beanFactory ) {
100
104
if (beanFactory == null ) {
101
- logger .warn ("Creating EvaluationContext with no beanFactory" , new RuntimeException ("No beanFactory" ));
105
+ LOGGER .warn ("Creating EvaluationContext with no beanFactory" , new RuntimeException ("No beanFactory" ));
102
106
}
103
107
return (SimpleEvaluationContext ) doCreateContext (beanFactory , true );
104
108
}
@@ -161,36 +165,56 @@ private static EvaluationContext createEvaluationContext(@Nullable ConversionSer
161
165
* @param expression the expression.
162
166
* @param evaluationContext the evaluation context.
163
167
* @param message the message (if available).
164
- * @param name the name of the result of the evaluation .
168
+ * @param propertyName the property name the expression is evaluated for .
165
169
* @return the File.
166
170
* @since 5.0
167
171
*/
168
172
public static File expressionToFile (Expression expression , EvaluationContext evaluationContext ,
169
- @ Nullable Message <?> message , String name ) {
170
-
171
- File file ;
172
- Object value = message == null
173
- ? expression .getValue (evaluationContext )
174
- : expression .getValue (evaluationContext , message );
175
- if (value == null ) {
176
- throw new IllegalStateException (String .format ("The provided %s expression (%s) must not evaluate to null." ,
177
- name , expression .getExpressionString ()));
178
- }
179
- else if (value instanceof File ) {
180
- file = (File ) value ;
173
+ @ Nullable Message <?> message , String propertyName ) {
174
+
175
+ Object value =
176
+ message == null
177
+ ? expression .getValue (evaluationContext )
178
+ : expression .getValue (evaluationContext , message );
179
+
180
+ Assert .state (value != null , () ->
181
+ String .format ("The provided %s expression (%s) must not evaluate to null." ,
182
+ propertyName , expression .getExpressionString ()));
183
+
184
+ if (value instanceof File ) {
185
+ return (File ) value ;
181
186
}
182
187
else if (value instanceof String ) {
183
188
String path = (String ) value ;
184
- Assert .hasText (path , String .format ("Unable to resolve %s for the provided Expression '%s'." , name ,
189
+ Assert .hasText (path , String .format ("Unable to resolve %s for the provided Expression '%s'." , propertyName ,
185
190
expression .getExpressionString ()));
186
- file = new File (path );
191
+ try {
192
+ return ResourceUtils .getFile (path );
193
+ }
194
+ catch (FileNotFoundException ex ) {
195
+ throw new IllegalStateException (
196
+ String .format ("Unable to resolve %s for the provided Expression '%s'." ,
197
+ propertyName , expression .getExpressionString ()),
198
+ ex );
199
+ }
200
+ }
201
+ else if (value instanceof Resource ) {
202
+ try {
203
+ return ((Resource ) value ).getFile ();
204
+ }
205
+ catch (IOException ex ) {
206
+ throw new IllegalStateException (
207
+ String .format ("Unable to resolve %s for the provided Expression '%s'." ,
208
+ propertyName , expression .getExpressionString ()),
209
+ ex );
210
+ }
187
211
}
188
212
else {
189
213
throw new IllegalStateException (String .format (
190
- "The provided %s expression (%s) must evaluate to type java.io.File or String, not %s." , name ,
214
+ "The provided %s expression (%s) must evaluate to type java.io.File, String " +
215
+ "or org.springframework.core.io.Resource, not %s." , propertyName ,
191
216
expression .getExpressionString (), value .getClass ().getName ()));
192
217
}
193
- return file ;
194
218
}
195
219
196
220
/**
0 commit comments