View Javadoc
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    *     https://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  
19  import java.util.Arrays;
20  import java.util.Collection;
21  
22  import org.apache.commons.configuration2.CombinedConfiguration;
23  import org.apache.commons.configuration2.Configuration;
24  import org.apache.commons.configuration2.builder.BasicBuilderParameters;
25  import org.apache.commons.configuration2.builder.BasicConfigurationBuilder;
26  import org.apache.commons.configuration2.builder.BuilderParameters;
27  
28  /**
29   * <p>
30   * A specialized {@code ConfigurationBuilderProvider} implementation which deals with combined configuration builders.
31   * </p>
32   * <p>
33   * This class is used to support {@code <configuration>} elements in configuration definition files. The provider
34   * creates another {@link CombinedConfigurationBuilder} which inherits some of the properties from its parent builder.
35   * </p>
36   *
37   * @since 2.0
38   */
39  public class CombinedConfigurationBuilderProvider extends BaseConfigurationBuilderProvider {
40  
41      /** Constant for the name of the supported builder class. */
42      private static final String BUILDER_CLASS = "org.apache.commons.configuration2.builder.combined.CombinedConfigurationBuilder";
43  
44      /** Constant for the name of the supported reloading builder class. */
45      private static final String RELOADING_BUILDER_CLASS = "org.apache.commons.configuration2.builder.combined.ReloadingCombinedConfigurationBuilder";
46  
47      /** Constant for the name of the supported configuration class. */
48      private static final String CONFIGURATION_CLASS = "org.apache.commons.configuration2.CombinedConfiguration";
49  
50      /** Constant for the combined configuration builder parameters class. */
51      private static final String COMBINED_PARAMS = "org.apache.commons.configuration2.builder.combined.CombinedBuilderParametersImpl";
52  
53      /** Constant for the name of the file-based builder parameters class. */
54      private static final String FILE_PARAMS = "org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl";
55  
56      /**
57       * Populates the specified parameters object with properties from the given configuration. This method is used to set
58       * default values for basic properties based on the result configuration of the parent builder.
59       *
60       * @param config the configuration whose properties are to be copied
61       * @param params the target parameters object
62       */
63      private static void setUpBasicParameters(final CombinedConfiguration config, final BasicBuilderParameters params) {
64          params.setListDelimiterHandler(config.getListDelimiterHandler()).setLogger(config.getLogger())
65              .setThrowExceptionOnMissing(config.isThrowExceptionOnMissing()).setConfigurationDecoder(config.getConfigurationDecoder());
66      }
67  
68      /**
69       * Creates a new instance of {@code CombinedConfigurationBuilderProvider}.
70       */
71      public CombinedConfigurationBuilderProvider() {
72          super(BUILDER_CLASS, RELOADING_BUILDER_CLASS, CONFIGURATION_CLASS, Arrays.asList(COMBINED_PARAMS, FILE_PARAMS));
73      }
74  
75      /**
76       * {@inheritDoc} This implementation creates the result builder object directly, not using reflection. (The
77       * reflection-based approach of the base class does not work here because a combined configuration builder has
78       * constructors with a different signature.) It also performs some additional initializations.
79       */
80      @Override
81      protected BasicConfigurationBuilder<? extends Configuration> createBuilder(final ConfigurationDeclaration decl, final Collection<BuilderParameters> params)
82          throws Exception {
83          final CombinedConfigurationBuilder builder;
84          if (decl.isReload()) {
85              builder = new ReloadingCombinedConfigurationBuilder();
86          } else {
87              builder = new CombinedConfigurationBuilder();
88          }
89          decl.getConfigurationBuilder().initChildEventListeners(builder);
90          return builder;
91      }
92  
93      /**
94       * {@inheritDoc} This implementation pre-fills basic parameters from the basic properties of the parent builder's result
95       * configuration.
96       */
97      @Override
98      protected void initializeParameterObjects(final ConfigurationDeclaration decl, final Collection<BuilderParameters> params) throws Exception {
99          // we know that the first object is the combined builder parameters
100         // object
101         final BasicBuilderParameters basicParams = (BasicBuilderParameters) params.iterator().next();
102         setUpBasicParameters(decl.getConfigurationBuilder().getConfigurationUnderConstruction(), basicParams);
103         // now properties set explicitly can be overridden
104         super.initializeParameterObjects(decl, params);
105     }
106 }