CombinedReloadingController.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.reloading;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.Objects;

  22. /**
  23.  * <p>
  24.  * A specialized {@code ReloadingController} implementation which manages an arbitrary number of other
  25.  * {@code ReloadingController} objects.
  26.  * </p>
  27.  * <p>
  28.  * This class can be used to handle multiple simple controllers for reload operations as a single object. As a usage
  29.  * example consider a combined configuration containing a number of configuration sources of which some support
  30.  * reloading. In this scenario all {@code ReloadingController} instances for the reloading-enabled sources can be added
  31.  * to a {@code CombinedReloadingController}. Then by triggering the combined controller a reload check is performed on
  32.  * all child sources.
  33.  * </p>
  34.  * <p>
  35.  * This class is a typical implementation of the <em>composite pattern</em>. An instance is constructed with a
  36.  * collection of sub {@code ReloadingController} objects. Its operations are implemented by delegating to all child
  37.  * controllers.
  38.  * </p>
  39.  * <p>
  40.  * This class expects the managed controller objects to be passed to the constructor. From this list a defensive copy is
  41.  * created so that it cannot be changed later on. Derived classes can override the {@link #getSubControllers()} method
  42.  * if they need another way to handle child controllers (for example a more dynamic way). However, they are then responsible to
  43.  * ensure a safe access to this list in a multi-threaded environment.
  44.  * </p>
  45.  *
  46.  * @since 2.0
  47.  */
  48. public class CombinedReloadingController extends ReloadingController {
  49.     /**
  50.      * A specialized implementation of the {@code ReloadingDetector} interface which operates on a collection of
  51.      * {@code ReloadingController} objects. The methods defined by the {@code ReloadingDetector} interface are delegated to
  52.      * the managed controllers.
  53.      */
  54.     private static final class MultiReloadingControllerDetector implements ReloadingDetector {
  55.         /** A reference to the owning combined reloading controller. */
  56.         private final CombinedReloadingController owner;

  57.         /**
  58.          * Creates a new instance of {@code MultiReloadingControllerDetector}.
  59.          *
  60.          * @param owner the owner
  61.          */
  62.         public MultiReloadingControllerDetector(final CombinedReloadingController owner) {
  63.             this.owner = owner;
  64.         }

  65.         /**
  66.          * {@inheritDoc} This implementation delegates to the managed controllers. For all of them the
  67.          * {@code checkForReloading()} method is called, giving them the chance to trigger a reload if necessary. If one of
  68.          * these calls returns <strong>true</strong>, the result of this method is <strong>true</strong>, otherwise <strong>false</strong>.
  69.          */
  70.         @Override
  71.         public boolean isReloadingRequired() {
  72.             return owner.getSubControllers().stream().reduce(false, (b, rc) -> b | rc.checkForReloading(null), (t, u) -> t | u);
  73.         }

  74.         /**
  75.          * {@inheritDoc} This implementation resets the reloading state on all managed controllers.
  76.          */
  77.         @Override
  78.         public void reloadingPerformed() {
  79.             owner.getSubControllers().forEach(ReloadingController::resetReloadingState);
  80.         }
  81.     }

  82.     /** Constant for a dummy reloading detector. */
  83.     private static final ReloadingDetector DUMMY = new MultiReloadingControllerDetector(null);

  84.     /**
  85.      * Checks the collection with the passed in sub controllers and creates a defensive copy.
  86.      *
  87.      * @param subCtrls the collection with sub controllers
  88.      * @return a copy of the collection to be stored in the newly created instance
  89.      * @throws IllegalArgumentException if the passed in collection is <strong>null</strong> or contains <strong>null</strong> entries
  90.      */
  91.     private static Collection<ReloadingController> checkManagedControllers(final Collection<? extends ReloadingController> subCtrls) {
  92.         if (subCtrls == null) {
  93.             throw new IllegalArgumentException("Collection with sub controllers must not be null!");
  94.         }
  95.         final Collection<ReloadingController> ctrls = new ArrayList<>(subCtrls);
  96.         if (ctrls.stream().anyMatch(Objects::isNull)) {
  97.             throw new IllegalArgumentException("Collection with sub controllers contains a null entry!");
  98.         }

  99.         return Collections.unmodifiableCollection(ctrls);
  100.     }

  101.     /** The collection with managed reloading controllers. */
  102.     private final Collection<ReloadingController> controllers;

  103.     /** The reloading detector used by this instance. */
  104.     private final ReloadingDetector detector;

  105.     /**
  106.      * Creates a new instance of {@code CombinedReloadingController} and initializes it with the {@code ReloadingController}
  107.      * objects to be managed.
  108.      *
  109.      * @param subCtrls the collection with sub {@code ReloadingController}s (must not be <strong>null</strong> or contain <strong>null</strong>
  110.      *        entries)
  111.      * @throws IllegalArgumentException if the passed in collection is <strong>null</strong> or contains <strong>null</strong> entries
  112.      */
  113.     public CombinedReloadingController(final Collection<? extends ReloadingController> subCtrls) {
  114.         super(DUMMY);
  115.         controllers = checkManagedControllers(subCtrls);
  116.         detector = new MultiReloadingControllerDetector(this);
  117.     }

  118.     /**
  119.      * {@inheritDoc} This implementation returns a special reloading detector which operates on all managed controllers.
  120.      */
  121.     @Override
  122.     public ReloadingDetector getDetector() {
  123.         return detector;
  124.     }

  125.     /**
  126.      * Gets a (unmodifiable) collection with the sub controllers managed by this combined controller.
  127.      *
  128.      * @return a collection with sub controllers
  129.      */
  130.     public Collection<ReloadingController> getSubControllers() {
  131.         return controllers;
  132.     }

  133.     /**
  134.      * Resets the reloading state of all managed sub controllers unconditionally. This method is intended to be called after
  135.      * the creation of an instance. It may be the case that some of the sub controllers are already in reloading state, so
  136.      * their state is out of sync with this controller's global reloading state. This method ensures that the reloading
  137.      * state of all sub controllers is reset.
  138.      */
  139.     public void resetInitialReloadingState() {
  140.         getDetector().reloadingPerformed();
  141.     }
  142. }