Run hooks when messages are validated.

This commit is contained in:
akwizgran
2016-02-05 12:08:48 +00:00
parent d75c51ec74
commit 741571bdb8
3 changed files with 34 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
package org.briarproject.api.sync;
import org.briarproject.api.db.Metadata;
/**
* Responsible for managing message validators and passing them messages to
* validate.
@@ -28,4 +30,11 @@ public interface ValidationManager {
/** Sets the message validator for the given client. */
void registerMessageValidator(ClientId c, MessageValidator v);
/** Registers a hook to be called whenever a message is validated. */
void registerValidationHook(ValidationHook hook);
interface ValidationHook {
void validatingMessage(Message m, ClientId c, Metadata meta);
}
}

View File

@@ -22,8 +22,10 @@ import org.briarproject.api.sync.MessageValidator;
import org.briarproject.api.sync.ValidationManager;
import org.briarproject.util.ByteUtils;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
@@ -40,6 +42,7 @@ class ValidationManagerImpl implements ValidationManager, Service,
private final Executor dbExecutor;
private final Executor cryptoExecutor;
private final Map<ClientId, MessageValidator> validators;
private final List<ValidationHook> hooks;
@Inject
ValidationManagerImpl(DatabaseComponent db,
@@ -49,6 +52,7 @@ class ValidationManagerImpl implements ValidationManager, Service,
this.dbExecutor = dbExecutor;
this.cryptoExecutor = cryptoExecutor;
validators = new ConcurrentHashMap<ClientId, MessageValidator>();
hooks = new CopyOnWriteArrayList<ValidationHook>();
}
@Override
@@ -67,6 +71,11 @@ class ValidationManagerImpl implements ValidationManager, Service,
validators.put(c, v);
}
@Override
public void registerValidationHook(ValidationHook hook) {
hooks.add(hook);
}
private void getMessagesToValidate(final ClientId c) {
dbExecutor.execute(new Runnable() {
public void run() {
@@ -119,6 +128,8 @@ class ValidationManagerImpl implements ValidationManager, Service,
if (meta == null) {
db.setMessageValid(m, c, false);
} else {
for (ValidationHook hook : hooks)
hook.validatingMessage(m, c, meta);
db.mergeMessageMetadata(m.getId(), meta);
db.setMessageValid(m, c, true);
}

View File

@@ -0,0 +1,14 @@
package org.briarproject.sync;
import org.briarproject.BriarTestCase;
import org.junit.Test;
import static org.junit.Assert.fail;
public class ValidationManagerImplTest extends BriarTestCase {
@Test
public void testUnitTestsExist() {
fail(); // FIXME: Write tests
}
}