Skip to content
This repository was archived by the owner on Aug 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<groupId>com.spotify</groupId>
<artifactId>dns</artifactId>
<packaging>bundle</packaging>
<version>3.1.6-SNAPSHOT</version>
<version>3.2.0-SNAPSHOT</version>
<name>Spotify DNS wrapper library</name>
<description>A thin wrapper around dnsjava for some features related to SRV lookups.
</description>
<url>https://github.com/spotify/dns-java</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<guava.version>12.0</guava.version>
<guava.version>28.2-jre</guava.version>
</properties>

<scm>
Expand Down Expand Up @@ -47,27 +47,22 @@
<dependency>
<groupId>dnsjava</groupId>
<artifactId>dnsjava</artifactId>
<version>2.1.8</version>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -91,7 +86,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
<version>1.7.30</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -233,10 +228,19 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.8.1</version>
<configuration>
<release>9</release>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<!-- Open up module for reflective access from junit/mockito -->
<argLine>--add-opens com.spotify.dns/com.spotify.dns=ALL-UNNAMED</argLine>
</configuration>
</plugin>
<plugin>
Expand All @@ -260,7 +264,7 @@
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.1</version>
<version>4.2.1</version>
<extensions>true</extensions>
<configuration>
<instructions>
Expand Down Expand Up @@ -289,7 +293,7 @@
<plugin>
<groupId>com.github.siom79.japicmp</groupId>
<artifactId>japicmp-maven-plugin</artifactId>
<version>0.6.1</version>
<version>0.14.3</version>
<configuration>
<oldVersion>
<dependency>
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/spotify/dns/AbstractChangeNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.spotify.dns;

import static java.util.Objects.requireNonNull;

import com.google.common.collect.ImmutableSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,33 +28,31 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* A helper for implementing the {@link ChangeNotifier} interface.
*/
abstract class AbstractChangeNotifier<T> implements ChangeNotifier<T> {

private static final Logger log = LoggerFactory.getLogger(AbstractChangeNotifier.class);

private final AtomicReference<Listener<T>> listenerRef = new AtomicReference<Listener<T>>();
private final AtomicReference<Listener<T>> listenerRef = new AtomicReference<>();

private final AtomicBoolean listenerNotified = new AtomicBoolean(false);

private final ReentrantLock lock = new ReentrantLock();

@Override
public void setListener(final Listener<T> listener, final boolean fire) {
checkNotNull(listener, "listener");
requireNonNull(listener, "listener");

lock.lock();
try {
if (!listenerRef.compareAndSet(null, listener)) {
if (!listenerRef.compareAndSet(null, listener)) {
throw new IllegalStateException("Listener already set!");
}

if (fire) {
notifyListener(newChangeNotification(current(), Collections.<T>emptySet()), true);
notifyListener(newChangeNotification(current(), Set.of()), true);
}
} finally {
lock.unlock();
Expand Down Expand Up @@ -81,7 +81,7 @@ protected final void fireRecordsUpdated(ChangeNotification<T> changeNotification
private void notifyListener(ChangeNotification<T> changeNotification, boolean newListener) {
lock.lock();
try {
checkNotNull(changeNotification, "changeNotification");
requireNonNull(changeNotification, "changeNotification");

final Listener<T> listener = listenerRef.get();
if (listener != null) {
Expand All @@ -100,10 +100,10 @@ private void notifyListener(ChangeNotification<T> changeNotification, boolean ne
}

protected final ChangeNotification<T> newChangeNotification(Set<T> current, Set<T> previous) {
checkNotNull(current, "current");
checkNotNull(previous, "previous");
requireNonNull(current, "current");
requireNonNull(previous, "previous");

return new ChangeNotificationImpl<T>(current, previous);
return new ChangeNotificationImpl<>(current, previous);
}

private static class ChangeNotificationImpl<T> implements ChangeNotification<T> {
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/spotify/dns/AggregatingChangeNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AggregatingChangeNotifier<T> extends AbstractChangeNotifier<T> {

private final List<ChangeNotifier<T>> changeNotifiers;

private volatile Set<T> records = ChangeNotifiers.initialEmptyDataInstance();
private volatile Set<T> records;

/**
* Create a new aggregating {@link ChangeNotifier}.
Expand All @@ -41,12 +41,7 @@ class AggregatingChangeNotifier<T> extends AbstractChangeNotifier<T> {

// Set up forwarding of listeners
for (final ChangeNotifier<T> changeNotifier : this.changeNotifiers) {
changeNotifier.setListener(new Listener<T>() {
@Override
public void onChange(final ChangeNotification<T> ignored) {
checkChange();
}
}, false);
changeNotifier.setListener(ignored -> checkChange(), false);
}

records = aggregateSet();
Expand Down
24 changes: 6 additions & 18 deletions src/main/java/com/spotify/dns/CachingLookupFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

package com.spotify.dns;

import com.google.common.base.Preconditions;
import static java.util.Objects.requireNonNull;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.util.concurrent.UncheckedExecutionException;

import org.xbill.DNS.Lookup;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import org.xbill.DNS.Lookup;

/**
* Caches Lookup instances using a per-thread cache; this is so that different threads will never
Expand All @@ -35,27 +33,17 @@ class CachingLookupFactory implements LookupFactory {
private final ThreadLocal<Cache<String, Lookup>> cacheHolder;

CachingLookupFactory(LookupFactory delegate) {
this.delegate = Preconditions.checkNotNull(delegate, "delegate");
this.delegate = requireNonNull(delegate, "delegate");
cacheHolder =
new ThreadLocal<Cache<String, Lookup>>() {
@Override
protected Cache<String, Lookup> initialValue() {
return CacheBuilder.newBuilder().build();
}
};
ThreadLocal.withInitial(() -> CacheBuilder.newBuilder().build());
}

@Override
public Lookup forName(final String fqdn) {
try {
return cacheHolder.get().get(
fqdn,
new Callable<Lookup>() {
@Override
public Lookup call() {
return delegate.forName(fqdn);
}
}
() -> delegate.forName(fqdn)
);
} catch (ExecutionException e) {
throw new DnsException(e);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/spotify/dns/ChangeNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public interface ChangeNotifier<T> {
/**
* A listener which will be called when the set of records change
*/
@FunctionalInterface
interface Listener<T> {

/**
Expand Down
37 changes: 19 additions & 18 deletions src/main/java/com/spotify/dns/ChangeNotifiers.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@

package com.spotify.dns;

import com.google.common.base.Supplier;
import com.google.common.collect.Sets;
import static com.spotify.dns.ChangeNotifierFactory.RunnableChangeNotifier;
import static java.util.Objects.requireNonNull;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.spotify.dns.ChangeNotifierFactory.RunnableChangeNotifier;
import java.util.function.Supplier;

public final class ChangeNotifiers {

Expand All @@ -37,7 +35,7 @@ public final class ChangeNotifiers {
* This is needed to distinguishing the initial state of change notifiers from
* when they have gotten proper data.
*/
private static final Set INITIAL_EMPTY_DATA = Collections.unmodifiableSet(new HashSet());
private static final Set INITIAL_EMPTY_DATA = Collections.unmodifiableSet(new HashSet<>());

private ChangeNotifiers() {
}
Expand Down Expand Up @@ -77,7 +75,7 @@ public static <T> ChangeNotifier<T> aggregate(ChangeNotifier<T>... notifiers) {
}

public static <T> ChangeNotifier<T> aggregate(Iterable<ChangeNotifier<T>> notifiers) {
return new AggregatingChangeNotifier<T>(notifiers);
return new AggregatingChangeNotifier<>(notifiers);
}

/**
Expand All @@ -93,11 +91,11 @@ public static <T> ChangeNotifier<T> aggregate(Iterable<ChangeNotifier<T>> notifi
* @return A notifier with a static set of records
*/
public static <T> ChangeNotifier<T> staticRecords(T... records) {
return staticRecords(Sets.newHashSet(records));
return staticRecords(Set.of(records));
}

public static <T> ChangeNotifier<T> staticRecords(Set<T> records) {
return new StaticChangeNotifier<T>(records);
return new StaticChangeNotifier<>(records);
}

/**
Expand All @@ -115,20 +113,23 @@ public static <T> ChangeNotifier<T> staticRecords(Set<T> records) {
* @return A runnable notifier
*/
public static <T> RunnableChangeNotifier<T> direct(Supplier<Set<T>> recordsSupplier) {
return new DirectChangeNotifier<T>(recordsSupplier);
return new DirectChangeNotifier<>(recordsSupplier);
}

/**
* @deprecated Use {@link #direct(java.util.function.Supplier)}
*/
@Deprecated(since = "3.2.0")
public static <T> RunnableChangeNotifier<T> direct(com.google.common.base.Supplier<Set<T>> recordsSupplier) {
return new DirectChangeNotifier<>(recordsSupplier);
}

public static <T> RunnableChangeNotifier<T> direct(AtomicReference<Set<T>> recordsHolder) {
return new DirectChangeNotifier<T>(supplierFromRef(recordsHolder));
return new DirectChangeNotifier<>(supplierFromRef(recordsHolder));
}

private static <T> Supplier<Set<T>> supplierFromRef(final AtomicReference<Set<T>> ref) {
checkNotNull(ref, "ref");
return new Supplier<Set<T>>() {
@Override
public Set<T> get() {
return ref.get();
}
};
requireNonNull(ref, "ref");
return ref::get;
}
}
7 changes: 3 additions & 4 deletions src/main/java/com/spotify/dns/DirectChangeNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package com.spotify.dns;

import com.google.common.base.Supplier;
import static java.util.Objects.requireNonNull;

import java.util.Set;

import static com.google.common.base.Preconditions.checkNotNull;
import java.util.function.Supplier;

class DirectChangeNotifier<T> extends AbstractChangeNotifier<T>
implements ChangeNotifierFactory.RunnableChangeNotifier<T> {
Expand All @@ -31,7 +30,7 @@ class DirectChangeNotifier<T> extends AbstractChangeNotifier<T>
private volatile boolean run = true;

public DirectChangeNotifier(Supplier<Set<T>> recordsSupplier) {
this.recordsSupplier = checkNotNull(recordsSupplier, "recordsSupplier");
this.recordsSupplier = requireNonNull(recordsSupplier, "recordsSupplier");
}

@Override
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/spotify/dns/DnsException.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@

package com.spotify.dns;

import javax.annotation.Nullable;

/**
* RuntimeException thrown by the Spotify DNS library.
*/
public class DnsException extends RuntimeException {
public DnsException() {
}

public DnsException(@Nullable String message) {
public DnsException(String message) {
super(message);
}

public DnsException(@Nullable String message, @Nullable Throwable cause) {
public DnsException(String message, Throwable cause) {
super(message, cause);
}

public DnsException(@Nullable Throwable cause) {
public DnsException(Throwable cause) {
super(cause);
}
}
Loading