Skip to content
Permalink
Browse files
Adapting to new configuration 2 api
  • Loading branch information
effrafax committed Dec 6, 2019
1 parent 1670b17 commit 3617088ada796aa9a8b3cc93574314bf7c80ebb6
Showing 6 changed files with 270 additions and 57 deletions.
@@ -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 @@ void addConfigurationFromResource( String resource, String prefix )
* @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 @@ void addConfigurationFromFile( File file )
* @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;

/**
@@ -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 @@ public CommonsConfigurationRegistry( )
{
// 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 Configuratio
}
}

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 boolean isEmpty( )

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 @@ public void addConfigurationFromResource( String resource, String prefix )
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 @@ else if ( resource.endsWith( ".xml" ) )
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 @@ else if ( resource.endsWith( ".xml" ) )
}
}

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 @@ public void initialize( )
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 @@ public Registry getSection( String name )
{
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( )
@@ -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 );
}
}

0 comments on commit 3617088

Please sign in to comment.