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.combined;
018
019import java.util.Collection;
020import java.util.LinkedList;
021import java.util.Map;
022
023import org.apache.commons.configuration2.CombinedConfiguration;
024import org.apache.commons.configuration2.HierarchicalConfiguration;
025import org.apache.commons.configuration2.XMLConfiguration;
026import org.apache.commons.configuration2.builder.BuilderParameters;
027import org.apache.commons.configuration2.builder.ConfigurationBuilder;
028import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
029import org.apache.commons.configuration2.ex.ConfigurationException;
030import org.apache.commons.configuration2.reloading.CombinedReloadingController;
031import org.apache.commons.configuration2.reloading.ReloadingController;
032import org.apache.commons.configuration2.reloading.ReloadingControllerSupport;
033
034/**
035 * <p>
036 * An extension of {@code CombinedConfigurationBuilder} which also supports reloading operations.
037 * </p>
038 * <p>
039 * This class differs from its super class in the following aspects:
040 * </p>
041 * <ul>
042 * <li>A {@link ReloadingController} is created which manages all child configuration builders supporting reloading
043 * operations.</li>
044 * <li>If no {@code ConfigurationBuilder} is provided for the definition configuration, a builder with reloading support
045 * is created.</li>
046 * </ul>
047 * <p>
048 * This class can be used exactly as its super class for creating combined configurations from multiple configuration
049 * sources. In addition, the combined reloading controller managed by an instance can be used to react on changes in one
050 * of these configuration sources or in the definition configuration.
051 * </p>
052 *
053 * @since 2.0
054 */
055public class ReloadingCombinedConfigurationBuilder extends CombinedConfigurationBuilder implements ReloadingControllerSupport {
056    /**
057     * Checks whether the passed in builder object supports reloading. If yes, its reloading controller is obtained and
058     * added to the given list.
059     *
060     * @param subControllers the list with sub controllers
061     * @param builder the builder object to be checked
062     */
063    public static void obtainReloadingController(final Collection<ReloadingController> subControllers, final Object builder) {
064        if (builder instanceof ReloadingControllerSupport) {
065            subControllers.add(((ReloadingControllerSupport) builder).getReloadingController());
066        }
067    }
068
069    /** The reloading controller used by this builder. */
070    private ReloadingController reloadingController;
071
072    /**
073     * Creates a new instance of {@code ReloadingCombinedConfigurationBuilder}. No parameters are set.
074     */
075    public ReloadingCombinedConfigurationBuilder() {
076    }
077
078    /**
079     * Creates a new instance of {@code ReloadingCombinedConfigurationBuilder} and sets the specified initialization
080     * parameters.
081     *
082     * @param params a map with initialization parameters
083     */
084    public ReloadingCombinedConfigurationBuilder(final Map<String, Object> params) {
085        super(params);
086    }
087
088    /**
089     * Creates a new instance of {@code ReloadingCombinedConfigurationBuilder} and sets the specified initialization
090     * parameters and the <em>allowFailOnInit</em> flag.
091     *
092     * @param params a map with initialization parameters
093     * @param allowFailOnInit the <em>allowFailOnInit</em> flag
094     */
095    public ReloadingCombinedConfigurationBuilder(final Map<String, Object> params, final boolean allowFailOnInit) {
096        super(params, allowFailOnInit);
097    }
098
099    /**
100     * {@inheritDoc} This method is overridden to adapt the return type.
101     */
102    @Override
103    public ReloadingCombinedConfigurationBuilder configure(final BuilderParameters... params) {
104        super.configure(params);
105        return this;
106    }
107
108    /**
109     * Creates the {@code ReloadingController} for this builder. This method is called after the result configuration has
110     * been created and initialized. It is called from a synchronized block. This implementation creates a
111     * {@link CombinedReloadingController}.
112     *
113     * @return the {@code ReloadingController} for this builder
114     * @throws ConfigurationException if an error occurs
115     */
116    protected ReloadingController createReloadingController() throws ConfigurationException {
117        final Collection<ReloadingController> subControllers = new LinkedList<>();
118        final ConfigurationBuilder<? extends HierarchicalConfiguration<?>> defBuilder = getDefinitionBuilder();
119        obtainReloadingController(subControllers, defBuilder);
120
121        getChildBuilders().forEach(b -> obtainReloadingController(subControllers, b));
122
123        final CombinedReloadingController ctrl = new CombinedReloadingController(subControllers);
124        ctrl.resetInitialReloadingState();
125        return ctrl;
126    }
127
128    /**
129     * {@inheritDoc} This implementation creates a builder for XML configurations with reloading support.
130     */
131    @Override
132    protected ConfigurationBuilder<? extends HierarchicalConfiguration<?>> createXMLDefinitionBuilder(final BuilderParameters builderParams) {
133        return new ReloadingFileBasedConfigurationBuilder<>(XMLConfiguration.class).configure(builderParams);
134    }
135
136    /**
137     * {@inheritDoc} This implementation makes sure that the reloading state of the managed reloading controller is reset.
138     * Note that this has to be done here and not in {@link #initResultInstance(CombinedConfiguration)} because it must be
139     * outside of a synchronized block; otherwise, a dead-lock situation can occur.
140     */
141    @Override
142    public CombinedConfiguration getConfiguration() throws ConfigurationException {
143        final CombinedConfiguration result = super.getConfiguration();
144        reloadingController.resetReloadingState();
145        return result;
146    }
147
148    /**
149     * {@inheritDoc} This implementation returns a {@link CombinedReloadingController} which contains sub controllers for
150     * all child configuration sources with reloading support. If the definition builder supports reloading, its controller
151     * is contained, too. Note that the combined reloading controller is initialized when the result configuration is
152     * created (i.e. when calling {@code getConfiguration()} for the first time). So this method does not return a
153     * meaningful result before.
154     */
155    @Override
156    public synchronized ReloadingController getReloadingController() {
157        return reloadingController;
158    }
159
160    /**
161     * {@inheritDoc} This implementation first calls the super method to actually initialize the result configuration. Then
162     * it creates the {@link CombinedReloadingController} for all child configuration sources with reloading support.
163     */
164    @Override
165    protected void initResultInstance(final CombinedConfiguration result) throws ConfigurationException {
166        super.initResultInstance(result);
167        if (reloadingController == null) {
168            reloadingController = createReloadingController();
169        }
170    }
171}