CombinedConfigurationBuilderProvider.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.builder.combined;

  18. import java.util.Arrays;
  19. import java.util.Collection;

  20. import org.apache.commons.configuration2.CombinedConfiguration;
  21. import org.apache.commons.configuration2.Configuration;
  22. import org.apache.commons.configuration2.builder.BasicBuilderParameters;
  23. import org.apache.commons.configuration2.builder.BasicConfigurationBuilder;
  24. import org.apache.commons.configuration2.builder.BuilderParameters;

  25. /**
  26.  * <p>
  27.  * A specialized {@code ConfigurationBuilderProvider} implementation which deals with combined configuration builders.
  28.  * </p>
  29.  * <p>
  30.  * This class is used to support {@code <configuration>} elements in configuration definition files. The provider
  31.  * creates another {@link CombinedConfigurationBuilder} which inherits some of the properties from its parent builder.
  32.  * </p>
  33.  *
  34.  * @since 2.0
  35.  */
  36. public class CombinedConfigurationBuilderProvider extends BaseConfigurationBuilderProvider {
  37.     /** Constant for the name of the supported builder class. */
  38.     private static final String BUILDER_CLASS = "org.apache.commons.configuration2.builder.combined.CombinedConfigurationBuilder";

  39.     /** Constant for the name of the supported reloading builder class. */
  40.     private static final String RELOADING_BUILDER_CLASS = "org.apache.commons.configuration2.builder.combined.ReloadingCombinedConfigurationBuilder";

  41.     /** Constant for the name of the supported configuration class. */
  42.     private static final String CONFIGURATION_CLASS = "org.apache.commons.configuration2.CombinedConfiguration";

  43.     /** Constant for the combined configuration builder parameters class. */
  44.     private static final String COMBINED_PARAMS = "org.apache.commons.configuration2.builder.combined.CombinedBuilderParametersImpl";

  45.     /** Constant for the name of the file-based builder parameters class. */
  46.     private static final String FILE_PARAMS = "org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl";

  47.     /**
  48.      * Populates the specified parameters object with properties from the given configuration. This method is used to set
  49.      * default values for basic properties based on the result configuration of the parent builder.
  50.      *
  51.      * @param config the configuration whose properties are to be copied
  52.      * @param params the target parameters object
  53.      */
  54.     private static void setUpBasicParameters(final CombinedConfiguration config, final BasicBuilderParameters params) {
  55.         params.setListDelimiterHandler(config.getListDelimiterHandler()).setLogger(config.getLogger())
  56.             .setThrowExceptionOnMissing(config.isThrowExceptionOnMissing()).setConfigurationDecoder(config.getConfigurationDecoder());
  57.     }

  58.     /**
  59.      * Creates a new instance of {@code CombinedConfigurationBuilderProvider}.
  60.      */
  61.     public CombinedConfigurationBuilderProvider() {
  62.         super(BUILDER_CLASS, RELOADING_BUILDER_CLASS, CONFIGURATION_CLASS, Arrays.asList(COMBINED_PARAMS, FILE_PARAMS));
  63.     }

  64.     /**
  65.      * {@inheritDoc} This implementation creates the result builder object directly, not using reflection. (The
  66.      * reflection-based approach of the base class does not work here because a combined configuration builder has
  67.      * constructors with a different signature.) It also performs some additional initializations.
  68.      */
  69.     @Override
  70.     protected BasicConfigurationBuilder<? extends Configuration> createBuilder(final ConfigurationDeclaration decl, final Collection<BuilderParameters> params)
  71.         throws Exception {
  72.         final CombinedConfigurationBuilder builder;
  73.         if (decl.isReload()) {
  74.             builder = new ReloadingCombinedConfigurationBuilder();
  75.         } else {
  76.             builder = new CombinedConfigurationBuilder();
  77.         }
  78.         decl.getConfigurationBuilder().initChildEventListeners(builder);
  79.         return builder;
  80.     }

  81.     /**
  82.      * {@inheritDoc} This implementation pre-fills basic parameters from the basic properties of the parent builder's result
  83.      * configuration.
  84.      */
  85.     @Override
  86.     protected void initializeParameterObjects(final ConfigurationDeclaration decl, final Collection<BuilderParameters> params) throws Exception {
  87.         // we know that the first object is the combined builder parameters
  88.         // object
  89.         final BasicBuilderParameters basicParams = (BasicBuilderParameters) params.iterator().next();
  90.         setUpBasicParameters(decl.getConfigurationBuilder().getConfigurationUnderConstruction(), basicParams);
  91.         // now properties set explicitly can be overridden
  92.         super.initializeParameterObjects(decl, params);
  93.     }
  94. }