mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
ENH: Replaces transport config with namespaced settings
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package org.briarproject.db;
|
||||
|
||||
import org.briarproject.api.Settings;
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
@@ -223,13 +222,6 @@ interface Database<T> {
|
||||
*/
|
||||
Collection<Group> getAvailableGroups(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the configuration for the given transport.
|
||||
* <p>
|
||||
* Locking: read.
|
||||
*/
|
||||
TransportConfig getConfig(T txn, TransportId t) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the contact with the given ID.
|
||||
* <p>
|
||||
@@ -419,11 +411,11 @@ interface Database<T> {
|
||||
int maxLength) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all settings.
|
||||
* Returns all settings that belong to a namespace.
|
||||
* <p>
|
||||
* Locking: read.
|
||||
*/
|
||||
Settings getSettings(T txn) throws DbException;
|
||||
Settings getSettings(T txn, String namespace) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all contacts who subscribe to the given group.
|
||||
@@ -524,15 +516,6 @@ interface Database<T> {
|
||||
void lowerRequestedFlag(T txn, ContactId c, Collection<MessageId> requested)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Merges the given configuration with the existing configuration for the
|
||||
* given transport.
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
void mergeConfig(T txn, TransportId t, TransportConfig config)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Merges the given properties with the existing local properties for the
|
||||
* given transport.
|
||||
@@ -547,7 +530,7 @@ interface Database<T> {
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
void mergeSettings(T txn, Settings s) throws DbException;
|
||||
void mergeSettings(T txn, Settings s, String Namespace) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks a message as needing to be acknowledged to the given contact.
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.briarproject.db;
|
||||
|
||||
import org.briarproject.api.Settings;
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
@@ -524,25 +523,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
}
|
||||
}
|
||||
|
||||
public TransportConfig getConfig(TransportId t) throws DbException {
|
||||
lock.readLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
TransportConfig config = db.getConfig(txn, t);
|
||||
db.commitTransaction(txn);
|
||||
return config;
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Contact getContact(ContactId c) throws DbException {
|
||||
lock.readLock().lock();
|
||||
try {
|
||||
@@ -808,12 +788,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
}
|
||||
}
|
||||
|
||||
public Settings getSettings() throws DbException {
|
||||
public Settings getSettings(String namespace) throws DbException {
|
||||
lock.readLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Settings s = db.getSettings(txn);
|
||||
Settings s = db.getSettings(txn, namespace);
|
||||
db.commitTransaction(txn);
|
||||
return s;
|
||||
} catch (DbException e) {
|
||||
@@ -939,25 +919,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
}
|
||||
}
|
||||
|
||||
public void mergeConfig(TransportId t, TransportConfig c)
|
||||
throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (!db.containsTransport(txn, t))
|
||||
throw new NoSuchTransportException();
|
||||
db.mergeConfig(txn, t, c);
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void mergeLocalProperties(TransportId t, TransportProperties p)
|
||||
throws DbException {
|
||||
boolean changed = false;
|
||||
@@ -982,14 +943,14 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
if (changed) eventBus.broadcast(new LocalTransportsUpdatedEvent());
|
||||
}
|
||||
|
||||
public void mergeSettings(Settings s) throws DbException {
|
||||
public void mergeSettings(Settings s, String namespace) throws DbException {
|
||||
boolean changed = false;
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (!s.equals(db.getSettings(txn))) {
|
||||
db.mergeSettings(txn, s);
|
||||
if (!s.equals(db.getSettings(txn, namespace))) {
|
||||
db.mergeSettings(txn, s, namespace);
|
||||
changed = true;
|
||||
}
|
||||
db.commitTransaction(txn);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.briarproject.db;
|
||||
|
||||
import org.briarproject.api.Settings;
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
@@ -64,14 +63,15 @@ import static org.briarproject.db.ExponentialBackoff.calculateExpiry;
|
||||
*/
|
||||
abstract class JdbcDatabase implements Database<Connection> {
|
||||
|
||||
private static final int SCHEMA_VERSION = 11;
|
||||
private static final int MIN_SCHEMA_VERSION = 10;
|
||||
private static final int SCHEMA_VERSION = 12;
|
||||
private static final int MIN_SCHEMA_VERSION = 12;
|
||||
|
||||
private static final String CREATE_SETTINGS =
|
||||
"CREATE TABLE settings"
|
||||
+ " (key VARCHAR NOT NULL,"
|
||||
+ " value VARCHAR NOT NULL,"
|
||||
+ " PRIMARY KEY (key))";
|
||||
+ " namespace VARCHAR NOT NULL,"
|
||||
+ " PRIMARY KEY (key, namespace))";
|
||||
|
||||
private static final String CREATE_LOCAL_AUTHORS =
|
||||
"CREATE TABLE localAuthors"
|
||||
@@ -343,7 +343,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
Settings s = new Settings();
|
||||
s.put("schemaVersion", String.valueOf(SCHEMA_VERSION));
|
||||
s.put("minSchemaVersion", String.valueOf(MIN_SCHEMA_VERSION));
|
||||
mergeSettings(txn, s);
|
||||
mergeSettings(txn, s, "db");
|
||||
}
|
||||
commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
@@ -354,7 +354,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
|
||||
private boolean checkSchemaVersion(Connection txn) throws DbException {
|
||||
try {
|
||||
Settings s = getSettings(txn);
|
||||
Settings s = getSettings(txn, "db");
|
||||
int schemaVersion = Integer.valueOf(s.get("schemaVersion"));
|
||||
if (schemaVersion == SCHEMA_VERSION) return true;
|
||||
if (schemaVersion < MIN_SCHEMA_VERSION) return false;
|
||||
@@ -1179,28 +1179,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public TransportConfig getConfig(Connection txn, TransportId t)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT key, value FROM transportConfigs"
|
||||
+ " WHERE transportId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, t.getString());
|
||||
rs = ps.executeQuery();
|
||||
TransportConfig c = new TransportConfig();
|
||||
while (rs.next()) c.put(rs.getString(1), rs.getString(2));
|
||||
rs.close();
|
||||
ps.close();
|
||||
return c;
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Contact getContact(Connection txn, ContactId c) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@@ -1921,12 +1899,13 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public Settings getSettings(Connection txn) throws DbException {
|
||||
public Settings getSettings(Connection txn, String namespace) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT key, value FROM settings";
|
||||
String sql = "SELECT key, value FROM settings WHERE namespace = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, namespace);
|
||||
rs = ps.executeQuery();
|
||||
Settings s = new Settings();
|
||||
while (rs.next()) s.put(rs.getString(1), rs.getString(2));
|
||||
@@ -2373,12 +2352,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void mergeConfig(Connection txn, TransportId t, TransportConfig c)
|
||||
throws DbException {
|
||||
// Merge the new configuration with the existing one
|
||||
mergeStringMap(txn, t, c, "transportConfigs");
|
||||
}
|
||||
|
||||
public void mergeLocalProperties(Connection txn, TransportId t,
|
||||
TransportProperties p) throws DbException {
|
||||
// Merge the new properties with the existing ones
|
||||
@@ -2446,15 +2419,16 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void mergeSettings(Connection txn, Settings s) throws DbException {
|
||||
public void mergeSettings(Connection txn, Settings s, String namespace) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
// Update any settings that already exist
|
||||
String sql = "UPDATE settings SET value = ? WHERE key = ?";
|
||||
String sql = "UPDATE settings SET value = ? WHERE key = ? AND namespace = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
for (Entry<String, String> e : s.entrySet()) {
|
||||
ps.setString(1, e.getValue());
|
||||
ps.setString(2, e.getKey());
|
||||
ps.setString(3, namespace);
|
||||
ps.addBatch();
|
||||
}
|
||||
int[] batchAffected = ps.executeBatch();
|
||||
@@ -2464,13 +2438,14 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
if (batchAffected[i] > 1) throw new DbStateException();
|
||||
}
|
||||
// Insert any settings that don't already exist
|
||||
sql = "INSERT INTO settings (key, value) VALUES (?, ?)";
|
||||
sql = "INSERT INTO settings (key, value, namespace) VALUES (?, ?, ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
int updateIndex = 0, inserted = 0;
|
||||
for (Entry<String, String> e : s.entrySet()) {
|
||||
if (batchAffected[updateIndex] == 0) {
|
||||
ps.setString(1, e.getKey());
|
||||
ps.setString(2, e.getValue());
|
||||
ps.setString(3, namespace);
|
||||
ps.addBatch();
|
||||
inserted++;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.plugins;
|
||||
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
@@ -28,6 +27,8 @@ import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.api.property.TransportPropertyManager;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.ui.UiCallback;
|
||||
import org.briarproject.api.Settings;
|
||||
import org.briarproject.api.settings.SettingsManager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@@ -167,7 +168,9 @@ class PluginManagerImpl implements PluginManager {
|
||||
public void run() {
|
||||
try {
|
||||
TransportId id = factory.getId();
|
||||
SimplexCallback callback = new SimplexCallback(id);
|
||||
String namespace = id.toString();
|
||||
|
||||
SimplexCallback callback = new SimplexCallback(id, namespace);
|
||||
SimplexPlugin plugin = factory.createPlugin(callback);
|
||||
if (plugin == null) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
@@ -230,7 +233,8 @@ class PluginManagerImpl implements PluginManager {
|
||||
public void run() {
|
||||
try {
|
||||
TransportId id = factory.getId();
|
||||
DuplexCallback callback = new DuplexCallback(id);
|
||||
String namespace = id.toString();
|
||||
DuplexCallback callback = new DuplexCallback(id, namespace);
|
||||
DuplexPlugin plugin = factory.createPlugin(callback);
|
||||
if (plugin == null) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
@@ -309,18 +313,22 @@ class PluginManagerImpl implements PluginManager {
|
||||
private abstract class PluginCallbackImpl implements PluginCallback {
|
||||
|
||||
protected final TransportId id;
|
||||
protected final String namespace;
|
||||
|
||||
protected PluginCallbackImpl(TransportId id) {
|
||||
protected PluginCallbackImpl(TransportId id, String namespace) {
|
||||
this.id = id;
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public TransportConfig getConfig() {
|
||||
public Settings getSettings() {
|
||||
|
||||
try {
|
||||
return db.getConfig(id);
|
||||
return db.getSettings(namespace);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return new TransportConfig();
|
||||
return new Settings();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public TransportProperties getLocalProperties() {
|
||||
@@ -343,12 +351,14 @@ class PluginManagerImpl implements PluginManager {
|
||||
}
|
||||
}
|
||||
|
||||
public void mergeConfig(TransportConfig c) {
|
||||
public void mergeSettings(Settings s) {
|
||||
|
||||
try {
|
||||
db.mergeConfig(id, c);
|
||||
db.mergeSettings(s, namespace);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void mergeLocalProperties(TransportProperties p) {
|
||||
@@ -386,8 +396,8 @@ class PluginManagerImpl implements PluginManager {
|
||||
private class SimplexCallback extends PluginCallbackImpl
|
||||
implements SimplexPluginCallback {
|
||||
|
||||
private SimplexCallback(TransportId id) {
|
||||
super(id);
|
||||
private SimplexCallback(TransportId id, String namespace) {
|
||||
super(id, namespace);
|
||||
}
|
||||
|
||||
public void readerCreated(TransportConnectionReader r) {
|
||||
@@ -402,8 +412,8 @@ class PluginManagerImpl implements PluginManager {
|
||||
private class DuplexCallback extends PluginCallbackImpl
|
||||
implements DuplexPluginCallback {
|
||||
|
||||
private DuplexCallback(TransportId id) {
|
||||
super(id);
|
||||
private DuplexCallback(TransportId id, String namespace) {
|
||||
super(id, namespace);
|
||||
}
|
||||
|
||||
public void incomingConnectionCreated(DuplexTransportConnection d) {
|
||||
@@ -415,4 +425,4 @@ class PluginManagerImpl implements PluginManager {
|
||||
connectionManager.manageOutgoingConnection(c, id, d);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,4 +76,4 @@ class LanTcpPlugin extends TcpPlugin {
|
||||
// Unrecognised prefix - may be compatible
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.briarproject.settings;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.briarproject.api.settings.SettingsManager;
|
||||
import org.briarproject.api.Settings;
|
||||
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
class SettingsManagerImpl implements SettingsManager {
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger("SettingsManagerImpl");
|
||||
|
||||
@Inject
|
||||
SettingsManagerImpl(DatabaseComponent db) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the settings object identified by the provided namespace
|
||||
* string
|
||||
*/
|
||||
@Override
|
||||
public Settings getSettings(String namespace) throws DbException {
|
||||
return db.getSettings(namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges (read syncs) the provided settings identified by the provided namespace
|
||||
* string
|
||||
*/
|
||||
@Override
|
||||
public void mergeSettings(Settings s, String namespace) throws DbException {
|
||||
db.mergeSettings(s, namespace);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
13
briar-core/src/org/briarproject/settings/SettingsModule.java
Normal file
13
briar-core/src/org/briarproject/settings/SettingsModule.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package org.briarproject.settings;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
import org.briarproject.api.settings.SettingsManager;
|
||||
|
||||
public class SettingsModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(SettingsManager.class).to(SettingsManagerImpl.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user