Refactored PollingRemovableDriveMonitor, fixed test.

This commit is contained in:
akwizgran
2012-05-04 14:24:16 +01:00
parent 4f2704e624
commit 4338bd9a8b
5 changed files with 67 additions and 54 deletions

View File

@@ -12,7 +12,7 @@ import net.sf.briar.api.plugins.PluginExecutor;
class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
private static final Logger LOG =
Logger.getLogger(PollingRemovableDriveMonitor.class.getName());
Logger.getLogger(PollingRemovableDriveMonitor.class.getName());
private final Executor pluginExecutor;
private final RemovableDriveFinder finder;
@@ -21,7 +21,6 @@ class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
private volatile boolean running = false;
private volatile Callback callback = null;
private volatile IOException exception = null;
public PollingRemovableDriveMonitor(@PluginExecutor Executor pluginExecutor,
RemovableDriveFinder finder, long pollingInterval) {
@@ -34,7 +33,6 @@ class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
synchronized(this) {
assert !running;
assert this.callback == null;
assert exception == null;
running = true;
this.callback = callback;
}
@@ -42,19 +40,15 @@ class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
}
public void stop() throws IOException {
IOException e;
synchronized(this) {
assert running;
assert callback != null;
running = false;
callback = null;
e = exception;
exception = null;
}
synchronized(pollingLock) {
pollingLock.notifyAll();
}
if(e != null) throw e;
}
public void run() {
@@ -76,7 +70,7 @@ class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
LOG.info("Interrupted while waiting to poll");
Thread.currentThread().interrupt();
} catch(IOException e) {
exception = e;
callback.exceptionThrown(e);
}
}
}

View File

@@ -12,5 +12,7 @@ interface RemovableDriveMonitor {
interface Callback {
void driveInserted(File root);
void exceptionThrown(IOException e);
}
}

View File

@@ -123,4 +123,8 @@ implements RemovableDriveMonitor.Callback {
for(File f : files) if(f.isFile()) createReaderFromFile(f);
}
}
public void exceptionThrown(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
}
}