diff --git a/src/net/sf/briar/api/clock/SystemTimer.java b/src/net/sf/briar/api/clock/SystemTimer.java new file mode 100644 index 0000000000000000000000000000000000000000..b220e3026b2b4a5acca196138899e3b7f38344bf --- /dev/null +++ b/src/net/sf/briar/api/clock/SystemTimer.java @@ -0,0 +1,29 @@ +package net.sf.briar.api.clock; + +import java.util.TimerTask; + +/** Default timer implementation. */ +public class SystemTimer implements Timer { + + private final java.util.Timer timer = new java.util.Timer(); + + public void cancel() { + timer.cancel(); + } + + public int purge() { + return timer.purge(); + } + + public void schedule(TimerTask task, long delay) { + timer.schedule(task, delay); + } + + public void schedule(TimerTask task, long delay, long period) { + timer.schedule(task, delay, period); + } + + public void scheduleAtFixedRate(TimerTask task, long delay, long period) { + timer.scheduleAtFixedRate(task, delay, period); + } +} diff --git a/src/net/sf/briar/api/clock/Timer.java b/src/net/sf/briar/api/clock/Timer.java new file mode 100644 index 0000000000000000000000000000000000000000..9e5cae3213e26c904b583916e4e7bc5a6e899ac7 --- /dev/null +++ b/src/net/sf/briar/api/clock/Timer.java @@ -0,0 +1,27 @@ +package net.sf.briar.api.clock; + +import java.util.TimerTask; + +/** + * A wrapper around a {@link java.util.Timer} that allows it to be replaced for + * testing. + */ +public interface Timer { + + /** @see {@link java.util.Timer#cancel()} */ + void cancel(); + + /** @see {@link java.util.Timer#purge()} */ + int purge(); + + /** @see {@link java.util.Timer#schedule(TimerTask, long)} */ + void schedule(TimerTask task, long delay); + + /** @see {@link java.util.Timer#schedule(TimerTask, long, long)} */ + void schedule(TimerTask task, long delay, long period); + + /** + * @see {@link java.util.Timer#scheduleAtFixedRate(TimerTask, long, long)} + */ + void scheduleAtFixedRate(TimerTask task, long delay, long period); +} diff --git a/src/net/sf/briar/api/crypto/MessageDigest.java b/src/net/sf/briar/api/crypto/MessageDigest.java index 641bf8d0fc35e26044e3f06cc5b35eb57869e34d..3855da89295be46d936fd2e8f92f7527150d4e52 100644 --- a/src/net/sf/briar/api/crypto/MessageDigest.java +++ b/src/net/sf/briar/api/crypto/MessageDigest.java @@ -1,6 +1,9 @@ package net.sf.briar.api.crypto; -/** An interface that allows a java.security.MessageDigest to be wrapped. */ +/** + * A wrapper around a {@link java.security.MessageDigest} that allows it to be + * replaced for testing. + */ public interface MessageDigest { /** @see {@link java.security.MessageDigest#digest()} */ diff --git a/src/net/sf/briar/clock/ClockModule.java b/src/net/sf/briar/clock/ClockModule.java index f394211cc3dc9877d4233741baa0e8f0b8396fe5..8bf9a3816dc240c51b28bd646c3af9e7f66f4aa3 100644 --- a/src/net/sf/briar/clock/ClockModule.java +++ b/src/net/sf/briar/clock/ClockModule.java @@ -2,6 +2,8 @@ package net.sf.briar.clock; import net.sf.briar.api.clock.Clock; import net.sf.briar.api.clock.SystemClock; +import net.sf.briar.api.clock.SystemTimer; +import net.sf.briar.api.clock.Timer; import com.google.inject.AbstractModule; @@ -10,5 +12,6 @@ public class ClockModule extends AbstractModule { @Override protected void configure() { bind(Clock.class).to(SystemClock.class); + bind(Timer.class).to(SystemTimer.class); } } diff --git a/src/net/sf/briar/db/DatabaseCleanerImpl.java b/src/net/sf/briar/db/DatabaseCleanerImpl.java index 6c0673068bd401a22b36ec1d9c950d60955e99e2..4b22fcdecb6f73a98ba144c9781439ff1ff245b1 100644 --- a/src/net/sf/briar/db/DatabaseCleanerImpl.java +++ b/src/net/sf/briar/db/DatabaseCleanerImpl.java @@ -3,29 +3,35 @@ package net.sf.briar.db; import static java.util.logging.Level.INFO; import static java.util.logging.Level.WARNING; -import java.util.Timer; import java.util.TimerTask; import java.util.logging.Logger; +import net.sf.briar.api.clock.Timer; import net.sf.briar.api.db.DbClosedException; import net.sf.briar.api.db.DbException; +import com.google.inject.Inject; + class DatabaseCleanerImpl extends TimerTask implements DatabaseCleaner { private static final Logger LOG = Logger.getLogger(DatabaseCleanerImpl.class.getName()); + private final Timer timer; + private volatile Callback callback = null; - private volatile Timer timer = null; + + @Inject + DatabaseCleanerImpl(Timer timer) { + this.timer = timer; + } public void startCleaning(Callback callback, long msBetweenSweeps) { this.callback = callback; - timer = new Timer(); timer.scheduleAtFixedRate(this, 0L, msBetweenSweeps); } public void stopCleaning() { - if(timer == null) throw new IllegalStateException(); timer.cancel(); } diff --git a/src/net/sf/briar/transport/KeyManagerImpl.java b/src/net/sf/briar/transport/KeyManagerImpl.java index 6ff240356bf53a3001594255fd4ef24df38f2951..39b4175abc681e950e75cb247b58c03d32d55f02 100644 --- a/src/net/sf/briar/transport/KeyManagerImpl.java +++ b/src/net/sf/briar/transport/KeyManagerImpl.java @@ -8,11 +8,11 @@ import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.Timer; import java.util.TimerTask; import java.util.logging.Logger; import net.sf.briar.api.ContactId; +import net.sf.briar.api.clock.Timer; import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.KeyManager; import net.sf.briar.api.db.DatabaseComponent; @@ -49,11 +49,11 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener { @Inject public KeyManagerImpl(CryptoComponent crypto, DatabaseComponent db, - ConnectionRecogniser recogniser) { + ConnectionRecogniser recogniser, Timer timer) { this.crypto = crypto; this.db = db; this.recogniser = recogniser; - timer = new Timer(); + this.timer = timer; outgoing = new HashMap<ContactTransportKey, TemporarySecret>(); incomingOld = new HashMap<ContactTransportKey, TemporarySecret>(); incomingNew = new HashMap<ContactTransportKey, TemporarySecret>();