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 * http://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 031 * reloading events if necessary. So it does not perform reloading itself, but 032 * only sends out notifications when it thinks that this should be done. This 033 * allows for a very generic setup in which different components involved in 034 * reloading are loosely coupled via events. 035 * </p> 036 * <p> 037 * A typical usage scenario is as follows: 038 * </p> 039 * <ul> 040 * <li>A {@code ReloadingController} instance is created and initialized with a 041 * {@link ReloadingDetector} object.</li> 042 * <li>A number of {@link EventListener} objects for reloading events can be 043 * registered at the controller.</li> 044 * <li>Now the controller's {@code checkForReloading()} method is called 045 * whenever a check is to be performed. This could be done for instance by a 046 * timer in regular intervals or by any other means appropriate for a specific 047 * application.</li> 048 * <li>When a check reveals that a reload operation is necessary all registered 049 * event listeners are notified.</li> 050 * <li>Typically one of the listeners is responsible to perform the actual 051 * reload operation. (How this is done is not in the scope of the controller 052 * object.) After this has been done, the controller's 053 * {@code resetReloadingState()} method must be called. It tells the controller 054 * that the last notification has been processed and that new checks are 055 * possible again. It is important that this method is called. Otherwise, 056 * {@code checkForReloading()} will not do any new checks or send out event 057 * notifications any more.</li> 058 * </ul> 059 * <p> 060 * This class can be accessed from multiple threads concurrently. It shields the 061 * associated {@link ReloadingDetector} object for concurrent access, so that a 062 * concrete detector implementation does not have to be thread-safe. 063 * </p> 064 * 065 * @since 2.0 066 */ 067public class ReloadingController implements EventSource 068{ 069 /** Stores a reference to the reloading detector. */ 070 private final ReloadingDetector detector; 071 072 /** The helper object which manages the registered event listeners. */ 073 private final EventListenerList listeners; 074 075 /** A flag whether this controller is in reloading state. */ 076 private boolean reloadingState; 077 078 /** 079 * Creates a new instance of {@code ReloadingController} and associates it 080 * with the given {@code ReloadingDetector} object. 081 * 082 * @param detect the {@code ReloadingDetector} (must not be <b>null</b>) 083 * @throws IllegalArgumentException if the detector is undefined 084 */ 085 public ReloadingController(final ReloadingDetector detect) 086 { 087 if (detect == null) 088 { 089 throw new IllegalArgumentException( 090 "ReloadingDetector must not be null!"); 091 } 092 093 detector = detect; 094 listeners = new EventListenerList(); 095 } 096 097 /** 098 * Returns the {@code ReloadingDetector} used by this controller. 099 * 100 * @return the {@code ReloadingDetector} 101 */ 102 public ReloadingDetector getDetector() 103 { 104 return detector; 105 } 106 107 /** 108 * {@inheritDoc} This class generates events of type {@code ReloadingEvent}. 109 */ 110 @Override 111 public <T extends Event> void addEventListener( 112 final EventType<T> eventType, final EventListener<? super T> listener) 113 { 114 listeners.addEventListener(eventType, listener); 115 } 116 117 @Override 118 public <T extends Event> boolean removeEventListener( 119 final EventType<T> eventType, final EventListener<? super T> listener) 120 { 121 return listeners.removeEventListener(eventType, listener); 122 } 123 124 /** 125 * Tests whether this controller is in <em>reloading state</em>. A return 126 * value of <b>true</b> means that a previous invocation of 127 * {@code checkForReloading()} has detected the necessity for a reload 128 * operation, but {@code resetReloadingState()} has not been called yet. In 129 * this state no further reloading checks are possible. 130 * 131 * @return a flag whether this controller is in reloading state 132 */ 133 public synchronized boolean isInReloadingState() 134 { 135 return reloadingState; 136 } 137 138 /** 139 * Performs a check whether a reload operation is necessary. This method has 140 * to be called to trigger the generation of reloading events. It delegates 141 * to the associated {@link ReloadingDetector} and sends out notifications 142 * if necessary. The argument can be an arbitrary data object; it will be 143 * part of the event notification sent out when a reload operation should be 144 * performed. The return value indicates whether a change was detected and 145 * an event was sent. Once a need for a reload is detected, this controller 146 * is in <em>reloading state</em>. Until this state is reset (by calling 147 * {@link #resetReloadingState()}), no further reloading checks are 148 * performed by this method, and no events are fired; it then returns always 149 * <b>true</b>. 150 * 151 * @param data additional data for an event notification 152 * @return a flag whether a reload operation is necessary 153 */ 154 public boolean checkForReloading(final Object data) 155 { 156 boolean sendEvent = false; 157 synchronized (this) 158 { 159 if (isInReloadingState()) 160 { 161 return true; 162 } 163 if (getDetector().isReloadingRequired()) 164 { 165 sendEvent = true; 166 reloadingState = true; 167 } 168 } 169 170 if (sendEvent) 171 { 172 listeners.fire(new ReloadingEvent(this, data)); 173 return true; 174 } 175 return false; 176 } 177 178 /** 179 * Resets the reloading state. This tells the controller that reloading has 180 * been performed and new checks are possible again. If this controller is 181 * not in reloading state, this method has no effect. 182 */ 183 public synchronized void resetReloadingState() 184 { 185 if (isInReloadingState()) 186 { 187 getDetector().reloadingPerformed(); 188 reloadingState = false; 189 } 190 } 191}