Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.cayenne.modeler.util;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
FileWatchdog is a watcher for files' change. If one of the files has changed or been removed, a doOnChange(org.apache.cayenne.modeler.util.FileWatchdog.FileInfo) or method will be called Original code taken from Log4J project
 
 public abstract class FileWatchdog extends Thread {

    
The default delay between every file modification check
 
     static final public long DEFAULT_DELAY = 4000;

    
The names of the files to observe for changes.
 
     protected Map<StringFileInfofilesInfo;

    
The delay to observe between every check. By default set DEFAULT_DELAY.
 
     protected long delay = ;

    
Paused flag
 
     protected boolean paused;

    
This flags shows whether only one or multiple notifications will be fired when several files change
 
     protected boolean singleNotification;

    
An object to enable synchronization
 
     private Object sync = new Object();
 
     private static Log log = LogFactory.getLog(FileWatchdog.class);
 
     protected FileWatchdog() {
          = Collections.synchronizedMap(new HashMap<StringFileInfo>());
         setDaemon(true);
     }

    
Sets whether only one or multiple notifications will be fired when several files change
 
     public void setSingleNotification(boolean b) {
          = b;
     }

    
Returns whether only one or multiple notifications will be fired when several files change
 
     public boolean isSingleNotification() {
         return ;
     }

    
Adds a new file to watch

Parameters:
location path of file
 
    public void addFile(String location) {
        synchronized () {
            try {
                .put(locationnew FileInfo(location));
            }
            catch (SecurityException e) {
                .error("SecurityException adding file " + locatione);
            }
        }
    }

    
Turns off watching for a specified file

Parameters:
location path of file
    public void removeFile(String location) {
        synchronized () {
            .remove(location);
        }
    }

    
Turns off watching for all files
    public void removeAllFiles() {
        synchronized () {
            .clear();
        }
    }

    
Set the delay to observe between each check of the file changes.
    public void setDelay(long delay) {
        this. = delay;
    }

    
Invoked when one of the watched files has changed

Parameters:
fileInfo Changed file info
    protected abstract void doOnChange(FileInfo fileInfo);

    
Invoked when one of the watched files has been removed

Parameters:
fileInfo Changed file info
    protected abstract void doOnRemove(FileInfo fileInfo);
    protected void check() {
        synchronized () {
            if ()
                return;
            List<FileInfochanged = new Vector<FileInfo>();
            List<FileInfodeleted = new Vector<FileInfo>();
            for (Iterator<FileInfoit = .values().iterator(); it.hasNext();) {
                FileInfo fi = it.next();
                boolean fileExists;
                try {
                    fileExists = fi.getFile().exists();
                }
                catch (SecurityException e) {
                    .error(
                            "SecurityException checking file " + fi.getFile().getPath(),
                            e);
                    // we still process with other files
                    continue;
                }
                if (fileExists) {
                    long l = fi.getFile().lastModified(); // this can also throw a
                                                            // SecurityException
                    if (l > fi.getLastModified()) { // however, if we reached this point
                                                    // this
                        fi.setLastModified(l); // is very unlikely.
                        changed.add(fi);
                    }
                }
                else if (fi.getLastModified() != -1) // the file has been removed
                {
                    deleted.add(fi);
                    it.remove(); // no point to watch the file now
                }
            }
            for (FileInfo aDeleted : deleted) {
                doOnRemove(aDeleted);
                if ()
                    return;
            }
            for (FileInfo aChanged : changed) {
                doOnChange(aChanged);
                if ()
                    return;
            }
        }
    }
    public void run() {
        while (true) {
            try {
                Thread.sleep();
                check();
            }
            catch (InterruptedException e) {
                // someone asked to stop
                return;
            }
        }
    }

    
Tells watcher to pause watching for some time. Useful before changing files
    public void pauseWatching() {
        synchronized () {
             = true;
        }
    }

    
Resumes watching for files
    public void resumeWatching() {
         = false;
    }

    
Class to store information about files (last modification time & File pointer)
    protected class FileInfo {

        
Exact java.io.File object, may not be null
        File file;

        
Time the file was modified
        long lastModified;

        
Creates new object

Parameters:
location the file path
        public FileInfo(String location) {
             = new File(location);
             = .exists() ? .lastModified() : -1;
        }
        public File getFile() {
            return ;
        }
        public long getLastModified() {
            return ;
        }
        public void setLastModified(long l) {
             = l;
        }
    }
New to GrepCode? Check out our FAQ X