| 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.archiva.components.registry.Registry; |
| import org.apache.archiva.components.registry.RegistryException; |
| import org.apache.archiva.components.registry.RegistryListener; |
| import org.apache.commons.configuration2.CombinedConfiguration; |
| import org.apache.commons.configuration2.Configuration; |
| 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.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 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; |
| import java.util.Iterator; |
| import java.util.List; |
| 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 |
| * enables a variety of sources to be used, including XML files, properties, JNDI, JDBC, etc. |
| * <p/> |
| * The component can be configured using the {@link #properties} configuration item, the content of which should take |
| * the format of an input to the Commons Configuration |
| * <a href="http://commons.apache.org/commons/configuration/howto_configurationbuilder.html">configuration |
| * builder</a>. |
| */ |
| @Service( "commons-configuration" ) |
| public class CommonsConfigurationRegistry |
| implements Registry |
| { |
| /** |
| * The combined configuration instance that houses the registry. |
| */ |
| private Configuration configuration; |
| private ConfigurationBuilder<? extends Configuration> configurationBuilder; |
| |
| private Logger logger = LoggerFactory.getLogger( getClass( ) ); |
| |
| private String propertyDelimiter = "."; |
| |
| /** |
| * The configuration properties for the registry. This should take the format of an input to the Commons |
| * Configuration |
| * <a href="http://commons.apache.org/configuration/howto_configurationbuilder.html">configuration |
| * builder</a>. |
| */ |
| private String properties; |
| |
| |
| public CommonsConfigurationRegistry( ) |
| { |
| // default constructor |
| logger.debug( "empty constructor" ); |
| 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( "configurationbuilder can not be null" ); |
| } |
| |
| this.configurationBuilder = configurationBuilder; |
| try |
| { |
| this.configuration = configurationBuilder.getConfiguration( ); |
| } |
| catch ( ConfigurationException e ) |
| { |
| logger.error( "Could not retrieve configuration" ); |
| } |
| } |
| |
| 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( ); |
| buffer.append( "Configuration Dump." ); |
| for ( Iterator i = configuration.getKeys( ); i.hasNext( ); ) |
| { |
| String key = (String) i.next( ); |
| Object value = configuration.getProperty( key ); |
| buffer.append( "\n\"" ).append( key ).append( "\" = \"" ).append( value ).append( "\"" ); |
| } |
| return buffer.toString( ); |
| } |
| |
| public boolean isEmpty( ) |
| { |
| return configuration.isEmpty( ); |
| } |
| |
| public Registry getSubset( String key ) |
| { |
| return new CommonsConfigurationRegistry( configurationBuilder, configuration.subset( key ) ); |
| } |
| |
| public List getList( String key ) |
| { |
| return configuration.getList( key ); |
| } |
| |
| public List<Registry> getSubsetList( String key ) |
| { |
| List<Registry> subsets = new ArrayList<>( ); |
| |
| boolean done = false; |
| do |
| { |
| Registry registry = getSubset( key + "(" + subsets.size( ) + ")" ); |
| if ( !registry.isEmpty( ) ) |
| { |
| subsets.add( registry ); |
| } |
| else |
| { |
| done = true; |
| } |
| } |
| while ( !done ); |
| |
| return subsets; |
| } |
| |
| public Properties getProperties( String key ) |
| { |
| Configuration configuration = this.configuration.subset( key ); |
| |
| Properties properties = new Properties( ); |
| for ( Iterator i = configuration.getKeys( ); i.hasNext( ); ) |
| { |
| String property = (String) i.next( ); |
| List l = configuration.getList( property ); |
| StringBuilder sb = new StringBuilder( ); |
| for ( Object element : l ) |
| { |
| sb.append( (String) element ); |
| sb.append( "," ); |
| } |
| if ( sb.length( ) > 0 ) |
| { |
| sb.deleteCharAt( sb.length( ) - 1 ); |
| } |
| properties.setProperty( property, sb.toString( ) ); |
| } |
| return properties; |
| } |
| |
| public void save( ) |
| throws RegistryException |
| { |
| if ( configurationBuilder instanceof FileBasedConfigurationBuilder ) |
| { |
| FileBasedConfigurationBuilder fileConfigurationBuilder = (FileBasedConfigurationBuilder) configurationBuilder; |
| try |
| { |
| fileConfigurationBuilder.save( ); |
| } |
| catch ( ConfigurationException e ) |
| { |
| throw new RegistryException( e.getMessage( ), e ); |
| } |
| } |
| else |
| { |
| throw new RegistryException( "Can only save file-based configurations" ); |
| } |
| } |
| |
| public void addChangeListener( RegistryListener listener ) |
| { |
| EventSource eventSource = EventSource.class.cast( this.configuration ); |
| |
| eventSource.addEventListener( Event.ANY, new ConfigurationListenerDelegate( listener, this ) ); |
| } |
| |
| @Override |
| public boolean removeChangeListener( RegistryListener listener ) |
| { |
| EventSource eventSource = EventSource.class.cast( this.configuration ); |
| |
| boolean removed = |
| eventSource.removeEventListener( Event.ANY, new ConfigurationListenerDelegate( listener, this ) ); |
| |
| return removed; |
| } |
| |
| |
| |
| public Collection<String> getKeys( ) |
| { |
| Set<String> keys = new HashSet<String>( ); |
| |
| for ( Iterator<String> i = configuration.getKeys( ); i.hasNext( ); ) |
| { |
| String key = i.next( ); |
| |
| int index = key.indexOf( '.' ); |
| if ( index < 0 ) |
| { |
| keys.add( key ); |
| } |
| else |
| { |
| keys.add( key.substring( 0, index ) ); |
| } |
| } |
| |
| return keys; |
| } |
| |
| public Collection getFullKeys( ) |
| { |
| Set<String> keys = new HashSet<String>( ); |
| |
| for ( Iterator<String> i = configuration.getKeys( ); i.hasNext( ); ) |
| { |
| keys.add( i.next( ) ); |
| } |
| |
| return keys; |
| } |
| |
| public void remove( String key ) |
| { |
| configuration.clearProperty( key ); |
| } |
| |
| public void removeSubset( String key ) |
| { |
| // create temporary list since removing a key will modify the iterator from configuration |
| List keys = new ArrayList( ); |
| for ( Iterator i = configuration.getKeys( key ); i.hasNext( ); ) |
| { |
| keys.add( i.next( ) ); |
| } |
| |
| for ( Iterator i = keys.iterator( ); i.hasNext( ); ) |
| { |
| configuration.clearProperty( (String) i.next( ) ); |
| } |
| } |
| |
| public String getString( String key ) |
| { |
| return configuration.getString( key ); |
| } |
| |
| public String getString( String key, String defaultValue ) |
| { |
| return configuration.getString( key, defaultValue ); |
| } |
| |
| public void setString( String key, String value ) |
| { |
| configuration.setProperty( key, value ); |
| } |
| |
| public int getInt( String key ) |
| { |
| return configuration.getInt( key ); |
| } |
| |
| public int getInt( String key, int defaultValue ) |
| { |
| return configuration.getInt( key, defaultValue ); |
| } |
| |
| public void setInt( String key, int value ) |
| { |
| configuration.setProperty( key, Integer.valueOf( value ) ); |
| } |
| |
| public boolean getBoolean( String key ) |
| { |
| return configuration.getBoolean( key ); |
| } |
| |
| public boolean getBoolean( String key, boolean defaultValue ) |
| { |
| return configuration.getBoolean( key, defaultValue ); |
| } |
| |
| public void setBoolean( String key, boolean value ) |
| { |
| configuration.setProperty( key, Boolean.valueOf( value ) ); |
| } |
| |
| public void addConfigurationFromResource( String resource ) |
| throws RegistryException |
| { |
| addConfigurationFromResource( resource, null ); |
| } |
| |
| public void addConfigurationFromResource( String resource, String prefix ) |
| throws RegistryException |
| { |
| if ( !( this.configuration instanceof CombinedConfiguration ) ) |
| { |
| throw new RegistryException( "This is not a combined configuration so cannot add resource" ); |
| } |
| CombinedConfiguration configuration = (CombinedConfiguration) this.configuration; |
| if ( resource.endsWith( ".properties" ) ) |
| { |
| try |
| { |
| logger.debug( "Loading properties configuration from classloader resource: {}", resource ); |
| Configurations configurations = new Configurations( ); |
| configuration.addConfiguration( configurations.properties( resource ), null, prefix ); |
| } |
| catch ( ConfigurationException e ) |
| { |
| throw new RegistryException( |
| "Unable to add configuration from resource '" + resource + "': " + e.getMessage( ), e ); |
| } |
| } |
| else if ( resource.endsWith( ".xml" ) ) |
| { |
| try |
| { |
| logger.debug( "Loading XML configuration from classloader resource: {}", resource ); |
| Configurations configurations = new Configurations( ); |
| configuration.addConfiguration( configurations.xml( resource ), null, prefix ); |
| } |
| catch ( ConfigurationException e ) |
| { |
| throw new RegistryException( |
| "Unable to add configuration from resource '" + resource + "': " + e.getMessage( ), e ); |
| } |
| } |
| else |
| { |
| throw new RegistryException( |
| "Unable to add configuration from resource '" + resource + "': unrecognised type" ); |
| } |
| } |
| |
| public void addConfigurationFromFile( Path file ) |
| throws RegistryException |
| { |
| addConfigurationFromFile( file, null ); |
| } |
| |
| public void addConfigurationFromFile( Path file, String prefix ) |
| throws RegistryException |
| { |
| CombinedConfiguration configuration = (CombinedConfiguration) this.configuration; |
| if ( file.getFileName( ).toString().endsWith( ".properties" ) ) |
| { |
| try |
| { |
| logger.debug( "Loading properties configuration from file: {}", file ); |
| 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.getFileName( ).toString( ) + "': " + e.getMessage( ), e ); |
| } |
| } |
| else if ( file.getFileName( ).toString( ).endsWith( ".xml" ) ) |
| { |
| try |
| { |
| logger.debug( "Loading XML configuration from file: {}", file ); |
| 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.getFileName( ).toString( ) + "': " + e.getMessage( ), e ); |
| } |
| } |
| else |
| { |
| throw new RegistryException( |
| "Unable to add configuration from file '" + file.getFileName( ).toString( ) + "': unrecognised type" ); |
| } |
| } |
| |
| @PostConstruct |
| public void initialize( ) |
| throws RegistryException |
| { |
| try |
| { |
| CombinedConfiguration configuration; |
| if ( StringUtils.isNotBlank( properties ) ) |
| { |
| 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.configure( new Parameters( ).combined( ).setDefinitionBuilder( defBuilder ) ); |
| configuration = builder.getConfiguration( ); |
| configuration.setExpressionEngine( expressionEngine ); |
| //configuration.set |
| } |
| else |
| { |
| logger.debug( "Creating a default configuration - no configuration was provided" ); |
| configuration = new CombinedConfiguration( ); |
| } |
| |
| configuration.addConfiguration( new SystemConfiguration( ) ); |
| |
| this.configuration = configuration; |
| } |
| catch ( ConfigurationException e ) |
| { |
| throw new RuntimeException( e.getMessage( ), e ); |
| } |
| } |
| |
| public void setProperties( String properties ) |
| { |
| this.properties = properties; |
| } |
| |
| public Registry getSection( String name ) |
| { |
| CombinedConfiguration combinedConfiguration = (CombinedConfiguration) configuration; |
| Configuration configuration = combinedConfiguration.getConfiguration( name ); |
| return configuration == null ? null : new CommonsConfigurationRegistry( configurationBuilder, configuration ); |
| } |
| |
| public String getPropertyDelimiter( ) |
| { |
| return propertyDelimiter; |
| } |
| |
| public void setPropertyDelimiter( String propertyDelimiter ) |
| { |
| this.propertyDelimiter = propertyDelimiter; |
| } |
| } |