25
25
import java .util .Properties ;
26
26
import java .util .concurrent .locks .ReentrantReadWriteLock ;
27
27
28
- import javax .inject .Inject ;
29
-
30
28
import org .apache .commons .configuration2 .Configuration ;
29
+ import org .xwiki .component .phase .Initializable ;
31
30
import org .xwiki .configuration .ConfigurationSource ;
32
- import org .xwiki .properties .ConverterManager ;
33
31
34
32
/**
35
33
* Wrap a Commons Configuration instance into a XWiki {@link ConfigurationSource}. This allows us to reuse the
39
37
* @version $Id$
40
38
* @since 1.6M1
41
39
*/
42
- public class CommonsConfigurationSource implements ConfigurationSource
40
+ public abstract class AbstractCommonsConfigurationSource extends AbstractPropertiesConfigurationSource
41
+ implements Initializable
43
42
{
44
- /**
45
- * Component used for performing type conversions.
46
- */
47
- @ Inject
48
- protected ConverterManager converterManager ;
49
-
50
43
protected final ReentrantReadWriteLock lock = new ReentrantReadWriteLock ();
51
44
52
45
private Configuration configuration ;
@@ -61,27 +54,15 @@ protected void setConfiguration(Configuration configuration)
61
54
this .configuration = configuration ;
62
55
}
63
56
64
- @ Override
65
- @ SuppressWarnings ("unchecked" )
66
- public <T > T getProperty (String key , T defaultValue )
57
+ protected AbstractCommonsConfigurationSource ()
67
58
{
68
- T result ;
69
- if (containsKey (key )) {
70
- if (defaultValue != null ) {
71
- return getProperty (key , (Class <T >) defaultValue .getClass ());
72
- } else {
73
- return getProperty (key );
74
- }
75
- } else {
76
- result = defaultValue ;
77
- }
78
-
79
- return result ;
59
+ // Enable system overwrite
60
+ this .systemOverwriteEnabled = true ;
80
61
}
81
62
82
63
@ Override
83
64
@ SuppressWarnings ("unchecked" )
84
- public <T > T getProperty (String key )
65
+ public <T > T getPropertyInternal (String key )
85
66
{
86
67
this .lock .readLock ().lock ();
87
68
@@ -94,9 +75,9 @@ public <T> T getProperty(String key)
94
75
95
76
@ Override
96
77
@ SuppressWarnings ("unchecked" )
97
- public <T > T getProperty (String key , Class <T > valueClass )
78
+ public <T > T getPropertyInternal (String key , Class <T > valueClass )
98
79
{
99
- T result = null ;
80
+ T result ;
100
81
101
82
try {
102
83
if (String .class == valueClass ) {
@@ -106,10 +87,7 @@ public <T> T getProperty(String key, Class<T> valueClass)
106
87
} else if (Properties .class .isAssignableFrom (valueClass )) {
107
88
result = (T ) getProperties (key );
108
89
} else {
109
- Object value = getProperty (key );
110
- if (value != null ) {
111
- result = this .converterManager .convert (valueClass , value );
112
- }
90
+ result = getConvertedProperty (key , valueClass , null );
113
91
}
114
92
} catch (org .apache .commons .configuration2 .ex .ConversionException
115
93
| org .xwiki .properties .converter .ConversionException e ) {
@@ -154,15 +132,35 @@ private Properties getProperties(String key)
154
132
}
155
133
156
134
@ Override
157
- public List <String > getKeys ()
135
+ public boolean containsKeyInternal (String key )
136
+ {
137
+ this .lock .readLock ().lock ();
138
+
139
+ try {
140
+ return getConfiguration ().containsKey (key );
141
+ } finally {
142
+ this .lock .readLock ().unlock ();
143
+ }
144
+ }
145
+
146
+ @ Override
147
+ public List <String > getKeysInternal ()
148
+ {
149
+ return getKeys ("" );
150
+ }
151
+
152
+ @ Override
153
+ public List <String > getKeysInternal (String prefix )
158
154
{
159
155
this .lock .readLock ().lock ();
160
156
161
157
try {
162
158
List <String > keysList = new ArrayList <>();
163
- Iterator <String > keys = getConfiguration ().getKeys ();
164
- while (keys .hasNext ()) {
165
- keysList .add (keys .next ());
159
+ for (Iterator <String > keys = getConfiguration ().getKeys (); keys .hasNext ();) {
160
+ String key = keys .next ();
161
+ if (key .startsWith (prefix )) {
162
+ keysList .add (key );
163
+ }
166
164
}
167
165
168
166
return keysList ;
@@ -172,24 +170,31 @@ public List<String> getKeys()
172
170
}
173
171
174
172
@ Override
175
- public boolean containsKey ( String key )
173
+ public boolean isEmptyInternal ( )
176
174
{
177
175
this .lock .readLock ().lock ();
178
176
179
177
try {
180
- return getConfiguration ().containsKey ( key );
178
+ return getConfiguration ().isEmpty ( );
181
179
} finally {
182
180
this .lock .readLock ().unlock ();
183
181
}
184
182
}
185
183
186
184
@ Override
187
- public boolean isEmpty ( )
185
+ public boolean isEmptyInternal ( String prefix )
188
186
{
189
187
this .lock .readLock ().lock ();
190
188
191
189
try {
192
- return getConfiguration ().isEmpty ();
190
+ for (Iterator <String > keys = getConfiguration ().getKeys (); keys .hasNext ();) {
191
+ String key = keys .next ();
192
+ if (key .startsWith (prefix )) {
193
+ return false ;
194
+ }
195
+ }
196
+
197
+ return true ;
193
198
} finally {
194
199
this .lock .readLock ().unlock ();
195
200
}
0 commit comments