Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #120 from zalando-incubator/reduce-dependencies
Browse files Browse the repository at this point in the history
Remove problem and guava dependencies
  • Loading branch information
jhorstmann authored Dec 6, 2016
2 parents 118c519 + 518b167 commit fff993f
Show file tree
Hide file tree
Showing 22 changed files with 251 additions and 176 deletions.
22 changes: 2 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,6 @@
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
Expand All @@ -325,13 +320,7 @@
<version>1.0.1</version>
</dependency>

<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem</artifactId>
<version>0.7.0</version>
</dependency>

<!-- Jackson -->
<!-- Jackson and required modules -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand All @@ -347,8 +336,6 @@
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>

<!-- problem handling and dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
Expand All @@ -364,11 +351,6 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>2.7.4</version>
</dependency>

<!-- testing -->
<dependency>
Expand All @@ -386,7 +368,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.0.86-beta</version>
<version>2.2.28</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/zalando/fahrschein/CursorManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.zalando.fahrschein;

import com.google.common.collect.Ordering;
import org.zalando.fahrschein.domain.Cursor;
import org.zalando.fahrschein.domain.Partition;
import org.zalando.fahrschein.domain.Subscription;
Expand All @@ -16,8 +15,6 @@
*/
public interface CursorManager {

Ordering<String> OFFSET_ORDERING = Ordering.natural().nullsFirst().onResultOf((String offset) -> "BEGIN".equals(offset) ? null : Long.parseLong(offset));

void onSuccess(String eventName, Cursor cursor) throws IOException;

void onError(String eventName, Cursor cursor, Throwable throwable) throws IOException;
Expand Down Expand Up @@ -59,7 +56,7 @@ default void updatePartitions(String eventName, List<Partition> partitions) thro

for (Partition partition : partitions) {
final Cursor cursor = cursorsByPartition.get(partition.getPartition());
if (cursor == null || (!"BEGIN".equals(cursor.getOffset()) && OFFSET_ORDERING.compare(cursor.getOffset(), partition.getOldestAvailableOffset()) < 0)) {
if (cursor == null || (!"BEGIN".equals(cursor.getOffset()) && OffsetComparator.INSTANCE.compare(cursor.getOffset(), partition.getOldestAvailableOffset()) < 0)) {
onSuccess(eventName, new Cursor(partition.getPartition(), "BEGIN"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import java.io.IOException;

import static com.google.common.base.Preconditions.checkState;
import static org.zalando.fahrschein.Preconditions.checkState;


public class ExponentialBackoffStrategy implements BackoffStrategy {
private static final Logger LOG = LoggerFactory.getLogger(ExponentialBackoffStrategy.class);
Expand Down
55 changes: 13 additions & 42 deletions src/main/java/org/zalando/fahrschein/IOProblem.java
Original file line number Diff line number Diff line change
@@ -1,90 +1,61 @@
package org.zalando.fahrschein;

import org.zalando.problem.Problem;

import org.springframework.http.HttpStatus;

import javax.annotation.Nullable;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.net.URI;
import java.util.Optional;

@SuppressWarnings("serial")
public class IOProblem extends IOException implements Problem {
static final class Status implements Response.StatusType {
private final int statusCode;
private final String reasonPhrase;

Status(final int statusCode, final String reasonPhrase) {
this.statusCode = statusCode;
this.reasonPhrase = reasonPhrase;
}

@Override
public int getStatusCode() {
return statusCode;
}

@Override
public Response.Status.Family getFamily() {
return Response.Status.Family.familyOf(statusCode);
}

@Override
public String getReasonPhrase() {
return reasonPhrase;
}
}
public class IOProblem extends IOException {

private final URI type;
private final String title;
private final Response.StatusType status;
private final int statusCode;
@Nullable
private final String detail;
@Nullable
private final URI instance;

public IOProblem(final URI type, final String title, final Response.StatusType status, @Nullable final String detail, @Nullable final URI instance) {
super(formatMessage(type, title, status.getStatusCode(), detail));
public IOProblem(final URI type, final String title, final int statusCode, @Nullable final String detail, @Nullable final URI instance) {
super(formatMessage(type, title, statusCode, detail));
this.type = type;
this.title = title;
this.status = status;
this.statusCode = statusCode;
this.detail = detail;
this.instance = instance;
}

public IOProblem(final URI type, final String title, final Response.StatusType status, @Nullable final String detail) {
this(type, title, status, detail, null);
public IOProblem(final URI type, final String title, final int statusCode, @Nullable final String detail) {
this(type, title, statusCode, detail, null);
}

public IOProblem(final URI type, final String title, final Response.StatusType status) {
this(type, title, status, null, null);
public IOProblem(final URI type, final String title, final int statusCode) {
this(type, title, statusCode, null, null);
}

private static String formatMessage(final URI type, final String title, final int status, @Nullable final String detail) {
return String.format("Problem [%s] with status [%d]: [%s] [%s]", type, status, title, detail == null ? "" : detail);
}

@Override
public URI getType() {
return type;
}

@Override
public String getTitle() {
return title;
}

@Override
public Response.StatusType getStatus() {
return status;
public int getStatusCode() {
return statusCode;
}

@Override
public Optional<String> getDetail() {
return Optional.ofNullable(detail);
}

@Override
public Optional<URI> getInstance() {
return Optional.ofNullable(instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Iterables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
Expand All @@ -24,6 +23,7 @@
import java.util.Map;

import static java.util.Collections.singletonList;
import static org.zalando.fahrschein.Preconditions.checkState;

public class ManagedCursorManager implements CursorManager {

Expand Down Expand Up @@ -83,7 +83,8 @@ public ManagedCursorManager(URI baseUri, ClientHttpRequestFactory clientHttpRequ

@Override
public void addSubscription(Subscription subscription) {
final String eventName = Iterables.getOnlyElement(subscription.getEventTypes());
checkState(subscription.getEventTypes().size() == 1);
final String eventName = subscription.getEventTypes().iterator().next();

LOG.debug("Adding subscription [{}] to event [{}]", subscription.getId(), eventName);

Expand All @@ -92,7 +93,8 @@ public void addSubscription(Subscription subscription) {

@Override
public void addStreamId(Subscription subscription, String streamId) {
final String eventName = Iterables.getOnlyElement(subscription.getEventTypes());
checkState(subscription.getEventTypes().size() == 1);
final String eventName = subscription.getEventTypes().iterator().next();

LOG.debug("Adding stream id [{}] for subscription [{}] to event [{}]", streamId, subscription.getId(), eventName);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/zalando/fahrschein/NakadiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Collections;
import java.util.List;

import static com.google.common.base.Preconditions.checkState;
import static org.zalando.fahrschein.Preconditions.checkState;

public class NakadiClient {
private static final Logger LOG = LoggerFactory.getLogger(NakadiClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import javax.annotation.Nullable;
import java.net.URI;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Optional.ofNullable;
import static org.zalando.fahrschein.Preconditions.checkNotNull;

public final class NakadiClientBuilder {
public static final int DEFAULT_CONNECT_TIMEOUT = 500;
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/org/zalando/fahrschein/NakadiReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Iterables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -38,10 +36,10 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.zalando.fahrschein.Preconditions.checkState;

class NakadiReader<T> implements IORunnable {

Expand All @@ -67,7 +65,7 @@ class NakadiReader<T> implements IORunnable {
private final MetricsCollector metricsCollector;

NakadiReader(URI uri, ClientHttpRequestFactory clientHttpRequestFactory, BackoffStrategy backoffStrategy, CursorManager cursorManager, ObjectMapper objectMapper, String eventName, Optional<Subscription> subscription, Optional<Lock> lock, Class<T> eventClass, Listener<T> listener, final MetricsCollector metricsCollector) {
checkState(!subscription.isPresent() || eventName.equals(Iterables.getOnlyElement(subscription.get().getEventTypes())), "Only subscriptions to single event types are currently supported");
checkState(!subscription.isPresent() || (subscription.get().getEventTypes().size() == 1 && eventName.equals(subscription.get().getEventTypes().iterator().next())), "Only subscriptions to single event types are currently supported");

this.uri = uri;
this.clientHttpRequestFactory = clientHttpRequestFactory;
Expand All @@ -83,10 +81,6 @@ class NakadiReader<T> implements IORunnable {
this.jsonFactory = objectMapper.getFactory();
this.eventReader = objectMapper.reader().forType(eventClass);
this.cursorHeaderWriter = DefaultObjectMapper.INSTANCE.writerFor(COLLECTION_OF_CURSORS);

if (clientHttpRequestFactory instanceof HttpComponentsClientHttpRequestFactory) {
LOG.warn("Using [{}] might block during reconnection, please consider using another implementation of ClientHttpRequestFactory", clientHttpRequestFactory.getClass().getName());
}
}

static class JsonInput implements Closeable {
Expand Down Expand Up @@ -258,7 +252,9 @@ public void run() throws IOException {
}
}

@VisibleForTesting
/*
* @VisibleForTesting
*/
void runInternal() throws IOException, BackoffException {
LOG.info("Starting to listen for events for [{}]", eventName);

Expand Down Expand Up @@ -307,7 +303,9 @@ void runInternal() throws IOException, BackoffException {
}
}

@VisibleForTesting
/*
* @VisibleForTesting
*/
void readSingleBatch() throws IOException {
try (final JsonInput jsonInput = openJsonInput()) {
final JsonParser jsonParser = jsonInput.getJsonParser();
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/zalando/fahrschein/OffsetComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.zalando.fahrschein;

import java.util.Comparator;

class OffsetComparator implements Comparator<String> {
static final Comparator<String> INSTANCE = new OffsetComparator();

private OffsetComparator() {

}

@Override
public int compare(String o1, String o2) {
if ("BEGIN".equals(o1)) {
return "BEGIN".equals(o2) ? 0 : -1;
} else {
int l1 = o1.length();
int l2 = o2.length();
if (l1 < l2) {
return -1;
} else if (l1 > l2) {
return 1;
} else {
return o1.compareTo(o2);
}
}
}
}
Loading

0 comments on commit fff993f

Please sign in to comment.