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  
18  package org.apache.commons.configuration2.reloading;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * A strategy to reload configuration based on management requests. Designed for JMX management.
25   */
26  public class ManagedReloadingDetector implements ReloadingDetector, ManagedReloadingDetectorMBean {
27      /** The logger. */
28      private final Log log = LogFactory.getLog(ManagedReloadingDetector.class);
29  
30      /** A flag whether a reload is required. */
31      private volatile boolean reloadingRequired;
32  
33      /**
34       * Constructs a new instance.
35       */
36      public ManagedReloadingDetector() {
37          // empty
38      }
39  
40      /**
41       * Checks whether reloading is required. This implementation checks whether the {@code refresh()} method has been
42       * invoked.
43       *
44       * @return a flag whether reloading is required
45       */
46      @Override
47      public boolean isReloadingRequired() {
48          return reloadingRequired;
49      }
50  
51      /**
52       * Tells this strategy that the monitored configuration file should be refreshed. This method will typically be called
53       * from outside (through an exposed MBean) on behalf of an administrator.
54       *
55       * @see org.apache.commons.configuration2.reloading.ManagedReloadingDetectorMBean#refresh()
56       */
57      @Override
58      public void refresh() {
59          log.info("Reloading configuration.");
60          reloadingRequired = true;
61      }
62  
63      /**
64       * {@inheritDoc} This implementation resets the internal flag indicating that a reload should be performed.
65       */
66      @Override
67      public void reloadingPerformed() {
68          reloadingRequired = false;
69      }
70  }