    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.configuration2.reloading;
    018
    019import org.apache.commons.configuration2.event.Event;
    020import org.apache.commons.configuration2.event.EventListener;
    021import org.apache.commons.configuration2.event.EventListenerList;
    022import org.apache.commons.configuration2.event.EventSource;
    023import org.apache.commons.configuration2.event.EventType;
    024
    025/**
    026 * <p>
    027 * A class for adding support for reload operations in a generic way.
    028 * </p>
    029 * <p>
    030 * A {@code ReloadingController} monitors a specific source and triggers reloading events if necessary. So it does not
    031 * perform reloading itself, but only sends out notifications when it thinks that this should be done. This allows for a
    032 * very generic setup in which different components involved in reloading are loosely coupled via events.
    033 * </p>
    034 * <p>
    035 * A typical usage scenario is as follows:
    036 * </p>
    037 * <ul>
    038 * <li>A {@code ReloadingController} instance is created and initialized with a {@link ReloadingDetector} object.</li>
    039 * <li>A number of {@link EventListener} objects for reloading events can be registered at the controller.</li>
    040 * <li>Now the controller's {@code checkForReloading()} method is called whenever a check is to be performed. This could
    041 * be done for instance by a timer in regular intervals or by any other means appropriate for a specific
    042 * application.</li>
    043 * <li>When a check reveals that a reload operation is necessary all registered event listeners are notified.</li>
    044 * <li>Typically one of the listeners is responsible to perform the actual reload operation. (How this is done is not in
    045 * the scope of the controller object.) After this has been done, the controller's {@code resetReloadingState()} method
    046 * must be called. It tells the controller that the last notification has been processed and that new checks are
    047 * possible again. It is important that this method is called. Otherwise, {@code checkForReloading()} will not do any
    048 * new checks or send out event notifications any more.</li>
    049 * </ul>
    050 * <p>
    051 * This class can be accessed from multiple threads concurrently. It shields the associated {@link ReloadingDetector}
    052 * object for concurrent access, so that a concrete detector implementation does not have to be thread-safe.
    053 * </p>
    054 *
    055 * @since 2.0
    056 */
    057public class ReloadingController implements EventSource {
    058
    059    /** Stores a reference to the reloading detector. */
    060    private final ReloadingDetector detector;
    061
    062    /** The helper object which manages the registered event listeners. */
    063    private final EventListenerList listeners;
    064
    065    /** A flag whether this controller is in reloading state. */
    066    private boolean reloadingState;
    067
    068    /**
    069     * Creates a new instance of {@code ReloadingController} and associates it with the given {@code ReloadingDetector}
    070     * object.
    071     *
    072     * @param detect the {@code ReloadingDetector} (must not be <strong>null</strong>)
    073     * @throws IllegalArgumentException if the detector is undefined
    074     */
    075    public ReloadingController(final ReloadingDetector detect) {
    076        if (detect == null) {
    077            throw new IllegalArgumentException("ReloadingDetector must not be null!");
    078        }
    079
    080        detector = detect;
    081        listeners = new EventListenerList();
    082    }
    083
    084    /**
    085     * {@inheritDoc} This class generates events of type {@code ReloadingEvent}.
    086     */
    087    @Override
    088    public <T extends Event> void addEventListener(final EventType<T> eventType, final EventListener<? super T> listener) {
    089        listeners.addEventListener(eventType, listener);
    090    }
    091
    092    /**
    093     * Performs a check whether a reload operation is necessary. This method has to be called to trigger the generation of
    094     * reloading events. It delegates to the associated {@link ReloadingDetector} and sends out notifications if necessary.
    095     * The argument can be an arbitrary data object; it will be part of the event notification sent out when a reload
    096     * operation should be performed. The return value indicates whether a change was detected and an event was sent. Once a
    097     * need for a reload is detected, this controller is in <em>reloading state</em>. Until this state is reset (by calling
    098     * {@link #resetReloadingState()}), no further reloading checks are performed by this method, and no events are fired;
    099     * it then returns always <strong>true</strong>.
    100     *
    101     * @param data additional data for an event notification
    102     * @return a flag whether a reload operation is necessary
    103     */
    104    public boolean checkForReloading(final Object data) {
    105        boolean sendEvent = false;
    106        synchronized (this) {
    107            if (isInReloadingState()) {
    108                return true;
    109            }
    110            if (getDetector().isReloadingRequired()) {
    111                sendEvent = true;
    112                reloadingState = true;
    113            }
    114        }
    115
    116        if (sendEvent) {
    117            listeners.fire(new ReloadingEvent(this, data));
    118            return true;
    119        }
    120        return false;
    121    }
    122
    123    /**
    124     * Gets the {@code ReloadingDetector} used by this controller.
    125     *
    126     * @return the {@code ReloadingDetector}
    127     */
    128    public ReloadingDetector getDetector() {
    129        return detector;
    130    }
    131
    132    /**
    133     * Tests whether this controller is in <em>reloading state</em>. A return value of <strong>true</strong> means that a previous
    134     * invocation of {@code checkForReloading()} has detected the necessity for a reload operation, but
    135     * {@code resetReloadingState()} has not been called yet. In this state no further reloading checks are possible.
    136     *
    137     * @return a flag whether this controller is in reloading state
    138     */
    139    public synchronized boolean isInReloadingState() {
    140        return reloadingState;
    141    }
    142
    143    @Override
    144    public <T extends Event> boolean removeEventListener(final EventType<T> eventType, final EventListener<? super T> listener) {
    145        return listeners.removeEventListener(eventType, listener);
    146    }
    147
    148    /**
    149     * Resets the reloading state. This tells the controller that reloading has been performed and new checks are possible
    150     * again. If this controller is not in reloading state, this method has no effect.
    151     */
    152    public synchronized void resetReloadingState() {
    153        if (isInReloadingState()) {
    154            getDetector().reloadingPerformed();
    155            reloadingState = false;
    156        }
    157    }
    158}