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