ReloadingBuilderSupportListener.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;

  18. import org.apache.commons.configuration2.event.Event;
  19. import org.apache.commons.configuration2.event.EventListener;
  20. import org.apache.commons.configuration2.reloading.ReloadingController;
  21. import org.apache.commons.configuration2.reloading.ReloadingEvent;

  22. /**
  23.  * <p>
  24.  * An internally used helper class for adding reloading support to an arbitrary {@link ConfigurationBuilder}.
  25.  * </p>
  26.  * <p>
  27.  * This class connects a configuration builder with a {@link ReloadingController}. This is done in the following way:
  28.  * <ul>
  29.  * <li>An instance is registered as listener at a {@code ReloadingController}. Whenever the controller indicates that a
  30.  * reload should happen, the associated configuration builder's {@link BasicConfigurationBuilder#resetResult()} method
  31.  * is called.</li>
  32.  * <li>When the builder fires a {@link ConfigurationBuilderResultCreatedEvent} event the reloading controller's
  33.  * reloading state is reset. At that time the reload has actually happened, and the controller is prepared to observe
  34.  * new changes.</li>
  35.  * </ul>
  36.  * </p>
  37.  *
  38.  * @since 2.0
  39.  */
  40. final class ReloadingBuilderSupportListener implements EventListener<Event> {
  41.     /**
  42.      * Creates a new instance of {@code ReloadingBuilderSupportListener} which connects the specified
  43.      * {@code ConfigurationBuilder} with the given {@code ReloadingController}. Listeners are registered to react on
  44.      * notifications and implement a reloading protocol as described in the class comment.
  45.      *
  46.      * @param configBuilder the {@code ConfigurationBuilder}
  47.      * @param controller the {@code ReloadingController}
  48.      * @return the newly created listener object
  49.      */
  50.     public static ReloadingBuilderSupportListener connect(final BasicConfigurationBuilder<?> configBuilder, final ReloadingController controller) {
  51.         final ReloadingBuilderSupportListener listener = new ReloadingBuilderSupportListener(configBuilder, controller);
  52.         controller.addEventListener(ReloadingEvent.ANY, listener);
  53.         configBuilder.installEventListener(ConfigurationBuilderResultCreatedEvent.RESULT_CREATED, listener);
  54.         return listener;
  55.     }

  56.     /** Stores the associated configuration builder. */
  57.     private final BasicConfigurationBuilder<?> builder;

  58.     /** Stores the associated reloading controller. */
  59.     private final ReloadingController reloadingController;

  60.     /**
  61.      * Creates a new instance of {@code ReloadingBuilderSupportListener} and initializes it with the associated objects.
  62.      *
  63.      * @param configBuilder the configuration builder
  64.      * @param controller the {@code ReloadingController}
  65.      */
  66.     private ReloadingBuilderSupportListener(final BasicConfigurationBuilder<?> configBuilder, final ReloadingController controller) {
  67.         builder = configBuilder;
  68.         reloadingController = controller;
  69.     }

  70.     /**
  71.      * {@inheritDoc} This implementation resets the controller's reloading state if an event about a newly created result
  72.      * was received. Otherwise, in case of a reloading event, the builder's result object is reset.
  73.      */
  74.     @Override
  75.     public void onEvent(final Event event) {
  76.         if (ConfigurationBuilderResultCreatedEvent.RESULT_CREATED.equals(event.getEventType())) {
  77.             reloadingController.resetReloadingState();
  78.         } else {
  79.             builder.resetResult();
  80.         }
  81.     }
  82. }