Replace boilerplate with static method.

This commit is contained in:
akwizgran
2018-06-15 16:17:08 +01:00
parent 0d2a91289f
commit abe14f19e6
19 changed files with 91 additions and 136 deletions

View File

@@ -30,9 +30,9 @@ import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.inject.Inject;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static org.briarproject.bramble.util.ByteUtils.INT_32_BYTES;
import static org.briarproject.bramble.util.TimeUtils.logDuration;
import static org.briarproject.bramble.util.TimeUtils.now;
@NotNullByDefault
@@ -136,8 +136,7 @@ class CryptoComponentImpl implements CryptoComponent {
byte allZero = 0;
for (byte b : secret) allZero |= b;
if (allZero == 0) throw new GeneralSecurityException();
if (LOG.isLoggable(FINE))
LOG.fine("Deriving shared secret took " + (now() - start) + " ms");
logDuration(LOG, "Deriving shared secret", start);
return secret;
}

View File

@@ -9,8 +9,8 @@ import java.util.logging.Logger;
import javax.inject.Inject;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static org.briarproject.bramble.util.TimeUtils.logDuration;
import static org.briarproject.bramble.util.TimeUtils.now;
class ScryptKdf implements PasswordBasedKdf {
@@ -56,10 +56,7 @@ class ScryptKdf implements PasswordBasedKdf {
byte[] passwordBytes = StringUtils.toUtf8(password);
SecretKey k = new SecretKey(SCrypt.generate(passwordBytes, salt, cost,
BLOCK_SIZE, PARALLELIZATION, SecretKey.LENGTH));
if (LOG.isLoggable(FINE)) {
long duration = now() - start;
LOG.fine("Deriving key from password took " + duration + " ms");
}
logDuration(LOG, "Deriving key from password", start);
return k;
}
}

View File

@@ -16,7 +16,7 @@ import java.util.logging.Logger;
import javax.annotation.concurrent.Immutable;
import static java.util.logging.Level.FINE;
import static org.briarproject.bramble.util.TimeUtils.logDuration;
import static org.briarproject.bramble.util.TimeUtils.now;
/**
@@ -81,8 +81,7 @@ class Sec1KeyParser implements KeyParser {
// Construct a public key from the point (x, y) and the params
ECPublicKeyParameters k = new ECPublicKeyParameters(pub, params);
PublicKey p = new Sec1PublicKey(k);
if (LOG.isLoggable(FINE))
LOG.fine("Parsing public key took " + (now() - start) + " ms");
logDuration(LOG, "Parsing public key", start);
return p;
}
@@ -99,8 +98,7 @@ class Sec1KeyParser implements KeyParser {
// Construct a private key from the private value and the params
ECPrivateKeyParameters k = new ECPrivateKeyParameters(d, params);
PrivateKey p = new Sec1PrivateKey(k, keyBits);
if (LOG.isLoggable(FINE))
LOG.fine("Parsing private key took " + (now() - start) + " ms");
logDuration(LOG, "Parsing private key", start);
return p;
}
}

View File

@@ -68,13 +68,13 @@ import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.api.sync.ValidationManager.State.DELIVERED;
import static org.briarproject.bramble.api.sync.ValidationManager.State.UNKNOWN;
import static org.briarproject.bramble.db.DatabaseConstants.MAX_OFFERED_MESSAGES;
import static org.briarproject.bramble.util.TimeUtils.logDuration;
import static org.briarproject.bramble.util.TimeUtils.now;
@ThreadSafe
@@ -127,12 +127,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
if (lock.getReadHoldCount() > 0) throw new IllegalStateException();
if (lock.getWriteHoldCount() > 0) throw new IllegalStateException();
long start = now();
if (readOnly) lock.readLock().lock();
else lock.writeLock().lock();
if (LOG.isLoggable(FINE)) {
long duration = now() - start;
if (readOnly) LOG.fine("Waited " + duration + " ms for read lock");
else LOG.fine("Waited " + duration + " ms for write lock");
if (readOnly) {
lock.readLock().lock();
logDuration(LOG, "Waiting for read lock", start);
} else {
lock.writeLock().lock();
logDuration(LOG, "Waiting for write lock", start);
}
try {
return new Transaction(db.startTransaction(), readOnly);

View File

@@ -44,6 +44,7 @@ import static org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResul
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult.DB_ERROR;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult.SERVICE_ERROR;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult.SUCCESS;
import static org.briarproject.bramble.util.TimeUtils.logDuration;
import static org.briarproject.bramble.util.TimeUtils.now;
@ThreadSafe
@@ -109,18 +110,14 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
byte[] privateKey = keyPair.getPrivate().getEncoded();
LocalAuthor localAuthor = authorFactory
.createLocalAuthor(nickname, publicKey, privateKey);
if (LOG.isLoggable(FINE))
LOG.fine("Creating local author took " + (now() - start) + " ms");
logDuration(LOG, "Creating local author", start);
return localAuthor;
}
private void registerLocalAuthor(LocalAuthor author) throws DbException {
long start = now();
identityManager.registerLocalAuthor(author);
if (LOG.isLoggable(FINE)) {
LOG.fine("Registering local author took " + (now() - start)
+ " ms");
}
logDuration(LOG, "Registering local author", start);
}
@Override
@@ -134,12 +131,8 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
long start = now();
boolean reopened = db.open(this);
if (LOG.isLoggable(FINE)) {
long duration = now() - start;
if (reopened)
LOG.fine("Reopening database took " + duration + " ms");
else LOG.fine("Creating database took " + duration + " ms");
}
if (reopened) logDuration(LOG, "Reopening database", start);
else logDuration(LOG, "Creating database", start);
if (nickname != null) {
registerLocalAuthor(createLocalAuthor(nickname));
@@ -155,9 +148,8 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
start = now();
c.createLocalState(txn);
if (LOG.isLoggable(FINE)) {
LOG.fine("Starting client "
+ c.getClass().getSimpleName()
+ " took " + (now() - start) + " ms");
logDuration(LOG, "Starting client "
+ c.getClass().getSimpleName(), start);
}
}
db.commitTransaction(txn);
@@ -168,8 +160,8 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
start = now();
s.startService();
if (LOG.isLoggable(FINE)) {
LOG.fine("Starting service " + s.getClass().getSimpleName()
+ " took " + (now() - start) + " ms");
logDuration(LOG, "Starting service "
+ s.getClass().getSimpleName(), start);
}
}
@@ -216,8 +208,8 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
long start = now();
s.stopService();
if (LOG.isLoggable(FINE)) {
LOG.fine("Stopping service " + s.getClass().getSimpleName()
+ " took " + (now() - start) + " ms");
logDuration(LOG, "Stopping service "
+ s.getClass().getSimpleName(), start);
}
}
for (ExecutorService e : executors) {
@@ -229,8 +221,7 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
}
long start = now();
db.close();
if (LOG.isLoggable(FINE))
LOG.fine("Closing database took " + (now() - start) + " ms");
logDuration(LOG, "Closing database", start);
shutdownLatch.countDown();
} catch (DbException | ServiceException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);

View File

@@ -52,6 +52,7 @@ import javax.inject.Inject;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.TimeUtils.logDuration;
import static org.briarproject.bramble.util.TimeUtils.now;
@ThreadSafe
@@ -210,8 +211,8 @@ class PluginManagerImpl implements PluginManager, Service {
long start = now();
plugin.start();
if (LOG.isLoggable(FINE)) {
LOG.fine("Starting plugin " + plugin.getId()
+ " took " + (now() - start) + " ms");
logDuration(LOG, "Starting plugin " + plugin.getId(),
start);
}
} catch (PluginException e) {
if (LOG.isLoggable(WARNING)) {
@@ -247,8 +248,8 @@ class PluginManagerImpl implements PluginManager, Service {
long start = now();
plugin.stop();
if (LOG.isLoggable(FINE)) {
LOG.fine("Stopping plugin " + plugin.getId()
+ " took " + (now() - start) + " ms");
logDuration(LOG, "Stopping plugin " + plugin.getId(),
start);
}
} catch (InterruptedException e) {
LOG.warning("Interrupted while waiting for plugin to stop");