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