001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.configuration2.builder.combined;
018
019import java.util.Arrays;
020import java.util.Collection;
021
022import org.apache.commons.configuration2.CombinedConfiguration;
023import org.apache.commons.configuration2.Configuration;
024import org.apache.commons.configuration2.builder.BasicBuilderParameters;
025import org.apache.commons.configuration2.builder.BasicConfigurationBuilder;
026import org.apache.commons.configuration2.builder.BuilderParameters;
027
028/**
029 * <p>
030 * A specialized {@code ConfigurationBuilderProvider} implementation which deals with combined configuration builders.
031 * </p>
032 * <p>
033 * This class is used to support {@code <configuration>} elements in configuration definition files. The provider
034 * creates another {@link CombinedConfigurationBuilder} which inherits some of the properties from its parent builder.
035 * </p>
036 *
037 * @since 2.0
038 */
039public class CombinedConfigurationBuilderProvider extends BaseConfigurationBuilderProvider {
040
041    /** Constant for the name of the supported builder class. */
042    private static final String BUILDER_CLASS = "org.apache.commons.configuration2.builder.combined.CombinedConfigurationBuilder";
043
044    /** Constant for the name of the supported reloading builder class. */
045    private static final String RELOADING_BUILDER_CLASS = "org.apache.commons.configuration2.builder.combined.ReloadingCombinedConfigurationBuilder";
046
047    /** Constant for the name of the supported configuration class. */
048    private static final String CONFIGURATION_CLASS = "org.apache.commons.configuration2.CombinedConfiguration";
049
050    /** Constant for the combined configuration builder parameters class. */
051    private static final String COMBINED_PARAMS = "org.apache.commons.configuration2.builder.combined.CombinedBuilderParametersImpl";
052
053    /** Constant for the name of the file-based builder parameters class. */
054    private static final String FILE_PARAMS = "org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl";
055
056    /**
057     * Populates the specified parameters object with properties from the given configuration. This method is used to set
058     * default values for basic properties based on the result configuration of the parent builder.
059     *
060     * @param config the configuration whose properties are to be copied
061     * @param params the target parameters object
062     */
063    private static void setUpBasicParameters(final CombinedConfiguration config, final BasicBuilderParameters params) {
064        params.setListDelimiterHandler(config.getListDelimiterHandler()).setLogger(config.getLogger())
065            .setThrowExceptionOnMissing(config.isThrowExceptionOnMissing()).setConfigurationDecoder(config.getConfigurationDecoder());
066    }
067
068    /**
069     * Creates a new instance of {@code CombinedConfigurationBuilderProvider}.
070     */
071    public CombinedConfigurationBuilderProvider() {
072        super(BUILDER_CLASS, RELOADING_BUILDER_CLASS, CONFIGURATION_CLASS, Arrays.asList(COMBINED_PARAMS, FILE_PARAMS));
073    }
074
075    /**
076     * {@inheritDoc} This implementation creates the result builder object directly, not using reflection. (The
077     * reflection-based approach of the base class does not work here because a combined configuration builder has
078     * constructors with a different signature.) It also performs some additional initializations.
079     */
080    @Override
081    protected BasicConfigurationBuilder<? extends Configuration> createBuilder(final ConfigurationDeclaration decl, final Collection<BuilderParameters> params)
082        throws Exception {
083        final CombinedConfigurationBuilder builder;
084        if (decl.isReload()) {
085            builder = new ReloadingCombinedConfigurationBuilder();
086        } else {
087            builder = new CombinedConfigurationBuilder();
088        }
089        decl.getConfigurationBuilder().initChildEventListeners(builder);
090        return builder;
091    }
092
093    /**
094     * {@inheritDoc} This implementation pre-fills basic parameters from the basic properties of the parent builder's result
095     * configuration.
096     */
097    @Override
098    protected void initializeParameterObjects(final ConfigurationDeclaration decl, final Collection<BuilderParameters> params) throws Exception {
099        // we know that the first object is the combined builder parameters
100        // object
101        final BasicBuilderParameters basicParams = (BasicBuilderParameters) params.iterator().next();
102        setUpBasicParameters(decl.getConfigurationBuilder().getConfigurationUnderConstruction(), basicParams);
103        // now properties set explicitly can be overridden
104        super.initializeParameterObjects(decl, params);
105    }
106}