View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
17   *
18   * $Id: FileMonitor.java,v 1.1 2005/01/07 10:05:07 indal Exp $
19   */
20  package no.feide.moria.servlet;
21  
22  import java.io.File;
23  import java.io.FileNotFoundException;
24  
25  /***
26   *
27   * Based (in part) on ConfigurationManager#FileListenerTask
28   * 
29   * @author Eva Indal
30   * @version $Revision: 1.1 $
31   */
32  public class FileMonitor {
33      
34      /***
35       * The monitored file
36       */
37      private final File monitoredFile;
38  
39      /***
40       * Modification time of the watched file.
41       */
42      private long lastModified;
43  
44      /***
45       * Constructor.
46       * 
47       * @param fileURI The file to monitor
48       * @throws FileNotFoundException
49       */
50      public FileMonitor(final String fileURI) throws FileNotFoundException {
51          monitoredFile = fileForURI(fileURI);
52          this.lastModified = monitoredFile.lastModified();
53      }
54      
55      /***
56       * Returns <code>true</code> if the monitored file has changed.
57       * 
58       * @return <code>true</code> if file has changed 
59       */
60      public boolean hasChanged() {
61          return monitoredFile.lastModified() != lastModified;
62      }
63      
64      /***
65       * Resolves a fileURI to a <code>File</code> object.
66       * @param fileURI
67       *            Reference to the file (full path or relative within the
68       *            classpath).
69       * @return A <code>File</code> object referenced by the fileURI.
70       * @throws FileNotFoundException
71       *             If the fileURI cannot be resolved to a readable file.
72       */
73      private static File fileForURI(final String fileURI) throws FileNotFoundException {
74  
75          if (fileURI == null || fileURI.equals("")) { throw new FileNotFoundException("File reference cannot be null."); }
76  
77          final File file = new File(fileURI);
78          return file;
79      }
80  
81  
82  }