Skip to content

XCOMMONS-3289: Allow overwritting xwiki.properties and xwiki.cfg properties via environment variable and Java system properties #4069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<artifactId>xwiki-commons-configuration-api</artifactId>
<version>${commons.version}</version>
</dependency>
<dependency>
<groupId>org.xwiki.commons</groupId>
<artifactId>xwiki-commons-configuration-default</artifactId>
<version>${commons.version}</version>
</dependency>
<dependency>
<groupId>org.xwiki.commons</groupId>
<artifactId>xwiki-commons-environment-api</artifactId>
Expand Down Expand Up @@ -107,7 +112,6 @@
org/xwiki/configuration/internal/AbstractDocumentConfigurationSource.java,
org/xwiki/configuration/internal/AllConfigurationSource.java,
org/xwiki/configuration/internal/CommonsConfigurationSource.java,
org/xwiki/configuration/internal/CompositeConfigurationSource.java,
org/xwiki/configuration/internal/DefaultConfigurationSource.java
</excludes>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
import java.util.Properties;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import javax.inject.Inject;

import org.apache.commons.configuration2.Configuration;
import org.xwiki.component.phase.Initializable;
import org.xwiki.configuration.ConfigurationSource;
import org.xwiki.properties.ConverterManager;

/**
* Wrap a Commons Configuration instance into a XWiki {@link ConfigurationSource}. This allows us to reuse the
Expand All @@ -39,14 +37,9 @@
* @version $Id$
* @since 1.6M1
*/
public class CommonsConfigurationSource implements ConfigurationSource
public abstract class AbstractCommonsConfigurationSource extends AbstractPropertiesConfigurationSource
implements Initializable
{
/**
* Component used for performing type conversions.
*/
@Inject
protected ConverterManager converterManager;

protected final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

private Configuration configuration;
Expand All @@ -61,27 +54,15 @@ protected void setConfiguration(Configuration configuration)
this.configuration = configuration;
}

@Override
@SuppressWarnings("unchecked")
public <T> T getProperty(String key, T defaultValue)
protected AbstractCommonsConfigurationSource()
{
T result;
if (containsKey(key)) {
if (defaultValue != null) {
return getProperty(key, (Class<T>) defaultValue.getClass());
} else {
return getProperty(key);
}
} else {
result = defaultValue;
}

return result;
// Enable system overwrite
this.systemOverwriteEnabled = true;
}

@Override
@SuppressWarnings("unchecked")
public <T> T getProperty(String key)
public <T> T getPropertyInternal(String key)
{
this.lock.readLock().lock();

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

@Override
@SuppressWarnings("unchecked")
public <T> T getProperty(String key, Class<T> valueClass)
public <T> T getPropertyInternal(String key, Class<T> valueClass)
{
T result = null;
T result;

try {
if (String.class == valueClass) {
Expand All @@ -106,10 +87,7 @@ public <T> T getProperty(String key, Class<T> valueClass)
} else if (Properties.class.isAssignableFrom(valueClass)) {
result = (T) getProperties(key);
} else {
Object value = getProperty(key);
if (value != null) {
result = this.converterManager.convert(valueClass, value);
}
result = getConvertedProperty(key, valueClass, null);
}
} catch (org.apache.commons.configuration2.ex.ConversionException
| org.xwiki.properties.converter.ConversionException e) {
Expand Down Expand Up @@ -154,15 +132,35 @@ private Properties getProperties(String key)
}

@Override
public List<String> getKeys()
public boolean containsKeyInternal(String key)
{
this.lock.readLock().lock();

try {
return getConfiguration().containsKey(key);
} finally {
this.lock.readLock().unlock();
}
}

@Override
public List<String> getKeysInternal()
{
return getKeys("");
}

@Override
public List<String> getKeysInternal(String prefix)
{
this.lock.readLock().lock();

try {
List<String> keysList = new ArrayList<>();
Iterator<String> keys = getConfiguration().getKeys();
while (keys.hasNext()) {
keysList.add(keys.next());
for (Iterator<String> keys = getConfiguration().getKeys(); keys.hasNext();) {
String key = keys.next();
if (key.startsWith(prefix)) {
keysList.add(key);
}
}

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

@Override
public boolean containsKey(String key)
public boolean isEmptyInternal()
{
this.lock.readLock().lock();

try {
return getConfiguration().containsKey(key);
return getConfiguration().isEmpty();
} finally {
this.lock.readLock().unlock();
}
}

@Override
public boolean isEmpty()
public boolean isEmptyInternal(String prefix)
{
this.lock.readLock().lock();

try {
return getConfiguration().isEmpty();
for (Iterator<String> keys = getConfiguration().getKeys(); keys.hasNext();) {
String key = keys.next();
if (key.startsWith(prefix)) {
return false;
}
}

return true;
} finally {
this.lock.readLock().unlock();
}
Expand Down

This file was deleted.

Loading