Adapting to new configuration 2 api
diff --git a/spring-registry/spring-registry-api/src/main/java/org/apache/archiva/components/registry/Registry.java b/spring-registry/spring-registry-api/src/main/java/org/apache/archiva/components/registry/Registry.java
index bf5c4ca..732c33e 100644
--- a/spring-registry/spring-registry-api/src/main/java/org/apache/archiva/components/registry/Registry.java
+++ b/spring-registry/spring-registry-api/src/main/java/org/apache/archiva/components/registry/Registry.java
@@ -19,7 +19,7 @@
* under the License.
*/
-import java.io.File;
+import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
@@ -141,7 +141,7 @@
* @param file the location to load the configuration from
* @throws RegistryException if a problem occurred reading the resource to add to the registry
*/
- void addConfigurationFromFile( File file )
+ void addConfigurationFromFile( Path file )
throws RegistryException;
/**
@@ -151,7 +151,7 @@
* @param prefix the location to add the configuration at in the registry
* @throws RegistryException if a problem occurred reading the resource to add to the registry
*/
- void addConfigurationFromFile( File file, String prefix )
+ void addConfigurationFromFile( Path file, String prefix )
throws RegistryException;
/**
diff --git a/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/components/registry/commons/CommonsConfigurationRegistry.java b/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/components/registry/commons/CommonsConfigurationRegistry.java
index e90b715..8018073 100644
--- a/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/components/registry/commons/CommonsConfigurationRegistry.java
+++ b/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/components/registry/commons/CommonsConfigurationRegistry.java
@@ -24,26 +24,32 @@
import org.apache.archiva.components.registry.RegistryListener;
import org.apache.commons.configuration2.CombinedConfiguration;
import org.apache.commons.configuration2.Configuration;
-import org.apache.commons.configuration2.FileBasedConfiguration;
-import org.apache.commons.configuration2.ImmutableConfiguration;
-import org.apache.commons.configuration2.PropertiesConfiguration;
+import org.apache.commons.configuration2.SystemConfiguration;
import org.apache.commons.configuration2.XMLConfiguration;
+import org.apache.commons.configuration2.builder.BasicBuilderParameters;
import org.apache.commons.configuration2.builder.ConfigurationBuilder;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
+import org.apache.commons.configuration2.builder.combined.CombinedConfigurationBuilder;
+import org.apache.commons.configuration2.builder.fluent.Configurations;
+import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.event.Event;
import org.apache.commons.configuration2.event.EventSource;
-import org.apache.commons.configuration2.event.EventType;
import org.apache.commons.configuration2.ex.ConfigurationException;
+import org.apache.commons.configuration2.tree.DefaultExpressionEngine;
+import org.apache.commons.configuration2.tree.DefaultExpressionEngineSymbols;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.text.lookup.StringLookupFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
+import org.xml.sax.SAXException;
import javax.annotation.PostConstruct;
-import java.io.File;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.IOException;
import java.io.StringReader;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -52,6 +58,7 @@
import java.util.Properties;
import java.util.Set;
+
/**
* Implementation of the registry component using
* <a href="http://commons.apache.org/commons/configuration">Commons Configuration</a>. The use of Commons Configuration
@@ -89,14 +96,22 @@
{
// default constructor
logger.debug( "empty constructor" );
- this.configuration = new CombinedConfiguration( );
+ this.configurationBuilder = new CombinedConfigurationBuilder( );
+ try
+ {
+ this.configuration = configurationBuilder.getConfiguration();
+ }
+ catch ( ConfigurationException e )
+ {
+ logger.error( "Could not initialize configuration: {}", e.getMessage( ) );
+ }
}
public CommonsConfigurationRegistry( ConfigurationBuilder<? extends Configuration> configurationBuilder )
{
if ( configurationBuilder == null )
{
- throw new NullPointerException( "configuration can not be null" );
+ throw new NullPointerException( "configurationbuilder can not be null" );
}
this.configurationBuilder = configurationBuilder;
@@ -110,6 +125,21 @@
}
}
+ public CommonsConfigurationRegistry( ConfigurationBuilder<? extends Configuration> configurationBuilder,
+ Configuration configuration) {
+ if ( configurationBuilder == null )
+ {
+ throw new NullPointerException( "configurationbuilder can not be null" );
+ }
+ if ( configuration == null )
+ {
+ throw new NullPointerException( "configuration can not be null" );
+ }
+ this.configurationBuilder = configurationBuilder;
+ this.configuration = configuration;
+
+ }
+
public String dump( )
{
StringBuilder buffer = new StringBuilder( );
@@ -130,7 +160,7 @@
public Registry getSubset( String key )
{
- return new CommonsConfigurationRegistry( configuration.subset( key ) );
+ return new CommonsConfigurationRegistry( configurationBuilder, configuration.subset( key ) );
}
public List getList( String key )
@@ -343,7 +373,8 @@
try
{
logger.debug( "Loading properties configuration from classloader resource: {}", resource );
- configuration.addConfiguration( new PropertiesConfiguration( resource ), null, prefix );
+ Configurations configurations = new Configurations( );
+ configuration.addConfiguration( configurations.properties( resource ), null, prefix );
}
catch ( ConfigurationException e )
{
@@ -356,7 +387,8 @@
try
{
logger.debug( "Loading XML configuration from classloader resource: {}", resource );
- configuration.addConfiguration( new XMLConfiguration( resource ), null, prefix );
+ Configurations configurations = new Configurations( );
+ configuration.addConfiguration( configurations.xml( resource ), null, prefix );
}
catch ( ConfigurationException e )
{
@@ -371,46 +403,48 @@
}
}
- public void addConfigurationFromFile( File file )
+ public void addConfigurationFromFile( Path file )
throws RegistryException
{
addConfigurationFromFile( file, null );
}
- public void addConfigurationFromFile( File file, String prefix )
+ public void addConfigurationFromFile( Path file, String prefix )
throws RegistryException
{
CombinedConfiguration configuration = (CombinedConfiguration) this.configuration;
- if ( file.getName( ).endsWith( ".properties" ) )
+ if ( file.getFileName( ).toString().endsWith( ".properties" ) )
{
try
{
logger.debug( "Loading properties configuration from file: {}", file );
- configuration.addConfiguration( new PropertiesConfiguration( file ), null, prefix );
+ Configurations configurations = new Configurations( );
+ configuration.addConfiguration( configurations.properties( file.toFile() ), null, prefix );
}
catch ( ConfigurationException e )
{
throw new RegistryException(
- "Unable to add configuration from file '" + file.getName( ) + "': " + e.getMessage( ), e );
+ "Unable to add configuration from file '" + file.getFileName( ).toString( ) + "': " + e.getMessage( ), e );
}
}
- else if ( file.getName( ).endsWith( ".xml" ) )
+ else if ( file.getFileName( ).toString( ).endsWith( ".xml" ) )
{
try
{
logger.debug( "Loading XML configuration from file: {}", file );
- configuration.addConfiguration( new XMLConfiguration( file ), null, prefix );
+ Configurations configurations = new Configurations( );
+ configuration.addConfiguration( configurations.xml( file.toFile() ), null, prefix );
}
catch ( ConfigurationException e )
{
throw new RegistryException(
- "Unable to add configuration from file '" + file.getName( ) + "': " + e.getMessage( ), e );
+ "Unable to add configuration from file '" + file.getFileName( ).toString( ) + "': " + e.getMessage( ), e );
}
}
else
{
throw new RegistryException(
- "Unable to add configuration from file '" + file.getName( ) + "': unrecognised type" );
+ "Unable to add configuration from file '" + file.getFileName( ).toString( ) + "': unrecognised type" );
}
}
@@ -423,18 +457,23 @@
CombinedConfiguration configuration;
if ( StringUtils.isNotBlank( properties ) )
{
- DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder( );
- DefaultExpressionEngine expressionEngine = new DefaultExpressionEngine( );
- expressionEngine.setPropertyDelimiter( propertyDelimiter );
- builder.setExpressionEngine( expressionEngine );
+ CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder( );
+ DefaultExpressionEngine expressionEngine = new DefaultExpressionEngine( DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS );
+ Parameters params = new Parameters();
StringSubstitutor substitutor = new StringSubstitutor( StringLookupFactory.INSTANCE.systemPropertyStringLookup( ) );
-
String interpolatedProps = substitutor.replace( properties );
+ Parameters p = new Parameters( );
+ ReaderBuilderParameters param = new ReaderBuilderParameters( );
+ param.setReader(new StringReader( interpolatedProps ) );
+
+ ReaderConfigurationBuilder<XMLConfiguration> defBuilder = new ReaderConfigurationBuilder<>( XMLConfiguration.class ).configure( param );
+
+
logger.debug( "Loading configuration into commons-configuration, xml {}", interpolatedProps );
- builder.load( new StringReader( interpolatedProps ) );
- configuration = builder.getConfiguration( false );
+ builder.configure( new Parameters( ).combined( ).setDefinitionBuilder( defBuilder ) );
+ configuration = builder.getConfiguration( );
configuration.setExpressionEngine( expressionEngine );
//configuration.set
}
@@ -463,7 +502,7 @@
{
CombinedConfiguration combinedConfiguration = (CombinedConfiguration) configuration;
Configuration configuration = combinedConfiguration.getConfiguration( name );
- return configuration == null ? null : new CommonsConfigurationRegistry( configuration );
+ return configuration == null ? null : new CommonsConfigurationRegistry( configurationBuilder, configuration );
}
public String getPropertyDelimiter( )
diff --git a/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/components/registry/commons/ReaderBuilderParameters.java b/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/components/registry/commons/ReaderBuilderParameters.java
new file mode 100644
index 0000000..0caa417
--- /dev/null
+++ b/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/components/registry/commons/ReaderBuilderParameters.java
@@ -0,0 +1,39 @@
+package org.apache.archiva.components.registry.commons;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.configuration2.builder.BasicBuilderParameters;
+
+import java.io.Reader;
+
+public class ReaderBuilderParameters extends BasicBuilderParameters
+{
+ public static final String INPUT_READER = RESERVED_PARAMETER_PREFIX+"inputReader";
+
+ public void setReader( Reader reader )
+ {
+ storeProperty(INPUT_READER, reader); ;
+ }
+
+ public Reader getReader( )
+ {
+ return (Reader) fetchProperty( INPUT_READER );
+ }
+}
diff --git a/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/components/registry/commons/ReaderConfigurationBuilder.java b/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/components/registry/commons/ReaderConfigurationBuilder.java
new file mode 100644
index 0000000..2436c8a
--- /dev/null
+++ b/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/components/registry/commons/ReaderConfigurationBuilder.java
@@ -0,0 +1,116 @@
+package org.apache.archiva.components.registry.commons;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.configuration2.FileBasedConfiguration;
+import org.apache.commons.configuration2.ImmutableConfiguration;
+import org.apache.commons.configuration2.builder.BasicConfigurationBuilder;
+import org.apache.commons.configuration2.builder.BuilderParameters;
+import org.apache.commons.configuration2.ex.ConfigurationException;
+import org.apache.commons.configuration2.io.FileBased;
+import org.apache.commons.configuration2.io.FileHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Reader;
+import java.util.Map;
+
+public class ReaderConfigurationBuilder<T extends ImmutableConfiguration> extends BasicConfigurationBuilder<T>
+{
+
+ private static final Logger log = LoggerFactory.getLogger( ReaderConfigurationBuilder.class );
+
+ private T configuration;
+
+ private Reader inputReader;
+
+ public ReaderConfigurationBuilder( Class<? extends T> resCls )
+ {
+ super( resCls );
+ }
+
+ public ReaderConfigurationBuilder( Class<? extends T> resCls, Map<String, Object> params )
+ {
+ super( resCls, params );
+ }
+
+ public ReaderConfigurationBuilder( Class<? extends T> resCls, Map<String, Object> params, boolean allowFailOnInit )
+ {
+ super( resCls, params, allowFailOnInit );
+ }
+
+ @Override
+ public T getConfiguration( ) throws ConfigurationException
+ {
+ if (configuration!=null) {
+ return configuration;
+ } else
+ {
+ T result = super.getConfiguration( );
+ log.debug( "Retrieving configuration: {}", result.getClass( ) );
+ log.debug( "Params: {}", getParameters( ) );
+ inputReader = (Reader) getParameters( ).get( ReaderBuilderParameters.INPUT_READER );
+ if ( result instanceof FileBasedConfiguration && inputReader!=null)
+ {
+ FileHandler fileHandler = new FileHandler( (FileBasedConfiguration) result );
+ fileHandler.load( inputReader );
+ log.debug( "Loaded from reader" );
+
+ }
+ else
+ {
+ log.warn( "This configuration is not file based" );
+ }
+ this.configuration = result;
+ return result;
+ }
+ }
+
+ @Override
+ public ReaderConfigurationBuilder<T> configure( BuilderParameters... params )
+ {
+ super.configure( params );
+ return this;
+ }
+
+ @Override
+ public synchronized ReaderConfigurationBuilder<T> setParameters( Map<String, Object> params )
+ {
+ super.setParameters( params );
+ return this;
+ }
+
+ @Override
+ public synchronized ReaderConfigurationBuilder<T> addParameters( Map<String, Object> params )
+ {
+ super.addParameters( params );
+ return this;
+ }
+
+ public Reader getInputReader( )
+ {
+ return inputReader;
+ }
+
+ public void setInputReader( Reader inputReader )
+ {
+ this.inputReader = inputReader;
+ }
+}
diff --git a/spring-registry/spring-registry-commons/src/test/java/org/apache/archiva/components/registry/test/CommonsConfigurationRegistryTest.java b/spring-registry/spring-registry-commons/src/test/java/org/apache/archiva/components/registry/test/CommonsConfigurationRegistryTest.java
index b6ccc0e..413e102 100644
--- a/spring-registry/spring-registry-commons/src/test/java/org/apache/archiva/components/registry/test/CommonsConfigurationRegistryTest.java
+++ b/spring-registry/spring-registry-commons/src/test/java/org/apache/archiva/components/registry/test/CommonsConfigurationRegistryTest.java
@@ -22,14 +22,15 @@
import org.apache.archiva.components.registry.Registry;
import org.apache.archiva.components.registry.RegistryException;
import org.apache.archiva.components.registry.RegistryListener;
-import org.apache.archiva.components.registry.commons.CommonsConfigurationRegistry;
-import org.apache.commons.configuration.XMLConfiguration;
+import org.apache.commons.configuration2.XMLConfiguration;
+import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import java.io.File;
+import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -143,7 +144,7 @@
{
registry = getRegistry( "default" );
- registry.addConfigurationFromFile( new File( "./src/test/resources/org/codehaus/plexus/registry/test.xml" ) );
+ registry.addConfigurationFromFile( Paths.get( "src/test/resources/org/codehaus/plexus/registry/test.xml" ) );
assertEquals( "Check system property default", System.getProperty( "user.dir" ),
registry.getString( "user.dir" ) );
@@ -157,7 +158,7 @@
registry = getRegistry( "default" );
registry.addConfigurationFromFile(
- new File( "./src/test/resources/org/codehaus/plexus/registry/test.properties" ) );
+ Paths.get( "src/test/resources/org/codehaus/plexus/registry/test.properties" ) );
assertEquals( "Check system property default", System.getProperty( "user.dir" ),
registry.getString( "user.dir" ) );
@@ -211,7 +212,7 @@
try
{
registry.addConfigurationFromFile(
- new File( "./src/test/resources/org/codehaus/plexus/registry/test.foo" ) );
+ Paths.get( "src/test/resources/org/codehaus/plexus/registry/test.foo" ) );
fail( );
}
catch ( RegistryException e )
@@ -360,7 +361,9 @@
registry.remove( "listElements.listElement(1)" );
registry.save( );
- XMLConfiguration configuration = new XMLConfiguration( dest );
+ Configurations configurations = new Configurations( );
+
+ XMLConfiguration configuration = configurations.xml( dest );
assertEquals( Arrays.asList( new String[]{"1", "3"} ), configuration.getList( "listElements.listElement" ) );
// file in ${basedir}/target/conf/shared.xml
@@ -368,32 +371,12 @@
section.setString( "foo", "zloug" );
section.save( );
- configuration = new XMLConfiguration( new File( "target/conf/shared.xml" ) );
+ configuration = configurations.xml( new File( "target/conf/shared.xml" ) );
assertNotNull( configuration.getString( "foo" ) );
}
- @Test
- public void test_listener( )
- throws Exception
- {
- registry = getRegistry( "default" );
-
- int listenerSize = CommonsConfigurationRegistry.class.cast( registry ).getChangeListenersSize( );
-
- MockChangeListener mockChangeListener = new MockChangeListener( );
-
- registry.addChangeListener( mockChangeListener );
-
- registry.addChangeListener( new MockChangeListener( ) );
-
- assertEquals( listenerSize + 2, CommonsConfigurationRegistry.class.cast( registry ).getChangeListenersSize( ) );
-
- registry.removeChangeListener( mockChangeListener );
-
- assertEquals( listenerSize + 1, CommonsConfigurationRegistry.class.cast( registry ).getChangeListenersSize( ) );
- }
private static class MockChangeListener
implements RegistryListener
diff --git a/spring-registry/spring-registry-commons/src/test/resources/log4j2-test.xml b/spring-registry/spring-registry-commons/src/test/resources/log4j2-test.xml
new file mode 100644
index 0000000..6eb5e39
--- /dev/null
+++ b/spring-registry/spring-registry-commons/src/test/resources/log4j2-test.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ -->
+<configuration>
+ <appenders>
+ <Console name="console" target="SYSTEM_OUT">
+ <PatternLayout pattern="[%t] %-5p %c %x - %m%n"/>
+ </Console>
+ </appenders>
+ <loggers>
+ <logger name="org.apache.archiva" level="warn"/>
+ <logger name="org.apache.archiva.components.registry" level="trace"/>
+
+ <root level="error" includeLocation="true">
+ <appender-ref ref="console"/>
+ </root>
+ </loggers>
+</configuration>
+
+