ReloadingMultiFileConfigurationBuilder.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.Collection;
  19. import java.util.Collections;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import java.util.stream.Collectors;

  23. import org.apache.commons.configuration2.FileBasedConfiguration;
  24. import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
  25. import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
  26. import org.apache.commons.configuration2.ex.ConfigurationException;
  27. import org.apache.commons.configuration2.reloading.CombinedReloadingController;
  28. import org.apache.commons.configuration2.reloading.ReloadingController;
  29. import org.apache.commons.configuration2.reloading.ReloadingControllerSupport;

  30. /**
  31.  * <p>
  32.  * A specialized {@code MultiFileConfigurationBuilder} implementation which adds support for reloading.
  33.  * </p>
  34.  * <p>
  35.  * This class - as its super class - allows operating on multiple configuration files whose file names are determined
  36.  * using a file name pattern and a {@code ConfigurationInterpolator} object. It provides the following additional
  37.  * features:
  38.  * </p>
  39.  * <ul>
  40.  * <li>Configuration builder for managed configurations have reloading support. So reloading is possible for all
  41.  * configuration sources loaded by this builder instance.</li>
  42.  * <li>A {@link ReloadingController} is provided which can be used to trigger reload checks on all managed
  43.  * configurations.</li>
  44.  * </ul>
  45.  * <p>
  46.  * Although this builder manages an arbitrary number of child configurations, to clients only a single configuration is
  47.  * visible - the one selected by the evaluation of the file name pattern. Builder reset notifications triggered by the
  48.  * reloading mechanism do not really take this fact into account; they are not limited to the currently selected child
  49.  * configuration, but occur for each of the managed configuration.
  50.  * </p>
  51.  *
  52.  * @param <T> the concrete type of {@code Configuration} objects created by this builder
  53.  * @since 2.0
  54.  */
  55. public class ReloadingMultiFileConfigurationBuilder<T extends FileBasedConfiguration> extends MultiFileConfigurationBuilder<T>
  56.     implements ReloadingControllerSupport {
  57.     /** The reloading controller used by this builder. */
  58.     private final ReloadingController reloadingController = createReloadingController();

  59.     /**
  60.      * Creates a new instance of {@code ReloadingMultiFileConfigurationBuilder} without setting initialization parameters.
  61.      *
  62.      * @param resCls the result configuration class
  63.      * @throws IllegalArgumentException if the result class is <strong>null</strong>
  64.      */
  65.     public ReloadingMultiFileConfigurationBuilder(final Class<T> resCls) {
  66.         super(resCls);
  67.     }

  68.     /**
  69.      * Creates a new instance of {@code ReloadingMultiFileConfigurationBuilder} and sets initialization parameters.
  70.      *
  71.      * @param resCls the result configuration class
  72.      * @param params a map with initialization parameters
  73.      * @throws IllegalArgumentException if the result class is <strong>null</strong>
  74.      */
  75.     public ReloadingMultiFileConfigurationBuilder(final Class<T> resCls, final Map<String, Object> params) {
  76.         super(resCls, params);
  77.     }

  78.     /**
  79.      * Creates a new instance of {@code ReloadingMultiFileConfigurationBuilder} and sets initialization parameters and a
  80.      * flag whether initialization failures should be ignored.
  81.      *
  82.      * @param resCls the result configuration class
  83.      * @param params a map with initialization parameters
  84.      * @param allowFailOnInit a flag whether initialization errors should be ignored
  85.      * @throws IllegalArgumentException if the result class is <strong>null</strong>
  86.      */
  87.     public ReloadingMultiFileConfigurationBuilder(final Class<T> resCls, final Map<String, Object> params, final boolean allowFailOnInit) {
  88.         super(resCls, params, allowFailOnInit);
  89.     }

  90.     /**
  91.      * {@inheritDoc} This implementation returns a file-based configuration builder with reloading support.
  92.      */
  93.     @Override
  94.     protected FileBasedConfigurationBuilder<T> createManagedBuilder(final String fileName, final Map<String, Object> params) throws ConfigurationException {
  95.         return new ReloadingFileBasedConfigurationBuilder<>(getResultClass(), params, isAllowFailOnInit());
  96.     }

  97.     /**
  98.      * Creates the reloading controller used by this builder. This method creates a specialized
  99.      * {@link CombinedReloadingController} which operates on the reloading controllers of the managed builders created so
  100.      * far.
  101.      *
  102.      * @return the newly created {@code ReloadingController}
  103.      */
  104.     private ReloadingController createReloadingController() {
  105.         final Set<ReloadingController> empty = Collections.emptySet();
  106.         return new CombinedReloadingController(empty) {
  107.             @Override
  108.             public Collection<ReloadingController> getSubControllers() {
  109.                 return getManagedBuilders().values().stream().map(b -> ((ReloadingControllerSupport) b).getReloadingController()).collect(Collectors.toList());
  110.             }
  111.         };
  112.     }

  113.     /**
  114.      * {@inheritDoc} This implementation returns a special {@code ReloadingController} that delegates to the reloading
  115.      * controllers of the managed builders created so far.
  116.      */
  117.     @Override
  118.     public ReloadingController getReloadingController() {
  119.         return reloadingController;
  120.     }
  121. }