VFSFileHandlerReloadingDetector.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.configuration2.ex.ConfigurationRuntimeException;
  19. import org.apache.commons.configuration2.io.FileHandler;
  20. import org.apache.commons.configuration2.io.FileSystem;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.commons.vfs2.FileObject;
  24. import org.apache.commons.vfs2.FileSystemException;
  25. import org.apache.commons.vfs2.FileSystemManager;
  26. import org.apache.commons.vfs2.VFS;

  27. /**
  28.  * <p>
  29.  * A file-based reloading strategy that uses <a href="https://commons.apache.org/vfs/">Commons VFS</a> to determine when
  30.  * a file was changed.
  31.  * </p>
  32.  * <p>
  33.  * This reloading strategy is very similar to {@link FileHandlerReloadingDetector}, except for the fact that it uses VFS
  34.  * and thus can deal with a variety of different configuration sources.
  35.  * </p>
  36.  * <p>
  37.  * This strategy only works with FileConfiguration instances.
  38.  * </p>
  39.  *
  40.  * @since 1.7
  41.  */
  42. public class VFSFileHandlerReloadingDetector extends FileHandlerReloadingDetector {
  43.     /** Stores the logger. */
  44.     private final Log log = LogFactory.getLog(getClass());

  45.     /**
  46.      * Creates a new instance of {@code VFSFileHandlerReloadingDetector} and initializes it with an empty
  47.      * {@code FileHandler} object.
  48.      */
  49.     public VFSFileHandlerReloadingDetector() {
  50.     }

  51.     /**
  52.      * Creates a new instance of {@code VFSFileHandlerReloadingDetector} and initializes it with the given
  53.      * {@code FileHandler} object.
  54.      *
  55.      * @param handler the {@code FileHandler}
  56.      */
  57.     public VFSFileHandlerReloadingDetector(final FileHandler handler) {
  58.         super(handler);
  59.     }

  60.     /**
  61.      * Creates a new instance of {@code VFSFileHandlerReloadingDetector} and initializes it with the given
  62.      * {@code FileHandler} object and the given refresh delay.
  63.      *
  64.      * @param handler the {@code FileHandler}
  65.      * @param refreshDelay the refresh delay
  66.      */
  67.     public VFSFileHandlerReloadingDetector(final FileHandler handler, final long refreshDelay) {
  68.         super(handler, refreshDelay);
  69.     }

  70.     /**
  71.      * Gets the file that is monitored by this strategy. Note that the return value can be <strong>null </strong> under some
  72.      * circumstances.
  73.      *
  74.      * @return the monitored file
  75.      */
  76.     protected FileObject getFileObject() {
  77.         if (!getFileHandler().isLocationDefined()) {
  78.             return null;
  79.         }

  80.         try {
  81.             final FileSystemManager fsManager = VFS.getManager();
  82.             final String uri = resolveFileURI();
  83.             if (uri == null) {
  84.                 throw new ConfigurationRuntimeException("Unable to determine file to monitor");
  85.             }
  86.             return fsManager.resolveFile(uri);
  87.         } catch (final FileSystemException fse) {
  88.             final String msg = "Unable to monitor " + getFileHandler().getURL().toString();
  89.             log.error(msg);
  90.             throw new ConfigurationRuntimeException(msg, fse);
  91.         }
  92.     }

  93.     /**
  94.      * {@inheritDoc} This implementation uses Commons VFS to obtain a {@code FileObject} and read the date of the last
  95.      * modification.
  96.      */
  97.     @Override
  98.     protected long getLastModificationDate() {
  99.         final FileObject file = getFileObject();
  100.         try {
  101.             if (file == null || !file.exists()) {
  102.                 return 0;
  103.             }

  104.             return file.getContent().getLastModifiedTime();
  105.         } catch (final FileSystemException ex) {
  106.             log.error("Unable to get last modified time for" + file.getName().getURI(), ex);
  107.             return 0;
  108.         }
  109.     }

  110.     /**
  111.      * Resolves the URI of the monitored file.
  112.      *
  113.      * @return the URI of the monitored file or <strong>null</strong> if it cannot be resolved
  114.      */
  115.     protected String resolveFileURI() {
  116.         final FileSystem fs = getFileHandler().getFileSystem();
  117.         return fs.getPath(null, getFileHandler().getURL(), getFileHandler().getBasePath(), getFileHandler().getFileName());
  118.     }
  119. }