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