    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.builder;
    018
    019import java.util.Map;
    020
    021import org.apache.commons.configuration2.FileBasedConfiguration;
    022import org.apache.commons.configuration2.ex.ConfigurationException;
    023import org.apache.commons.configuration2.io.FileHandler;
    024import org.apache.commons.configuration2.reloading.ReloadingController;
    025import org.apache.commons.configuration2.reloading.ReloadingControllerSupport;
    026import org.apache.commons.configuration2.reloading.ReloadingDetector;
    027
    028/**
    029 * <p>
    030 * A specialized {@code ConfigurationBuilder} implementation which can handle configurations read from a
    031 * {@link FileHandler} and supports reloading.
    032 * </p>
    033 * <p>
    034 * This builder class exposes a {@link ReloadingController} object controlling reload operations on the file-based
    035 * configuration produced as result object. For the {@code FileHandler} defining the location of the configuration a
    036 * configurable {@link ReloadingDetector} is created and associated with the controller. So changes on the source file
    037 * can be detected. When ever such a change occurs, the result object of this builder is reset. This means that the next
    038 * time {@code getConfiguration()} is called a new {@code Configuration} object is created which is loaded from the
    039 * modified file.
    040 * </p>
    041 * <p>
    042 * Client code interested in notifications can register a listener at this builder to receive reset events. When such an
    043 * event is received the new result object can be requested. This way client applications can be sure to work with an
    044 * up-to-date configuration. It is also possible to register a listener directly at the {@code ReloadingController}.
    045 * </p>
    046 * <p>
    047 * This builder does not actively trigger the {@code ReloadingController} to perform a reload check. This has to be done
    048 * by an external component, e.g. a timer.
    049 * </p>
    050 *
    051 * @since 2.0
    052 * @param <T> the concrete type of {@code Configuration} objects created by this builder
    053 */
    054public class ReloadingFileBasedConfigurationBuilder<T extends FileBasedConfiguration> extends FileBasedConfigurationBuilder<T>
    055    implements ReloadingControllerSupport {
    056    /** The default factory for creating reloading detector objects. */
    057    private static final ReloadingDetectorFactory DEFAULT_DETECTOR_FACTORY = new DefaultReloadingDetectorFactory();
    058
    059    /** The reloading controller associated with this object. */
    060    private final ReloadingController reloadingController;
    061
    062    /**
    063     * The reloading detector which does the actual reload check for the current result object. A new instance is created
    064     * whenever a new result object (and thus a new current file handler) becomes available. The field must be volatile
    065     * because it is accessed by the reloading controller probably from within another thread.
    066     */
    067    private volatile ReloadingDetector resultReloadingDetector;
    068
    069    /**
    070     * Creates a new instance of {@code ReloadingFileBasedConfigurationBuilder} which produces result objects of the
    071     * specified class and sets initialization parameters.
    072     *
    073     * @param resCls the result class (must not be <b>null</b>
    074     * @param params a map with initialization parameters
    075     * @throws IllegalArgumentException if the result class is <b>null</b>
    076     */
    077    public ReloadingFileBasedConfigurationBuilder(final Class<? extends T> resCls, final Map<String, Object> params) {
    078        super(resCls, params);
    079        reloadingController = createReloadingController();
    080    }
    081
    082    /**
    083     * Creates a new instance of {@code ReloadingFileBasedConfigurationBuilder} which produces result objects of the
    084     * specified class and sets initialization parameters and the <em>allowFailOnInit</em> flag.
    085     *
    086     * @param resCls the result class (must not be <b>null</b>
    087     * @param params a map with initialization parameters
    088     * @param allowFailOnInit the <em>allowFailOnInit</em> flag
    089     * @throws IllegalArgumentException if the result class is <b>null</b>
    090     */
    091    public ReloadingFileBasedConfigurationBuilder(final Class<? extends T> resCls, final Map<String, Object> params, final boolean allowFailOnInit) {
    092        super(resCls, params, allowFailOnInit);
    093        reloadingController = createReloadingController();
    094    }
    095
    096    /**
    097     * Creates a new instance of {@code ReloadingFileBasedConfigurationBuilder} which produces result objects of the
    098     * specified class.
    099     *
    100     * @param resCls the result class (must not be <b>null</b>
    101     * @throws IllegalArgumentException if the result class is <b>null</b>
    102     */
    103    public ReloadingFileBasedConfigurationBuilder(final Class<? extends T> resCls) {
    104        super(resCls);
    105        reloadingController = createReloadingController();
    106    }
    107
    108    /**
    109     * Gets the {@code ReloadingController} associated with this builder. This controller is directly created. However,
    110     * it becomes active (i.e. associated with a meaningful reloading detector) not before a result object was created.
    111     *
    112     * @return the {@code ReloadingController}
    113     */
    114    @Override
    115    public ReloadingController getReloadingController() {
    116        return reloadingController;
    117    }
    118
    119    /**
    120     * {@inheritDoc} This method is overridden here to change the result type.
    121     */
    122    @Override
    123    public ReloadingFileBasedConfigurationBuilder<T> configure(final BuilderParameters... params) {
    124        super.configure(params);
    125        return this;
    126    }
    127
    128    /**
    129     * Creates a {@code ReloadingDetector} which monitors the passed in {@code FileHandler}. This method is called each time
    130     * a new result object is created with the current {@code FileHandler}. This implementation checks whether a
    131     * {@code ReloadingDetectorFactory} is specified in the current parameters. If this is the case, it is invoked.
    132     * Otherwise, a default factory is used to create a {@code FileHandlerReloadingDetector} object. Note: This method is
    133     * called from a synchronized block.
    134     *
    135     * @param handler the current {@code FileHandler}
    136     * @param fbparams the object with parameters related to file-based builders
    137     * @return a {@code ReloadingDetector} for this {@code FileHandler}
    138     * @throws ConfigurationException if an error occurs
    139     */
    140    protected ReloadingDetector createReloadingDetector(final FileHandler handler, final FileBasedBuilderParametersImpl fbparams)
    141        throws ConfigurationException {
    142        return fetchDetectorFactory(fbparams).createReloadingDetector(handler, fbparams);
    143    }
    144
    145    /**
    146     * {@inheritDoc} This implementation also takes care that a new {@code ReloadingDetector} for the new current
    147     * {@code FileHandler} is created. Also, the reloading controller's reloading state has to be reset; after the creation
    148     * of a new result object changes in the underlying configuration source have to be monitored again.
    149     */
    150    @Override
    151    protected void initFileHandler(final FileHandler handler) throws ConfigurationException {
    152        super.initFileHandler(handler);
    153
    154        resultReloadingDetector = createReloadingDetector(handler, FileBasedBuilderParametersImpl.fromParameters(getParameters(), true));
    155    }
    156
    157    /**
    158     * Creates the {@code ReloadingController} associated with this object. The controller is assigned a specialized
    159     * reloading detector which delegates to the detector for the current result object. (
    160     * {@code FileHandlerReloadingDetector} does not support changing the file handler, and {@code ReloadingController} does
    161     * not support changing the reloading detector; therefore, this level of indirection is needed to change the monitored
    162     * file dynamically.)
    163     *
    164     * @return the new {@code ReloadingController}
    165     */
    166    private ReloadingController createReloadingController() {
    167        final ReloadingDetector ctrlDetector = createReloadingDetectorForController();
    168        final ReloadingController ctrl = new ReloadingController(ctrlDetector);
    169        connectToReloadingController(ctrl);
    170        return ctrl;
    171    }
    172
    173    /**
    174     * Creates a {@code ReloadingDetector} wrapper to be passed to the associated {@code ReloadingController}. This detector
    175     * wrapper simply delegates to the current {@code ReloadingDetector} if it is available.
    176     *
    177     * @return the wrapper {@code ReloadingDetector}
    178     */
    179    private ReloadingDetector createReloadingDetectorForController() {
    180        return new ReloadingDetector() {
    181            @Override
    182            public void reloadingPerformed() {
    183                final ReloadingDetector detector = resultReloadingDetector;
    184                if (detector != null) {
    185                    detector.reloadingPerformed();
    186                }
    187            }
    188
    189            @Override
    190            public boolean isReloadingRequired() {
    191                final ReloadingDetector detector = resultReloadingDetector;
    192                return detector != null && detector.isReloadingRequired();
    193            }
    194        };
    195    }
    196
    197    /**
    198     * Returns a {@code ReloadingDetectorFactory} either from the passed in parameters or a default factory.
    199     *
    200     * @param params the current parameters object
    201     * @return the {@code ReloadingDetectorFactory} to be used
    202     */
    203    private static ReloadingDetectorFactory fetchDetectorFactory(final FileBasedBuilderParametersImpl params) {
    204        final ReloadingDetectorFactory factory = params.getReloadingDetectorFactory();
    205        return factory != null ? factory : DEFAULT_DETECTOR_FACTORY;
    206    }
    207}