Skip to content

Commit a66e5e3

Browse files
committed
XCOMMONS-3289: Allow overwritting xwiki.properties and xwiki.cfg properties via environment variable and Java system properties
1 parent c1596c3 commit a66e5e3

File tree

21 files changed

+268
-305
lines changed

21 files changed

+268
-305
lines changed

xwiki-platform-core/xwiki-platform-configuration/xwiki-platform-configuration-default/pom.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
<artifactId>xwiki-commons-configuration-api</artifactId>
4343
<version>${commons.version}</version>
4444
</dependency>
45+
<dependency>
46+
<groupId>org.xwiki.commons</groupId>
47+
<artifactId>xwiki-commons-configuration-default</artifactId>
48+
<version>${commons.version}</version>
49+
</dependency>
4550
<dependency>
4651
<groupId>org.xwiki.commons</groupId>
4752
<artifactId>xwiki-commons-environment-api</artifactId>
@@ -107,7 +112,6 @@
107112
org/xwiki/configuration/internal/AbstractDocumentConfigurationSource.java,
108113
org/xwiki/configuration/internal/AllConfigurationSource.java,
109114
org/xwiki/configuration/internal/CommonsConfigurationSource.java,
110-
org/xwiki/configuration/internal/CompositeConfigurationSource.java,
111115
org/xwiki/configuration/internal/DefaultConfigurationSource.java
112116
</excludes>
113117
</configuration>
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import java.util.Properties;
2626
import java.util.concurrent.locks.ReentrantReadWriteLock;
2727

28-
import javax.inject.Inject;
29-
3028
import org.apache.commons.configuration2.Configuration;
29+
import org.xwiki.component.phase.Initializable;
3130
import org.xwiki.configuration.ConfigurationSource;
32-
import org.xwiki.properties.ConverterManager;
3331

3432
/**
3533
* Wrap a Commons Configuration instance into a XWiki {@link ConfigurationSource}. This allows us to reuse the
@@ -39,14 +37,9 @@
3937
* @version $Id$
4038
* @since 1.6M1
4139
*/
42-
public class CommonsConfigurationSource implements ConfigurationSource
40+
public abstract class AbstractCommonsConfigurationSource extends AbstractPropertiesConfigurationSource
41+
implements Initializable
4342
{
44-
/**
45-
* Component used for performing type conversions.
46-
*/
47-
@Inject
48-
protected ConverterManager converterManager;
49-
5043
protected final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
5144

5245
private Configuration configuration;
@@ -61,27 +54,15 @@ protected void setConfiguration(Configuration configuration)
6154
this.configuration = configuration;
6255
}
6356

64-
@Override
65-
@SuppressWarnings("unchecked")
66-
public <T> T getProperty(String key, T defaultValue)
57+
protected AbstractCommonsConfigurationSource()
6758
{
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;
8061
}
8162

8263
@Override
8364
@SuppressWarnings("unchecked")
84-
public <T> T getProperty(String key)
65+
public <T> T getPropertyInternal(String key)
8566
{
8667
this.lock.readLock().lock();
8768

@@ -94,9 +75,9 @@ public <T> T getProperty(String key)
9475

9576
@Override
9677
@SuppressWarnings("unchecked")
97-
public <T> T getProperty(String key, Class<T> valueClass)
78+
public <T> T getPropertyInternal(String key, Class<T> valueClass)
9879
{
99-
T result = null;
80+
T result;
10081

10182
try {
10283
if (String.class == valueClass) {
@@ -106,10 +87,7 @@ public <T> T getProperty(String key, Class<T> valueClass)
10687
} else if (Properties.class.isAssignableFrom(valueClass)) {
10788
result = (T) getProperties(key);
10889
} else {
109-
Object value = getProperty(key);
110-
if (value != null) {
111-
result = this.converterManager.convert(valueClass, value);
112-
}
90+
result = getConvertedProperty(key, valueClass, null);
11391
}
11492
} catch (org.apache.commons.configuration2.ex.ConversionException
11593
| org.xwiki.properties.converter.ConversionException e) {
@@ -154,15 +132,35 @@ private Properties getProperties(String key)
154132
}
155133

156134
@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)
158154
{
159155
this.lock.readLock().lock();
160156

161157
try {
162158
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+
}
166164
}
167165

168166
return keysList;
@@ -172,24 +170,31 @@ public List<String> getKeys()
172170
}
173171

174172
@Override
175-
public boolean containsKey(String key)
173+
public boolean isEmptyInternal()
176174
{
177175
this.lock.readLock().lock();
178176

179177
try {
180-
return getConfiguration().containsKey(key);
178+
return getConfiguration().isEmpty();
181179
} finally {
182180
this.lock.readLock().unlock();
183181
}
184182
}
185183

186184
@Override
187-
public boolean isEmpty()
185+
public boolean isEmptyInternal(String prefix)
188186
{
189187
this.lock.readLock().lock();
190188

191189
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;
193198
} finally {
194199
this.lock.readLock().unlock();
195200
}

xwiki-platform-core/xwiki-platform-configuration/xwiki-platform-configuration-default/src/main/java/org/xwiki/configuration/internal/AbstractCompositeConfigurationSource.java

-135
This file was deleted.

0 commit comments

Comments
 (0)