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 19 import org.apache.commons.configuration2.event.Event; 20 import org.apache.commons.configuration2.event.EventListener; 21 import org.apache.commons.configuration2.reloading.ReloadingController; 22 import org.apache.commons.configuration2.reloading.ReloadingEvent; 23 24 /** 25 * <p> 26 * An internally used helper class for adding reloading support to an arbitrary {@link ConfigurationBuilder}. 27 * </p> 28 * <p> 29 * This class connects a configuration builder with a {@link ReloadingController}. This is done in the following way: 30 * <ul> 31 * <li>An instance is registered as listener at a {@code ReloadingController}. Whenever the controller indicates that a 32 * reload should happen, the associated configuration builder's {@link BasicConfigurationBuilder#resetResult()} method 33 * is called.</li> 34 * <li>When the builder fires a {@link ConfigurationBuilderResultCreatedEvent} event the reloading controller's 35 * reloading state is reset. At that time the reload has actually happened, and the controller is prepared to observe 36 * new changes.</li> 37 * </ul> 38 * </p> 39 * 40 * @since 2.0 41 */ 42 final class ReloadingBuilderSupportListener implements EventListener<Event> { 43 /** Stores the associated configuration builder. */ 44 private final BasicConfigurationBuilder<?> builder; 45 46 /** Stores the associated reloading controller. */ 47 private final ReloadingController reloadingController; 48 49 /** 50 * Creates a new instance of {@code ReloadingBuilderSupportListener} and initializes it with the associated objects. 51 * 52 * @param configBuilder the configuration builder 53 * @param controller the {@code ReloadingController} 54 */ 55 private ReloadingBuilderSupportListener(final BasicConfigurationBuilder<?> configBuilder, final ReloadingController controller) { 56 builder = configBuilder; 57 reloadingController = controller; 58 } 59 60 /** 61 * Creates a new instance of {@code ReloadingBuilderSupportListener} which connects the specified 62 * {@code ConfigurationBuilder} with the given {@code ReloadingController}. Listeners are registered to react on 63 * notifications and implement a reloading protocol as described in the class comment. 64 * 65 * @param configBuilder the {@code ConfigurationBuilder} 66 * @param controller the {@code ReloadingController} 67 * @return the newly created listener object 68 */ 69 public static ReloadingBuilderSupportListener connect(final BasicConfigurationBuilder<?> configBuilder, final ReloadingController controller) { 70 final ReloadingBuilderSupportListener listener = new ReloadingBuilderSupportListener(configBuilder, controller); 71 controller.addEventListener(ReloadingEvent.ANY, listener); 72 configBuilder.installEventListener(ConfigurationBuilderResultCreatedEvent.RESULT_CREATED, listener); 73 return listener; 74 } 75 76 /** 77 * {@inheritDoc} This implementation resets the controller's reloading state if an event about a newly created result 78 * was received. Otherwise, in case of a reloading event, the builder's result object is reset. 79 */ 80 @Override 81 public void onEvent(final Event event) { 82 if (ConfigurationBuilderResultCreatedEvent.RESULT_CREATED.equals(event.getEventType())) { 83 reloadingController.resetReloadingState(); 84 } else { 85 builder.resetResult(); 86 } 87 } 88 }