    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 *     https://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 */
    017
    018package org.apache.commons.configuration2.reloading;
    019
    020import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
    021import org.apache.commons.configuration2.io.FileHandler;
    022import org.apache.commons.configuration2.io.FileSystem;
    023import org.apache.commons.logging.Log;
    024import org.apache.commons.logging.LogFactory;
    025import org.apache.commons.vfs2.FileObject;
    026import org.apache.commons.vfs2.FileSystemException;
    027import org.apache.commons.vfs2.FileSystemManager;
    028import org.apache.commons.vfs2.VFS;
    029
    030/**
    031 * <p>
    032 * A file-based reloading strategy that uses <a href="https://commons.apache.org/vfs/">Commons VFS</a> to determine when
    033 * a file was changed.
    034 * </p>
    035 * <p>
    036 * This reloading strategy is very similar to {@link FileHandlerReloadingDetector}, except for the fact that it uses VFS
    037 * and thus can deal with a variety of different configuration sources.
    038 * </p>
    039 * <p>
    040 * This strategy only works with FileConfiguration instances.
    041 * </p>
    042 *
    043 * @since 1.7
    044 */
    045public class VFSFileHandlerReloadingDetector extends FileHandlerReloadingDetector {
    046
    047    /** Stores the logger. */
    048    private final Log log = LogFactory.getLog(getClass());
    049
    050    /**
    051     * Creates a new instance of {@code VFSFileHandlerReloadingDetector} and initializes it with an empty
    052     * {@code FileHandler} object.
    053     */
    054    public VFSFileHandlerReloadingDetector() {
    055    }
    056
    057    /**
    058     * Creates a new instance of {@code VFSFileHandlerReloadingDetector} and initializes it with the given
    059     * {@code FileHandler} object.
    060     *
    061     * @param handler the {@code FileHandler}
    062     */
    063    public VFSFileHandlerReloadingDetector(final FileHandler handler) {
    064        super(handler);
    065    }
    066
    067    /**
    068     * Creates a new instance of {@code VFSFileHandlerReloadingDetector} and initializes it with the given
    069     * {@code FileHandler} object and the given refresh delay.
    070     *
    071     * @param handler the {@code FileHandler}
    072     * @param refreshDelay the refresh delay
    073     */
    074    public VFSFileHandlerReloadingDetector(final FileHandler handler, final long refreshDelay) {
    075        super(handler, refreshDelay);
    076    }
    077
    078    /**
    079     * Gets the file that is monitored by this strategy. Note that the return value can be <strong>null </strong> under some
    080     * circumstances.
    081     *
    082     * @return the monitored file
    083     */
    084    protected FileObject getFileObject() {
    085        if (!getFileHandler().isLocationDefined()) {
    086            return null;
    087        }
    088
    089        try {
    090            final FileSystemManager fsManager = VFS.getManager();
    091            final String uri = resolveFileURI();
    092            if (uri == null) {
    093                throw new ConfigurationRuntimeException("Unable to determine file to monitor");
    094            }
    095            return fsManager.resolveFile(uri);
    096        } catch (final FileSystemException e) {
    097            final String msg = "Unable to monitor " + getFileHandler().getURL().toString();
    098            log.error(msg);
    099            throw new ConfigurationRuntimeException(msg, e);
    100        }
    101    }
    102
    103    /**
    104     * {@inheritDoc} This implementation uses Commons VFS to obtain a {@code FileObject} and read the date of the last
    105     * modification.
    106     */
    107    @Override
    108    protected long getLastModificationDate() {
    109        final FileObject file = getFileObject();
    110        try {
    111            if (file == null || !file.exists()) {
    112                return 0;
    113            }
    114
    115            return file.getContent().getLastModifiedTime();
    116        } catch (final FileSystemException ex) {
    117            log.error("Unable to get last modified time for" + file.getName().getURI(), ex);
    118            return 0;
    119        }
    120    }
    121
    122    /**
    123     * Resolves the URI of the monitored file.
    124     *
    125     * @return the URI of the monitored file or <strong>null</strong> if it cannot be resolved
    126     */
    127    protected String resolveFileURI() {
    128        final FileSystem fs = getFileHandler().getFileSystem();
    129        return fs.getPath(null, getFileHandler().getURL(), getFileHandler().getBasePath(), getFileHandler().getFileName());
    130    }
    131}