Skip to content
Permalink
Browse files
Updating save mechanism
  • Loading branch information
effrafax committed Dec 8, 2019
1 parent ebe803d commit 258d3a64f327073b10ec41acacf489b2af5ed0f9
Showing 4 changed files with 101 additions and 51 deletions.
@@ -25,6 +25,7 @@
import org.apache.commons.configuration2.CombinedConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.HierarchicalConfiguration;
import org.apache.commons.configuration2.SystemConfiguration;
import org.apache.commons.configuration2.XMLConfiguration;
import org.apache.commons.configuration2.builder.ConfigurationBuilder;
@@ -37,6 +38,7 @@
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.configuration2.tree.ImmutableNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.text.lookup.StringLookupFactory;
@@ -68,6 +70,15 @@
* the format of an input to the Commons Configuration
* <a href="http://commons.apache.org/commons/configuration/howto_configurationbuilder.html">configuration
* builder</a>.
*
* If you initialize a <code>CombinedConfiguration</code>, which is the default, if you do not give a builder in the
* constructor or use the {@link #setInitialConfiguration(String)} or {@link #setInitialConfigurationFile(Path)} methods,
* then you should be careful with modifications and the {@link #save()} method.
* You should always change your configuration with a given section, which represents a concrete configuration source
* in the <code>CombinedConfiguration</code>. The section names correspond to the config-name attributes in the
* configuration definition.
*
*
*/
@Service( "commons-configuration" )
public class CommonsConfigurationRegistry
@@ -318,19 +329,17 @@ public Collection getFullKeys( )
return keys;
}

/**
* Removes a given configuration node from the configuration. For a combined configuration, this
* may be only in memory and can not be persisted. The method runs the <code>clearProperty()</code>
* on the configuration instance.
*
* @param key the key to remove
*/
@Override
public void remove( String key )
{
if (configuration instanceof CombinedConfiguration) {
CombinedConfiguration config = (CombinedConfiguration) configuration;
Set<Configuration> source = config.getSources( key );
if (source != null) {

}
} else
{
configuration.clearProperty( key );
}
configuration.clearProperty( key );
}

@Override
@@ -403,13 +412,32 @@ public void setBoolean( String key, boolean value )
configuration.setProperty( key, Boolean.valueOf( value ) );
}

/**
*
* Adds a new configuration source to the combined configuration.
*
* This is only possible with a combined configuration (which is the default).
*
* @param resource the location to load the configuration from
* @throws RegistryException if the configuration is not a <code>CombinedConfiguration</code>
*/
@Override
public void addConfigurationFromResource( String resource )
throws RegistryException
{
addConfigurationFromResource( resource, null );
}

/**
*
* Adds a new configuration source to the combined configuration.
*
* This is only possible with a combined configuration (which is the default).
*
* @param resource the location to load the configuration from
* @param prefix the prefix where the root of the given configuration is placed
* @throws RegistryException if the configuration is not a <code>CombinedConfiguration</code>
*/
@Override
public void addConfigurationFromResource( String resource, String prefix )
throws RegistryException
@@ -454,13 +482,32 @@ else if ( resource.endsWith( ".xml" ) )
}
}

/**
*
* Adds a new configuration source to the combined configuration.
*
* This is only possible with a combined configuration (which is the default).
*
* @param file the path where the configuration can be loaded
* @throws RegistryException if the configuration is not a <code>CombinedConfiguration</code>
*/
@Override
public void addConfigurationFromFile( Path file )
throws RegistryException
{
addConfigurationFromFile( file, null );
}

/**
*
* Adds a new configuration source to the combined configuration.
*
* This is only possible with a combined configuration (which is the default).
*
* @param file the path, where the configuration can be loaded
* @param prefix the prefix where the root of the given configuration is placed
* @throws RegistryException if the configuration is not a <code>CombinedConfiguration</code>
*/
@Override
public void addConfigurationFromFile( Path file, String prefix )
throws RegistryException
@@ -501,11 +548,23 @@ else if ( file.getFileName( ).toString( ).endsWith( ".xml" ) )
}
}


/**
* This method tries to read a combined configuration definition either from the string given
* by {@link #setInitialConfiguration(String)} or from the file given by {@link #setInitialConfigurationFile(Path)}
*
* The initialization assumes that override combiner is used and tries to find the first writable configuration
* source as save target.
*
* @throws RegistryException if the configuration initialization failed
*/
@Override
@PostConstruct
public void initialize( )
throws RegistryException
{


synchronized (this)
{
try
@@ -552,6 +611,14 @@ else if ( StringUtils.isNotBlank( configurationDefinition ) )

}

HierarchicalConfiguration<?> defConfig = builder.getDefinitionBuilder( ).getConfiguration( );
logger.debug( "Node def children: {}", defConfig.getNodeModel( ).getInMemoryRepresentation( ).getChildren( ) );
for ( ImmutableNode child : defConfig.getNodeModel().getInMemoryRepresentation().getChildren()) {
logger.debug( "Child: {}, Attributes: {}" , child.getNodeName( ), child.getAttributes( ));

}



this.configuration = configuration;
this.configurationBuilder = builder;
@@ -1,3 +1,21 @@
<!--
~ 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>
<system />
<properties config-name="default-properties" config-optional="true" fileName="commons-registry-configuration.properties" />
@@ -354,59 +354,23 @@ public void testSaveSection( )

registry = getRegistry( "test-save" );

Registry registry = this.registry.getSection( "org.codehaus.plexus.registry" );
assertEquals( "check list elements", Arrays.asList( new String[]{"1", "2", "3"} ),
registry.getList( "listElements.listElement" ) );

registry.remove( "listElements.listElement(1)" );
registry.save( );

Configurations configurations = new Configurations( );

XMLConfiguration configuration = configurations.xml( dest );
assertEquals( Arrays.asList( new String[]{"1", "3"} ), configuration.getList( "listElements.listElement" ) );
// Testing removal does not make sense here, because commons configurations 2 has
// not the same parent nodes in the combined and source configuration. So the
// a node is only removed in the combined tree, but not in the source configuration.


// file in ${basedir}/target/conf/shared.xml
Registry section = this.registry.getSection( "org.apache.maven.shared.app.user" );
section.setString( "foo", "zloug" );
section.save( );

configuration = configurations.xml( new File( "target/conf/shared.xml" ) );
XMLConfiguration configuration = configurations.xml( new File( "target/conf/shared.xml" ) );
assertNotNull( configuration.getString( "foo" ) );

}

@Test
public void testSave( )
throws Exception
{
File src = new File( "./src/test/resources/test-save.xml" );
File dest = new File( "./target/test-classes/test-save.xml" );
FileCopyUtils.copy( src, dest );

registry = getRegistry( "test-save" );

assertEquals( "check list elements", Arrays.asList( new String[]{"1", "2", "3"} ),
registry.getList( "org.codehaus.plexus.registry.listElements.listElement" ) );

registry.remove( "org.codehaus.plexus.registry.listElements.listElement(1)" );
registry.save( );

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
Registry section = this.registry.getSection( "org.apache.maven.shared.app.user" );
registry.setString( "org.apache.maven.shared.app.user.foo", "zloug" );
registry.save( );

configuration = configurations.xml( new File( "target/conf/shared.xml" ) );
assertNotNull( configuration.getString( "foo" ) );
assertEquals( "zloug", configuration.getString( "foo" ) );

}


private static class MockChangeListener
@@ -20,4 +20,5 @@
<listElement>2</listElement>
<listElement>3</listElement>
</listElements>
<singleElement>123</singleElement>
</registry>

0 comments on commit 258d3a6

Please sign in to comment.