View Javadoc
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    *     https://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 java.util.Map;
20  
21  import org.apache.commons.configuration2.FileBasedConfiguration;
22  import org.apache.commons.configuration2.ex.ConfigurationException;
23  import org.apache.commons.configuration2.io.FileHandler;
24  import org.apache.commons.configuration2.reloading.ReloadingController;
25  import org.apache.commons.configuration2.reloading.ReloadingControllerSupport;
26  import org.apache.commons.configuration2.reloading.ReloadingDetector;
27  
28  /**
29   * <p>
30   * A specialized {@code ConfigurationBuilder} implementation which can handle configurations read from a
31   * {@link FileHandler} and supports reloading.
32   * </p>
33   * <p>
34   * This builder class exposes a {@link ReloadingController} object controlling reload operations on the file-based
35   * configuration produced as result object. For the {@code FileHandler} defining the location of the configuration a
36   * configurable {@link ReloadingDetector} is created and associated with the controller. So changes on the source file
37   * can be detected. When ever such a change occurs, the result object of this builder is reset. This means that the next
38   * time {@code getConfiguration()} is called a new {@code Configuration} object is created which is loaded from the
39   * modified file.
40   * </p>
41   * <p>
42   * Client code interested in notifications can register a listener at this builder to receive reset events. When such an
43   * event is received the new result object can be requested. This way client applications can be sure to work with an
44   * up-to-date configuration. It is also possible to register a listener directly at the {@code ReloadingController}.
45   * </p>
46   * <p>
47   * This builder does not actively trigger the {@code ReloadingController} to perform a reload check. This has to be done
48   * by an external component, for example a timer.
49   * </p>
50   *
51   * @param <T> the concrete type of {@code Configuration} objects created by this builder
52   * @since 2.0
53   */
54  public class ReloadingFileBasedConfigurationBuilder<T extends FileBasedConfiguration> extends FileBasedConfigurationBuilder<T>
55      implements ReloadingControllerSupport {
56  
57      /** The default factory for creating reloading detector objects. */
58      private static final ReloadingDetectorFactory DEFAULT_DETECTOR_FACTORY = new DefaultReloadingDetectorFactory();
59  
60      /**
61       * Returns a {@code ReloadingDetectorFactory} either from the passed in parameters or a default factory.
62       *
63       * @param params the current parameters object
64       * @return the {@code ReloadingDetectorFactory} to be used
65       */
66      private static ReloadingDetectorFactory fetchDetectorFactory(final FileBasedBuilderParametersImpl params) {
67          final ReloadingDetectorFactory factory = params.getReloadingDetectorFactory();
68          return factory != null ? factory : DEFAULT_DETECTOR_FACTORY;
69      }
70  
71      /** The reloading controller associated with this object. */
72      private final ReloadingController reloadingController;
73  
74      /**
75       * The reloading detector which does the actual reload check for the current result object. A new instance is created
76       * whenever a new result object (and thus a new current file handler) becomes available. The field must be volatile
77       * because it is accessed by the reloading controller probably from within another thread.
78       */
79      private volatile ReloadingDetector resultReloadingDetector;
80  
81      /**
82       * Creates a new instance of {@code ReloadingFileBasedConfigurationBuilder} which produces result objects of the
83       * specified class.
84       *
85       * @param resCls the result class (must not be <strong>null</strong>
86       * @throws IllegalArgumentException if the result class is <strong>null</strong>
87       */
88      public ReloadingFileBasedConfigurationBuilder(final Class<? extends T> resCls) {
89          super(resCls);
90          reloadingController = createReloadingController();
91      }
92  
93      /**
94       * Creates a new instance of {@code ReloadingFileBasedConfigurationBuilder} which produces result objects of the
95       * specified class and sets initialization parameters.
96       *
97       * @param resCls the result class (must not be <strong>null</strong>
98       * @param params a map with initialization parameters
99       * @throws IllegalArgumentException if the result class is <strong>null</strong>
100      */
101     public ReloadingFileBasedConfigurationBuilder(final Class<? extends T> resCls, final Map<String, Object> params) {
102         super(resCls, params);
103         reloadingController = createReloadingController();
104     }
105 
106     /**
107      * Creates a new instance of {@code ReloadingFileBasedConfigurationBuilder} which produces result objects of the
108      * specified class and sets initialization parameters and the <em>allowFailOnInit</em> flag.
109      *
110      * @param resCls the result class (must not be <strong>null</strong>
111      * @param params a map with initialization parameters
112      * @param allowFailOnInit the <em>allowFailOnInit</em> flag
113      * @throws IllegalArgumentException if the result class is <strong>null</strong>
114      */
115     public ReloadingFileBasedConfigurationBuilder(final Class<? extends T> resCls, final Map<String, Object> params, final boolean allowFailOnInit) {
116         super(resCls, params, allowFailOnInit);
117         reloadingController = createReloadingController();
118     }
119 
120     /**
121      * {@inheritDoc} This method is overridden here to change the result type.
122      */
123     @Override
124     public ReloadingFileBasedConfigurationBuilder<T> configure(final BuilderParameters... params) {
125         super.configure(params);
126         return this;
127     }
128 
129     /**
130      * Creates the {@code ReloadingController} associated with this object. The controller is assigned a specialized
131      * reloading detector which delegates to the detector for the current result object. (
132      * {@code FileHandlerReloadingDetector} does not support changing the file handler, and {@code ReloadingController} does
133      * not support changing the reloading detector; therefore, this level of indirection is needed to change the monitored
134      * file dynamically.)
135      *
136      * @return the new {@code ReloadingController}
137      */
138     private ReloadingController createReloadingController() {
139         final ReloadingDetector ctrlDetector = createReloadingDetectorForController();
140         final ReloadingController ctrl = new ReloadingController(ctrlDetector);
141         connectToReloadingController(ctrl);
142         return ctrl;
143     }
144 
145     /**
146      * Creates a {@code ReloadingDetector} which monitors the passed in {@code FileHandler}. This method is called each time
147      * a new result object is created with the current {@code FileHandler}. This implementation checks whether a
148      * {@code ReloadingDetectorFactory} is specified in the current parameters. If this is the case, it is invoked.
149      * Otherwise, a default factory is used to create a {@code FileHandlerReloadingDetector} object. Note: This method is
150      * called from a synchronized block.
151      *
152      * @param handler the current {@code FileHandler}
153      * @param fbparams the object with parameters related to file-based builders
154      * @return a {@code ReloadingDetector} for this {@code FileHandler}
155      * @throws ConfigurationException if an error occurs
156      */
157     protected ReloadingDetector createReloadingDetector(final FileHandler handler, final FileBasedBuilderParametersImpl fbparams)
158         throws ConfigurationException {
159         return fetchDetectorFactory(fbparams).createReloadingDetector(handler, fbparams);
160     }
161 
162     /**
163      * Creates a {@code ReloadingDetector} wrapper to be passed to the associated {@code ReloadingController}. This detector
164      * wrapper simply delegates to the current {@code ReloadingDetector} if it is available.
165      *
166      * @return the wrapper {@code ReloadingDetector}
167      */
168     private ReloadingDetector createReloadingDetectorForController() {
169         return new ReloadingDetector() {
170             @Override
171             public boolean isReloadingRequired() {
172                 final ReloadingDetector detector = resultReloadingDetector;
173                 return detector != null && detector.isReloadingRequired();
174             }
175 
176             @Override
177             public void reloadingPerformed() {
178                 final ReloadingDetector detector = resultReloadingDetector;
179                 if (detector != null) {
180                     detector.reloadingPerformed();
181                 }
182             }
183         };
184     }
185 
186     /**
187      * Gets the {@code ReloadingController} associated with this builder. This controller is directly created. However,
188      * it becomes active (i.e. associated with a meaningful reloading detector) not before a result object was created.
189      *
190      * @return the {@code ReloadingController}
191      */
192     @Override
193     public ReloadingController getReloadingController() {
194         return reloadingController;
195     }
196 
197     /**
198      * {@inheritDoc} This implementation also takes care that a new {@code ReloadingDetector} for the new current
199      * {@code FileHandler} is created. Also, the reloading controller's reloading state has to be reset; after the creation
200      * of a new result object changes in the underlying configuration source have to be monitored again.
201      */
202     @Override
203     protected void initFileHandler(final FileHandler handler) throws ConfigurationException {
204         super.initFileHandler(handler);
205 
206         resultReloadingDetector = createReloadingDetector(handler, FileBasedBuilderParametersImpl.fromParameters(getParameters(), true));
207     }
208 }