|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DirectoryFileStateMonitor.java | 56.2% | 76.5% | 50% | 64.9% |
|
||||||||||||||
| 1 | /* | |
| 2 | * $Id: DirectoryFileStateMonitor.java,v 1.1 2004/04/27 10:59:15 | |
| 3 | * johndavidtaylor Exp $ Created on Apr 26, 2004 by jdt@roe.ac.uk The alipes | |
| 4 | * project Copyright | |
| 5 | * | |
| 6 | */ | |
| 7 | package org.abraracourcix.alipes.monitors.file; | |
| 8 | import java.io.File; | |
| 9 | import java.io.FileFilter; | |
| 10 | import java.io.IOException; | |
| 11 | import java.util.Set; | |
| 12 | ||
| 13 | import org.abraracourcix.alipes.listeners.Listener; | |
| 14 | /** | |
| 15 | * Like a FileStateMonitor, but monitors a specifig directory, rather than a | |
| 16 | * specific collection of files. Why? We want to allow for new files being | |
| 17 | * created. @TODO we're going to need to allow filters though. | |
| 18 | * | |
| 19 | * @author jdt | |
| 20 | */ | |
| 21 | public class DirectoryFileStateMonitor extends AbstractFileStateMonitor { | |
| 22 | /** | |
| 23 | * Directory we're monitoring. | |
| 24 | */ | |
| 25 | private File dir; | |
| 26 | /** | |
| 27 | * Constructor | |
| 28 | * | |
| 29 | * @param listener listener | |
| 30 | * @param pollingFrequency frequency of polling (see PollingMonitor) | |
| 31 | * @param dir directory to monitor | |
| 32 | */ | |
| 33 | 1 | public DirectoryFileStateMonitor( |
| 34 | final Listener listener, | |
| 35 | final long pollingFrequency, | |
| 36 | final File dir) { | |
| 37 | 1 | super(listener, pollingFrequency); |
| 38 | 1 | this.dir = dir; |
| 39 | 1 | updateFileList(true); |
| 40 | } | |
| 41 | /** | |
| 42 | * Constructor | |
| 43 | * | |
| 44 | * @param listener listener | |
| 45 | * @param pollingFrequency frequency of polling (see PollingMonitor) | |
| 46 | * @param dir directory to monitor | |
| 47 | * @param fileFilter filter to apply to the files in the directory | |
| 48 | * @throws IOException if the directory does not exist | |
| 49 | */ | |
| 50 | 0 | public DirectoryFileStateMonitor( |
| 51 | final Listener listener, | |
| 52 | final int pollingFrequency, | |
| 53 | final File dir, | |
| 54 | final FileFilter fileFilter) { | |
| 55 | 0 | this(listener, pollingFrequency, dir); |
| 56 | 0 | this.filter = fileFilter; |
| 57 | } | |
| 58 | /** | |
| 59 | * Commons-logging logger | |
| 60 | */ | |
| 61 | private static org.apache.commons.logging.Log log = | |
| 62 | org.apache.commons.logging.LogFactory.getLog(FileStateMonitor.class); | |
| 63 | private FileFilter filter = null; | |
| 64 | /** | |
| 65 | * Check the directory to see if any new files have appeared | |
| 66 | * This checks the root folder, and includes any folders within that folder, but it | |
| 67 | * is not recursive, so it does not drill down into subfolders. The root folder itself | |
| 68 | * is also included in the list. Note that overtime this list could bloat and bloat | |
| 69 | * as there is no mechanism for removing files once they're in. | |
| 70 | * | |
| 71 | * @param treatAsUnknown sets the initial state you want for the file - if | |
| 72 | * the program has started up, you probably want it to be set to | |
| 73 | * UNKNOWN, so set this argument to true, otherwise will be as | |
| 74 | * newly created | |
| 75 | */ | |
| 76 | 1 | private void updateFileList(final boolean treatAsUnknown) { |
| 77 | 1 | assert dir != null; |
| 78 | ||
| 79 | 1 | final Set existingFiles = getMonitoredFiles(); |
| 80 | 1 | assert existingFiles != null; |
| 81 | 1 | final File[] files = dir.listFiles(filter); |
| 82 | 1 | if (files!=null ) { |
| 83 | // null indicates that this is perhaps not a directory | |
| 84 | // so it contains no files | |
| 85 | ||
| 86 | 1 | for (int i = 0; i < files.length; ++i) { |
| 87 | 304 | final File candidate = files[i]; |
| 88 | 304 | if (!(existingFiles.contains(candidate))) { |
| 89 | 304 | addFile( |
| 90 | candidate, | |
| 91 | 304 | treatAsUnknown |
| 92 | ? FileState.UNKNOWN | |
| 93 | : FileState.FILE_NO_EXIST); | |
| 94 | } | |
| 95 | } | |
| 96 | } | |
| 97 | //Add the directory itself | |
| 98 | 1 | final File candidate = dir; |
| 99 | 1 | if (!(existingFiles.contains(candidate))) { |
| 100 | 1 | addFile( |
| 101 | candidate, | |
| 102 | 1 | treatAsUnknown |
| 103 | ? FileState.UNKNOWN | |
| 104 | : FileState.FILE_NO_EXIST); | |
| 105 | } | |
| 106 | } | |
| 107 | /** | |
| 108 | * Check the file list and respond acccordingly @TODO deal with exception | |
| 109 | * properly | |
| 110 | * | |
| 111 | * @see org.abraracourcix.alipes.monitors.PollingMonitor#pole() | |
| 112 | */ | |
| 113 | 0 | public final void pole() { |
| 114 | 0 | updateFileList(false); |
| 115 | //@TODO this doesn't need to be done every pole, but just for now.... | |
| 116 | 0 | checkTheFiles(); |
| 117 | } | |
| 118 | } | |
| 119 | /* | |
| 120 | * $Log: DirectoryFileStateMonitor.java,v $ | |
| 121 | * Revision 1.2 2004/06/03 15:50:24 johndavidtaylor | |
| 122 | * Refactored DirChangeFileStateMonitor's behaviour when | |
| 123 | * the directory it monitors disappears. Wired up the DirChangeEmailAlerter. | |
| 124 | * Revision 1.1 2004/04/27 10:59:15 | |
| 125 | * johndavidtaylor Refactored and added a directory monitor | |
| 126 | * | |
| 127 | */ |
|
||||||||||