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