Skip to content

Commit

Permalink
Merge pull request #61 from gruelbox/task-support
Browse files Browse the repository at this point in the history
Support Tasks
  • Loading branch information
badgerwithagun authored Mar 29, 2020
2 parents eafc5de + 0576b89 commit 2731c7c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 21 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ You now have access to some Guice idiomatic bind points in your `Module`s:
- Multibindings to `WebResource` provide resources.
- Multibindings to `HealthCheck` provide healthchecks.
- Multibindings to `ExceptionMapper` allow you to map exceptions from your endpoints to HTTP responses.
- Multibindings to `Task` to provide administration tasks.
- Multibindings to `Managed` provide controlled startup and shutdown of components.
- Multibindings to `Service` provide controlled startup and shutdown of background processes.
- Multibindings to `EnvironmentInitialiser` allow you to hook into the `Application.run` phase directly from anywhere in your code when you need to do anything else on startup, without having to add code to `Application.run` directly.
Expand All @@ -88,7 +89,7 @@ Add the dependency:
<dependency>
<groupId>com.gruelbox</groupId>
<artifactId>dropwizard-guice-box-hibernate</artifactId>
<version>0.0.2</version>
<version>1.0.0</version>
</dependency>
```

Expand Down Expand Up @@ -135,8 +136,4 @@ You now have access to both `SessionFactory` and `HibernateBundle` via injection

## Examples

See the [tests](https://github.com/gruelbox/dropwizard-guice-box/tree/master/dropwizard-guice-box/src/test/java/com/gruelbox/tools/dropwizard/guice/example/simple) for some basic examples.

## Credit

The POM and Travis build borrow heavily from other projects. See [oss-archetype](https://github.com/gruelbox/oss-archetype#credit) for credits.
See the [tests](https://github.com/gruelbox/dropwizard-guice-box/tree/master/dropwizard-guice-box/src/test/java/com/gruelbox/tools/dropwizard/guice/example/simple) for some basic examples.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@
*/
package com.gruelbox.tools.dropwizard.guice.resources;

import com.gruelbox.tools.dropwizard.guice.EnvironmentInitialiser;
import io.dropwizard.servlets.tasks.Task;
import io.dropwizard.setup.Environment;
import java.util.Set;

import javax.inject.Inject;
import javax.ws.rs.ext.ExceptionMapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Streams;
import com.gruelbox.tools.dropwizard.guice.EnvironmentInitialiser;

import io.dropwizard.setup.Environment;

class GuiceJerseyEnvironment implements EnvironmentInitialiser {

private static final Logger LOGGER = LoggerFactory.getLogger(GuiceJerseyEnvironment.class);
Expand All @@ -37,17 +33,27 @@ class GuiceJerseyEnvironment implements EnvironmentInitialiser {

@SuppressWarnings("rawtypes")
private final Set<ExceptionMapper> exceptionMappers;
private final Set<Task> tasks;

@Inject
GuiceJerseyEnvironment(Set<WebResource> webResources, Set<ExceptionMapper> exceptionMappers) {
GuiceJerseyEnvironment(Set<WebResource> webResources,
Set<ExceptionMapper> exceptionMappers,
Set<Task> tasks) {
this.webResources = webResources;
this.exceptionMappers = exceptionMappers;
this.tasks = tasks;
}

@Override
public void init(Environment environment) {
Streams.concat(webResources.stream(), exceptionMappers.stream())
.peek(t -> LOGGER.debug("Registering resource {}", t))
.forEach(environment.jersey()::register);
webResources.stream()
.peek(t -> LOGGER.debug("Registering resource {}", t))
.forEach(environment.jersey()::register);
exceptionMappers.stream()
.peek(t -> LOGGER.debug("Registering exception mapper {}", t))
.forEach(environment.jersey()::register);
tasks.stream()
.peek(t -> LOGGER.debug("Registering task {}", t))
.forEach(environment.admin()::addTask);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.gruelbox.tools.dropwizard.guice.resources;

import io.dropwizard.servlets.tasks.Task;
import javax.ws.rs.ext.ExceptionMapper;

import com.google.inject.AbstractModule;
Expand All @@ -27,6 +28,7 @@ public class GuiceJerseyModule extends AbstractModule {
protected void configure() {
Multibinder.newSetBinder(binder(), WebResource.class);
Multibinder.newSetBinder(binder(), ExceptionMapper.class);
Multibinder.newSetBinder(binder(), Task.class);
Multibinder.newSetBinder(binder(), EnvironmentInitialiser.class)
.addBinding().to(GuiceJerseyEnvironment.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.gruelbox.tools.dropwizard.guice.example.simple;

import io.dropwizard.servlets.tasks.Task;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.ws.rs.ext.ExceptionMapper;
Expand All @@ -35,6 +36,8 @@ protected void configure() {
.addBinding().to(ExampleResource.class);
Multibinder.newSetBinder(binder(), ExceptionMapper.class)
.addBinding().to(ExampleExceptionMapper.class);
Multibinder.newSetBinder(binder(), Task.class)
.addBinding().to(ExampleTask.class);
}

@SuppressWarnings("SameReturnValue")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.gruelbox.tools.dropwizard.guice.example.simple;

import io.dropwizard.servlets.tasks.Task;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;

class ExampleTask extends Task {

@Inject
ExampleTask() {
super("mytask");
}

@Override
public void execute(Map<String, List<String>> map, PrintWriter printWriter) {
printWriter.print("Foo");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
import static org.junit.Assert.assertEquals;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

Expand All @@ -30,6 +35,7 @@
import io.dropwizard.client.JerseyClientBuilder;
import io.dropwizard.testing.ResourceHelpers;
import io.dropwizard.testing.junit.DropwizardAppRule;
import org.junit.jupiter.api.BeforeAll;

public class TestExampleApplicationSimple {

Expand All @@ -40,10 +46,20 @@ public class TestExampleApplicationSimple {
ResourceHelpers.resourceFilePath("example-configuration.yml")
);

private static Client client;

@BeforeClass
public static void before() {
client = new JerseyClientBuilder(RULE.getEnvironment()).build("test client");
}

@AfterClass
public static void after() {
client.close();
}

@Test
public void testResource() {
Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test client");

assertEquals(
ExampleApplicationSimple.APP_NAME,
client.target(
Expand All @@ -68,8 +84,6 @@ public void testResource() {

@Test
public void testExceptionMapper() {
Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test client 2");

assertEquals(
Status.FORBIDDEN.getStatusCode(),
client.target(
Expand All @@ -79,4 +93,15 @@ public void testExceptionMapper() {
.getStatus()
);
}

@Test
public void testTask() {
assertEquals(
"Foo",
client.target(
String.format("http://localhost:%d/tasks/mytask", RULE.getAdminPort()))
.request()
.post(Entity.text(""), String.class)
);
}
}

0 comments on commit 2731c7c

Please sign in to comment.