Author: niallp
Date: Thu Dec 23 00:13:25 2010
New Revision: 1052114

URL: http://svn.apache.org/viewvc?rev=1052114&view=rev
Log:
IO-256 - Provide ThreadFactory for FileAlternationMonitor - thanks to Martin 
Beránek

Modified:
    
commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
    
commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java

Modified: 
commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java?rev=1052114&r1=1052113&r2=1052114&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
 (original)
+++ 
commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java
 Thu Dec 23 00:13:25 2010
@@ -18,6 +18,7 @@ package org.apache.commons.io.monitor;
 
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ThreadFactory;
 
 /**
  * A runnable that spawns a monitoring thread triggering any
@@ -32,6 +33,7 @@ public final class FileAlterationMonitor
     private final long interval;
     private final List<FileAlterationObserver> observers = new 
CopyOnWriteArrayList<FileAlterationObserver>();
     private Thread thread = null;
+    private ThreadFactory threadFactory;
     private volatile boolean running = false;
 
     /**
@@ -77,6 +79,24 @@ public final class FileAlterationMonitor
     }
 
     /**
+     * Return the thread factory.
+     *
+     * @return the threadFactory
+     */
+    public ThreadFactory getThreadFactory() {
+        return threadFactory;
+    }
+
+    /**
+     * Set the thread factory.
+     *
+     * @param threadFactory the thread factory
+     */
+    public void setThreadFactory(ThreadFactory threadFactory) {
+        this.threadFactory = threadFactory;
+    }
+
+    /**
      * Add a file system observer to this monitor.
      *
      * @param observer The file system observer to add
@@ -122,7 +142,11 @@ public final class FileAlterationMonitor
             observer.initialize();
         }
         running = true;
-        thread = new Thread(this);
+        if (threadFactory != null) {
+            thread = threadFactory.newThread(this);
+        } else {
+            thread = new Thread(this);
+        }
         thread.start();
     }
 

Modified: 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java?rev=1052114&r1=1052113&r2=1052114&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java
 (original)
+++ 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java
 Thu Dec 23 00:13:25 2010
@@ -19,6 +19,7 @@ package org.apache.commons.io.monitor;
 import java.io.File;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.concurrent.Executors;
 
 /**
  * {...@link FileAlterationMonitor} Test Case.
@@ -90,8 +91,10 @@ public class FileAlterationMonitorTestCa
     public void testMonitor() {
         try {
             long interval = 100;
+            listener.clear();
             FileAlterationMonitor monitor = new 
FileAlterationMonitor(interval, observer);
             assertEquals("Interval", interval, monitor.getInterval());
+            assertNull("Thread Factory", monitor.getThreadFactory());
             monitor.start();
 
             try {
@@ -133,6 +136,40 @@ public class FileAlterationMonitorTestCa
     }
 
     /**
+     * Test using a thread factory.
+     */
+    public void testThreadFactory() {
+        try {
+            long interval = 100;
+            listener.clear();
+            FileAlterationMonitor monitor = new 
FileAlterationMonitor(interval, observer);
+            monitor.setThreadFactory(Executors.defaultThreadFactory());
+            assertEquals("Interval", interval, monitor.getInterval());
+            assertNotNull("Thread Factory", monitor.getThreadFactory());
+            monitor.start();
+
+            // Create a File
+            checkCollectionsEmpty("A");
+            File file2 = touch(new File(testDir, "file2.java"));
+            checkFile("Create", file2, listener.getCreatedFiles());
+            listener.clear();
+
+            // Delete a file
+            checkCollectionsEmpty("B");
+            file2.delete();
+            checkFile("Delete", file2, listener.getDeletedFiles());
+            listener.clear();
+
+            // Stop monitoring
+            monitor.stop();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail("Threw " + e);
+        }
+    }
+
+    /**
      * Check all the File Collections have the expected sizes.
      */
     private void checkFile(String label, File file, Collection<File> files) {


Reply via email to