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 *     http://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    /** Constant for the name of the supported builder class. */
041    private static final String BUILDER_CLASS = "org.apache.commons.configuration2.builder.combined.CombinedConfigurationBuilder";
042
043    /** Constant for the name of the supported reloading builder class. */
044    private static final String RELOADING_BUILDER_CLASS = "org.apache.commons.configuration2.builder.combined.ReloadingCombinedConfigurationBuilder";
045
046    /** Constant for the name of the supported configuration class. */
047    private static final String CONFIGURATION_CLASS = "org.apache.commons.configuration2.CombinedConfiguration";
048
049    /** Constant for the combined configuration builder parameters class. */
050    private static final String COMBINED_PARAMS = "org.apache.commons.configuration2.builder.combined.CombinedBuilderParametersImpl";
051
052    /** Constant for the name of the file-based builder parameters class. */
053    private static final String FILE_PARAMS = "org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl";
054
055    /**
056     * Creates a new instance of {@code CombinedConfigurationBuilderProvider}.
057     */
058    public CombinedConfigurationBuilderProvider() {
059        super(BUILDER_CLASS, RELOADING_BUILDER_CLASS, CONFIGURATION_CLASS, Arrays.asList(COMBINED_PARAMS, FILE_PARAMS));
060    }
061
062    /**
063     * {@inheritDoc} This implementation creates the result builder object directly, not using reflection. (The
064     * reflection-based approach of the base class does not work here because a combined configuration builder has
065     * constructors with a different signature.) It also performs some additional initializations.
066     */
067    @Override
068    protected BasicConfigurationBuilder<? extends Configuration> createBuilder(final ConfigurationDeclaration decl, final Collection<BuilderParameters> params)
069        throws Exception {
070        final CombinedConfigurationBuilder builder;
071        if (decl.isReload()) {
072            builder = new ReloadingCombinedConfigurationBuilder();
073        } else {
074            builder = new CombinedConfigurationBuilder();
075        }
076        decl.getConfigurationBuilder().initChildEventListeners(builder);
077        return builder;
078    }
079
080    /**
081     * {@inheritDoc} This implementation pre-fills basic parameters from the basic properties of the parent builder's result
082     * configuration.
083     */
084    @Override
085    protected void initializeParameterObjects(final ConfigurationDeclaration decl, final Collection<BuilderParameters> params) throws Exception {
086        // we know that the first object is the combined builder parameters
087        // object
088        final BasicBuilderParameters basicParams = (BasicBuilderParameters) params.iterator().next();
089        setUpBasicParameters(decl.getConfigurationBuilder().getConfigurationUnderConstruction(), basicParams);
090        // now properties set explicitly can be overridden
091        super.initializeParameterObjects(decl, params);
092    }
093
094    /**
095     * Populates the specified parameters object with properties from the given configuration. This method is used to set
096     * default values for basic properties based on the result configuration of the parent builder.
097     *
098     * @param config the configuration whose properties are to be copied
099     * @param params the target parameters object
100     */
101    private static void setUpBasicParameters(final CombinedConfiguration config, final BasicBuilderParameters params) {
102        params.setListDelimiterHandler(config.getListDelimiterHandler()).setLogger(config.getLogger())
103            .setThrowExceptionOnMissing(config.isThrowExceptionOnMissing()).setConfigurationDecoder(config.getConfigurationDecoder());
104    }
105}