Identity manager hooks. #209

This commit is contained in:
akwizgran
2016-01-20 12:03:15 +00:00
parent 82cf12040f
commit c4692a7007
16 changed files with 258 additions and 53 deletions

View File

@@ -334,7 +334,7 @@ interface Database<T> {
* Locking: read
*/
Collection<MessageStatus> getMessageStatus(T txn, ContactId c, GroupId g)
throws DbException;
throws DbException;
/**
* Returns the status of the given message with respect to the given
@@ -343,7 +343,7 @@ interface Database<T> {
* Locking: read
*/
MessageStatus getMessageStatus(T txn, ContactId c, MessageId m)
throws DbException;
throws DbException;
/**
* Returns the IDs of some messages received from the given contact that
@@ -388,7 +388,7 @@ interface Database<T> {
* Locking: read.
*/
Collection<MessageId> getMessagesToValidate(T txn, ClientId c)
throws DbException;
throws DbException;
/**
* Returns the message with the given ID, in serialised form.
@@ -639,6 +639,14 @@ interface Database<T> {
void setContactStatus(T txn, ContactId c, Contact.Status s)
throws DbException;
/**
* Sets the status of the given local pseudonym.
* <p>
* Locking: write.
*/
void setLocalAuthorStatus(T txn, AuthorId a, LocalAuthor.Status s)
throws DbException;
/**
* Marks the given message as valid or invalid.
* <p>

View File

@@ -16,10 +16,7 @@ import org.briarproject.api.db.NoSuchLocalAuthorException;
import org.briarproject.api.db.NoSuchMessageException;
import org.briarproject.api.db.NoSuchSubscriptionException;
import org.briarproject.api.db.NoSuchTransportException;
import org.briarproject.api.event.ContactRemovedEvent;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.LocalAuthorAddedEvent;
import org.briarproject.api.event.LocalAuthorRemovedEvent;
import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent;
import org.briarproject.api.event.LocalTransportsUpdatedEvent;
import org.briarproject.api.event.MessageAddedEvent;
@@ -218,7 +215,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
} finally {
lock.writeLock().unlock();
}
eventBus.broadcast(new LocalAuthorAddedEvent(a.getId()));
}
public void addLocalMessage(Message m, ClientId c, Metadata meta)
@@ -572,6 +568,25 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public Collection<ContactId> getContacts(AuthorId a) throws DbException {
lock.readLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsLocalAuthor(txn, a))
throw new NoSuchLocalAuthorException();
Collection<ContactId> contacts = db.getContacts(txn, a);
db.commitTransaction(txn);
return contacts;
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.readLock().unlock();
}
}
public Group getGroup(GroupId g) throws DbException {
lock.readLock().lock();
try {
@@ -1242,14 +1257,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
public void removeLocalAuthor(AuthorId a) throws DbException {
Collection<ContactId> affected;
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsLocalAuthor(txn, a))
throw new NoSuchLocalAuthorException();
affected = db.getContacts(txn, a);
db.removeLocalAuthor(txn, a);
db.commitTransaction(txn);
} catch (DbException e) {
@@ -1259,9 +1272,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
} finally {
lock.writeLock().unlock();
}
for (ContactId c : affected)
eventBus.broadcast(new ContactRemovedEvent(c));
eventBus.broadcast(new LocalAuthorRemovedEvent(a));
}
public void removeTransport(TransportId t) throws DbException {
@@ -1302,6 +1312,25 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public void setLocalAuthorStatus(AuthorId a, LocalAuthor.Status s)
throws DbException {
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsLocalAuthor(txn, a))
throw new NoSuchLocalAuthorException();
db.setLocalAuthorStatus(txn, a, s);
db.commitTransaction(txn);
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.writeLock().unlock();
}
}
public void setMessageValidity(Message m, ClientId c, boolean valid)
throws DbException {
lock.writeLock().lock();

View File

@@ -64,8 +64,8 @@ import static org.briarproject.db.ExponentialBackoff.calculateExpiry;
*/
abstract class JdbcDatabase implements Database<Connection> {
private static final int SCHEMA_VERSION = 15;
private static final int MIN_SCHEMA_VERSION = 15;
private static final int SCHEMA_VERSION = 16;
private static final int MIN_SCHEMA_VERSION = 16;
private static final String CREATE_SETTINGS =
"CREATE TABLE settings"
@@ -81,6 +81,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " publicKey BINARY NOT NULL,"
+ " privateKey BINARY NOT NULL,"
+ " created BIGINT NOT NULL,"
+ " status INT NOT NULL,"
+ " PRIMARY KEY (authorId))";
private static final String CREATE_CONTACTS =
@@ -719,15 +720,16 @@ abstract class JdbcDatabase implements Database<Connection> {
throws DbException {
PreparedStatement ps = null;
try {
String sql = "INSERT INTO localAuthors"
+ " (authorId, name, publicKey, privateKey, created)"
+ " VALUES (?, ?, ?, ?, ?)";
String sql = "INSERT INTO localAuthors (authorId, name, publicKey,"
+ " privateKey, created, status)"
+ " VALUES (?, ?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, a.getId().getBytes());
ps.setString(2, a.getName());
ps.setBytes(3, a.getPublicKey());
ps.setBytes(4, a.getPrivateKey());
ps.setLong(5, a.getTimeCreated());
ps.setInt(6, a.getStatus().getValue());
int affected = ps.executeUpdate();
if (affected != 1) throw new DbStateException();
ps.close();
@@ -1345,7 +1347,7 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT name, publicKey, privateKey, created"
String sql = "SELECT name, publicKey, privateKey, created, status"
+ " FROM localAuthors"
+ " WHERE authorId = ?";
ps = txn.prepareStatement(sql);
@@ -1356,8 +1358,10 @@ abstract class JdbcDatabase implements Database<Connection> {
byte[] publicKey = rs.getBytes(2);
byte[] privateKey = rs.getBytes(3);
long created = rs.getLong(4);
LocalAuthor.Status status = LocalAuthor.Status.fromValue(
rs.getInt(5));
LocalAuthor localAuthor = new LocalAuthor(a, name, publicKey,
privateKey, created);
privateKey, created, status);
if (rs.next()) throw new DbStateException();
rs.close();
ps.close();
@@ -1374,7 +1378,8 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT authorId, name, publicKey, privateKey, created"
String sql = "SELECT authorId, name, publicKey, privateKey,"
+ " created, status"
+ " FROM localAuthors";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
@@ -1385,8 +1390,10 @@ abstract class JdbcDatabase implements Database<Connection> {
byte[] publicKey = rs.getBytes(3);
byte[] privateKey = rs.getBytes(4);
long created = rs.getLong(5);
LocalAuthor.Status status = LocalAuthor.Status.fromValue(
rs.getInt(6));
authors.add(new LocalAuthor(authorId, name, publicKey,
privateKey, created));
privateKey, created, status));
}
rs.close();
ps.close();
@@ -2398,7 +2405,8 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void mergeSettings(Connection txn, Settings s, String namespace) throws DbException {
public void mergeSettings(Connection txn, Settings s, String namespace)
throws DbException {
PreparedStatement ps = null;
try {
// Update any settings that already exist
@@ -2712,6 +2720,24 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void setLocalAuthorStatus(Connection txn, AuthorId a,
LocalAuthor.Status s) throws DbException {
PreparedStatement ps = null;
try {
String sql = "UPDATE localAuthors SET status = ?"
+ " WHERE authorId = ?";
ps = txn.prepareStatement(sql);
ps.setInt(1, s.getValue());
ps.setBytes(2, a.getBytes());
int affected = ps.executeUpdate();
if (affected < 0 || affected > 1) throw new DbStateException();
ps.close();
} catch (SQLException e) {
tryToClose(ps);
throw new DbException(e);
}
}
public void setMessageValidity(Connection txn, MessageId m, boolean valid)
throws DbException {
PreparedStatement ps = null;