    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    /** Stores the logger. */
    047    private final Log log = LogFactory.getLog(getClass());
    048
    049    /**
    050     * Creates a new instance of {@code VFSFileHandlerReloadingDetector} and initializes it with an empty
    051     * {@code FileHandler} object.
    052     */
    053    public VFSFileHandlerReloadingDetector() {
    054    }
    055
    056    /**
    057     * Creates a new instance of {@code VFSFileHandlerReloadingDetector} and initializes it with the given
    058     * {@code FileHandler} object.
    059     *
    060     * @param handler the {@code FileHandler}
    061     */
    062    public VFSFileHandlerReloadingDetector(final FileHandler handler) {
    063        super(handler);
    064    }
    065
    066    /**
    067     * Creates a new instance of {@code VFSFileHandlerReloadingDetector} and initializes it with the given
    068     * {@code FileHandler} object and the given refresh delay.
    069     *
    070     * @param handler the {@code FileHandler}
    071     * @param refreshDelay the refresh delay
    072     */
    073    public VFSFileHandlerReloadingDetector(final FileHandler handler, final long refreshDelay) {
    074        super(handler, refreshDelay);
    075    }
    076
    077    /**
    078     * Gets the file that is monitored by this strategy. Note that the return value can be <strong>null </strong> under some
    079     * circumstances.
    080     *
    081     * @return the monitored file
    082     */
    083    protected FileObject getFileObject() {
    084        if (!getFileHandler().isLocationDefined()) {
    085            return null;
    086        }
    087
    088        try {
    089            final FileSystemManager fsManager = VFS.getManager();
    090            final String uri = resolveFileURI();
    091            if (uri == null) {
    092                throw new ConfigurationRuntimeException("Unable to determine file to monitor");
    093            }
    094            return fsManager.resolveFile(uri);
    095        } catch (final FileSystemException fse) {
    096            final String msg = "Unable to monitor " + getFileHandler().getURL().toString();
    097            log.error(msg);
    098            throw new ConfigurationRuntimeException(msg, fse);
    099        }
    100    }
    101
    102    /**
    103     * {@inheritDoc} This implementation uses Commons VFS to obtain a {@code FileObject} and read the date of the last
    104     * modification.
    105     */
    106    @Override
    107    protected long getLastModificationDate() {
    108        final FileObject file = getFileObject();
    109        try {
    110            if (file == null || !file.exists()) {
    111                return 0;
    112            }
    113
    114            return file.getContent().getLastModifiedTime();
    115        } catch (final FileSystemException ex) {
    116            log.error("Unable to get last modified time for" + file.getName().getURI(), ex);
    117            return 0;
    118        }
    119    }
    120
    121    /**
    122     * Resolves the URI of the monitored file.
    123     *
    124     * @return the URI of the monitored file or <strong>null</strong> if it cannot be resolved
    125     */
    126    protected String resolveFileURI() {
    127        final FileSystem fs = getFileHandler().getFileSystem();
    128        return fs.getPath(null, getFileHandler().getURL(), getFileHandler().getBasePath(), getFileHandler().getFileName());
    129    }
    130}