ManagedReloadingDetector.java

  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.  *     http://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.reloading;

  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;

  20. /**
  21.  * A strategy to reload configuration based on management requests. Designed for JMX management.
  22.  */
  23. public class ManagedReloadingDetector implements ReloadingDetector, ManagedReloadingDetectorMBean {
  24.     /** The logger. */
  25.     private final Log log = LogFactory.getLog(ManagedReloadingDetector.class);

  26.     /** A flag whether a reload is required. */
  27.     private volatile boolean reloadingRequired;

  28.     /**
  29.      * Checks whether reloading is required. This implementation checks whether the {@code refresh()} method has been
  30.      * invoked.
  31.      *
  32.      * @return a flag whether reloading is required
  33.      */
  34.     @Override
  35.     public boolean isReloadingRequired() {
  36.         return reloadingRequired;
  37.     }

  38.     /**
  39.      * Tells this strategy that the monitored configuration file should be refreshed. This method will typically be called
  40.      * from outside (through an exposed MBean) on behalf of an administrator.
  41.      *
  42.      * @see org.apache.commons.configuration2.reloading.ManagedReloadingDetectorMBean#refresh()
  43.      */
  44.     @Override
  45.     public void refresh() {
  46.         log.info("Reloading configuration.");
  47.         reloadingRequired = true;
  48.     }

  49.     /**
  50.      * {@inheritDoc} This implementation resets the internal flag indicating that a reload should be performed.
  51.      */
  52.     @Override
  53.     public void reloadingPerformed() {
  54.         reloadingRequired = false;
  55.     }
  56. }