1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 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.
16
16
17
17
package org .springframework .core .env ;
18
18
19
- import static java .lang .String .format ;
20
-
21
19
import org .springframework .core .convert .ConversionException ;
22
20
import org .springframework .util .ClassUtils ;
23
21
@@ -35,6 +33,7 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
35
33
36
34
private final PropertySources propertySources ;
37
35
36
+
38
37
/**
39
38
* Create a new resolver against the given property sources.
40
39
* @param propertySources the set of {@link PropertySource} objects to use
@@ -43,106 +42,110 @@ public PropertySourcesPropertyResolver(PropertySources propertySources) {
43
42
this .propertySources = propertySources ;
44
43
}
45
44
45
+
46
+ @ Override
46
47
public boolean containsProperty (String key ) {
47
- for (PropertySource <?> propertySource : this .propertySources ) {
48
- if (propertySource .containsProperty (key )) {
49
- return true ;
48
+ if (this .propertySources != null ) {
49
+ for (PropertySource <?> propertySource : this .propertySources ) {
50
+ if (propertySource .containsProperty (key )) {
51
+ return true ;
52
+ }
50
53
}
51
54
}
52
55
return false ;
53
56
}
54
57
58
+ @ Override
55
59
public String getProperty (String key ) {
56
- if (logger .isTraceEnabled ()) {
57
- logger .trace (format ("getProperty(\" %s\" ) (implicit targetType [String])" , key ));
58
- }
59
- return this .getProperty (key , String .class );
60
+ return getProperty (key , String .class );
60
61
}
61
62
63
+ @ Override
62
64
public <T > T getProperty (String key , Class <T > targetValueType ) {
63
65
boolean debugEnabled = logger .isDebugEnabled ();
64
66
if (logger .isTraceEnabled ()) {
65
- logger .trace (format ("getProperty(\" %s\" , %s)" , key , targetValueType .getSimpleName ()));
67
+ logger .trace (String . format ("getProperty(\" %s\" , %s)" , key , targetValueType .getSimpleName ()));
66
68
}
67
-
68
- for (PropertySource <?> propertySource : this .propertySources ) {
69
- if (debugEnabled ) {
70
- logger .debug (format ("Searching for key '%s' in [%s]" , key , propertySource .getName ()));
71
- }
72
- Object value ;
73
- if ((value = propertySource .getProperty (key )) != null ) {
74
- Class <?> valueType = value .getClass ();
75
- if (String .class .equals (valueType )) {
76
- value = this .resolveNestedPlaceholders ((String ) value );
77
- }
69
+ if (this .propertySources != null ) {
70
+ for (PropertySource <?> propertySource : this .propertySources ) {
78
71
if (debugEnabled ) {
79
- logger .debug (
80
- format ("Found key '%s' in [%s] with type [%s] and value '%s'" ,
81
- key , propertySource .getName (), valueType .getSimpleName (), value ));
72
+ logger .debug (String .format ("Searching for key '%s' in [%s]" , key , propertySource .getName ()));
82
73
}
83
- if (!this .conversionService .canConvert (valueType , targetValueType )) {
84
- throw new IllegalArgumentException (
85
- format ("Cannot convert value [%s] from source type [%s] to target type [%s]" ,
86
- value , valueType .getSimpleName (), targetValueType .getSimpleName ()));
74
+ Object value ;
75
+ if ((value = propertySource .getProperty (key )) != null ) {
76
+ Class <?> valueType = value .getClass ();
77
+ if (String .class .equals (valueType )) {
78
+ value = this .resolveNestedPlaceholders ((String ) value );
79
+ }
80
+ if (debugEnabled ) {
81
+ logger .debug (String .format ("Found key '%s' in [%s] with type [%s] and value '%s'" ,
82
+ key , propertySource .getName (), valueType .getSimpleName (), value ));
83
+ }
84
+ if (!this .conversionService .canConvert (valueType , targetValueType )) {
85
+ throw new IllegalArgumentException (String .format (
86
+ "Cannot convert value [%s] from source type [%s] to target type [%s]" ,
87
+ value , valueType .getSimpleName (), targetValueType .getSimpleName ()));
88
+ }
89
+ return conversionService .convert (value , targetValueType );
87
90
}
88
- return conversionService .convert (value , targetValueType );
89
91
}
90
92
}
91
-
92
93
if (debugEnabled ) {
93
- logger .debug (format ("Could not find key '%s' in any property source. Returning [null]" , key ));
94
+ logger .debug (String . format ("Could not find key '%s' in any property source. Returning [null]" , key ));
94
95
}
95
96
return null ;
96
97
}
97
98
99
+ @ Override
98
100
public <T > Class <T > getPropertyAsClass (String key , Class <T > targetValueType ) {
99
101
boolean debugEnabled = logger .isDebugEnabled ();
100
102
if (logger .isTraceEnabled ()) {
101
- logger .trace (format ("getPropertyAsClass(\" %s\" , %s)" , key , targetValueType .getSimpleName ()));
103
+ logger .trace (String . format ("getPropertyAsClass(\" %s\" , %s)" , key , targetValueType .getSimpleName ()));
102
104
}
103
-
104
- for (PropertySource <?> propertySource : this .propertySources ) {
105
- if (debugEnabled ) {
106
- logger .debug (format ("Searching for key '%s' in [%s]" , key , propertySource .getName ()));
107
- }
108
- Object value ;
109
- if ((value = propertySource .getProperty (key )) != null ) {
105
+ if (this .propertySources != null ) {
106
+ for (PropertySource <?> propertySource : this .propertySources ) {
110
107
if (debugEnabled ) {
111
- logger .debug (
112
- format ("Found key '%s' in [%s] with value '%s'" , key , propertySource .getName (), value ));
108
+ logger .debug (String .format ("Searching for key '%s' in [%s]" , key , propertySource .getName ()));
113
109
}
114
-
115
- Class <?> clazz ;
116
- if (value instanceof String ) {
117
- try {
118
- clazz = ClassUtils .forName ((String )value , null );
119
- } catch (Exception ex ) {
120
- throw new ClassConversionException ((String )value , targetValueType , ex );
110
+ Object value = propertySource .getProperty (key );
111
+ if (value != null ) {
112
+ if (debugEnabled ) {
113
+ logger .debug (String .format ("Found key '%s' in [%s] with value '%s'" , key , propertySource .getName (), value ));
121
114
}
115
+ Class <?> clazz ;
116
+ if (value instanceof String ) {
117
+ try {
118
+ clazz = ClassUtils .forName ((String )value , null );
119
+ }
120
+ catch (Exception ex ) {
121
+ throw new ClassConversionException ((String )value , targetValueType , ex );
122
+ }
123
+ }
124
+ else if (value instanceof Class ) {
125
+ clazz = (Class <?>)value ;
126
+ }
127
+ else {
128
+ clazz = value .getClass ();
129
+ }
130
+ if (!targetValueType .isAssignableFrom (clazz )) {
131
+ throw new ClassConversionException (clazz , targetValueType );
132
+ }
133
+ @ SuppressWarnings ("unchecked" )
134
+ Class <T > targetClass = (Class <T >) clazz ;
135
+ return targetClass ;
122
136
}
123
- else if (value instanceof Class ) {
124
- clazz = (Class <?>)value ;
125
- } else {
126
- clazz = value .getClass ();
127
- }
128
-
129
- if (!targetValueType .isAssignableFrom (clazz )) {
130
- throw new ClassConversionException (clazz , targetValueType );
131
- }
132
- @ SuppressWarnings ("unchecked" )
133
- Class <T > targetClass = (Class <T >)clazz ;
134
- return targetClass ;
135
137
}
136
138
}
137
-
138
139
if (debugEnabled ) {
139
- logger .debug (format ("Could not find key '%s' in any property source. Returning [null]" , key ));
140
+ logger .debug (String . format ("Could not find key '%s' in any property source. Returning [null]" , key ));
140
141
}
141
142
return null ;
142
143
}
143
144
145
+
144
146
@ SuppressWarnings ("serial" )
145
- static class ClassConversionException extends ConversionException {
147
+ private static class ClassConversionException extends ConversionException {
148
+
146
149
public ClassConversionException (Class <?> actual , Class <?> expected ) {
147
150
super (String .format ("Actual type %s is not assignable to expected type %s" , actual .getName (), expected .getName ()));
148
151
}
@@ -151,4 +154,5 @@ public ClassConversionException(String actual, Class<?> expected, Exception ex)
151
154
super (String .format ("Could not find/load class %s during attempt to convert to %s" , actual , expected .getName ()), ex );
152
155
}
153
156
}
157
+
154
158
}
0 commit comments