Listeners for local transport updates.

This commit is contained in:
akwizgran
2011-08-02 09:44:15 +01:00
parent ff0909a0e9
commit 0e48f4ba55
4 changed files with 94 additions and 0 deletions

View File

@@ -151,6 +151,9 @@ public interface DatabaseComponent {
/** Records the user's rating for the given author. */
void setRating(AuthorId a, Rating r) throws DbException;
/** Sets the local transport details, replacing any existing details. */
void setTransports(Map<String, String> transports) throws DbException;
/**
* Makes the given group visible to the given set of contacts and invisible
* to any other contacts.

View File

@@ -902,6 +902,29 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
}
}
public void setTransports(Map<String, String> transports)
throws DbException {
boolean changed = false;
transportLock.writeLock().lock();
try {
Txn txn = db.startTransaction();
try {
if(!transports.equals(db.getTransports(txn))) {
db.setTransports(txn, transports);
changed = true;
}
db.commitTransaction(txn);
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
transportLock.writeLock().unlock();
}
// Call the listeners outside the lock
if(changed) callListeners(DatabaseListener.Event.TRANSPORTS_UPDATED);
}
public void setVisibility(GroupId g, Collection<ContactId> visible)
throws DbException {
contactLock.readLock().lock();

View File

@@ -668,6 +668,26 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
}
}
public void setTransports(Map<String, String> transports)
throws DbException {
boolean changed = false;
synchronized(transportLock) {
Txn txn = db.startTransaction();
try {
if(!transports.equals(db.getTransports(txn))) {
db.setTransports(txn, transports);
changed = true;
}
db.commitTransaction(txn);
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
}
// Call the listeners outside the lock
if(changed) callListeners(DatabaseListener.Event.TRANSPORTS_UPDATED);
}
public void setVisibility(GroupId g, Collection<ContactId> visible)
throws DbException {
synchronized(contactLock) {

View File

@@ -1100,4 +1100,52 @@ public abstract class DatabaseComponentTest extends TestCase {
context.assertIsSatisfied();
}
@Test
public void testTransportsChangedCallsListeners() throws Exception {
Mockery context = new Mockery();
@SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final DatabaseListener listener = context.mock(DatabaseListener.class);
context.checking(new Expectations() {{
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransports(txn);
will(returnValue(Collections.singletonMap("foo", "bar")));
oneOf(database).setTransports(txn,
Collections.singletonMap("bar", "baz"));
oneOf(database).commitTransaction(txn);
oneOf(listener).eventOccurred(
DatabaseListener.Event.TRANSPORTS_UPDATED);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransports(Collections.singletonMap("bar", "baz"));
context.assertIsSatisfied();
}
@Test
public void testTransportsUnchangedDoesNotCallListeners() throws Exception {
Mockery context = new Mockery();
@SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final DatabaseListener listener = context.mock(DatabaseListener.class);
context.checking(new Expectations() {{
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).getTransports(txn);
will(returnValue(Collections.singletonMap("bar", "baz")));
oneOf(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
db.addListener(listener);
db.setTransports(Collections.singletonMap("bar", "baz"));
context.assertIsSatisfied();
}
}