Skip to content

Commit

Permalink
initial checking of just dropwizard project; it is NOT an archetype y…
Browse files Browse the repository at this point in the history
…et; to be converted though :-)
  • Loading branch information
Alexey Prohorenko committed May 3, 2012
1 parent 2fbb103 commit cf25881
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
@@ -1,4 +1,14 @@
dropwizard-archetype
====================

Maven2 Archetype for Coda Hale's Dropwizard
Maven2 Archetype for Coda Hale's Dropwizard

$ mvn package

$ java -jar target/dropwizard-example-0.0.1-SNAPSHOT.jar server configs/hello-world.yml

$ open http://localhost:8080/hello-world http://localhost:8080/hello-world?name=Successful+Dropwizard+User

$ open http://localhost:8081/ http://localhost:8081/metrics http://localhost:8081/threads http://localhost:8081/healthcheck


2 changes: 2 additions & 0 deletions configs/hello-world.yml
@@ -0,0 +1,2 @@
template: Hello, %s!
defaultName: Stranger
72 changes: 72 additions & 0 deletions pom.xml
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.alexeypro.samples</groupId>
<artifactId>dropwizard-example</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>com.yammer.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.4.0</version>
</dependency>
</dependencies>

<properties>
<!-- use UTF-8 for everything -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<!-- compile for Java 1.6 -->
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.alexeypro.samples.services.HelloWorldService</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
23 changes: 23 additions & 0 deletions src/main/java/com/alexeypro/samples/HelloWorldConfiguration.java
@@ -0,0 +1,23 @@
package com.alexeypro.samples;

import com.yammer.dropwizard.config.Configuration;
import org.codehaus.jackson.annotate.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;

public class HelloWorldConfiguration extends Configuration {
@NotEmpty
@JsonProperty
private String template;

@NotEmpty
@JsonProperty
private String defaultName = "Stranger";

public String getTemplate() {
return template;
}

public String getDefaultName() {
return defaultName;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/alexeypro/samples/Saying.java
@@ -0,0 +1,19 @@
package com.alexeypro.samples;

public class Saying {
private final long id;
private final String content;

public Saying(long id, String content) {
this.id = id;
this.content = content;
}

public long getId() {
return id;
}

public String getContent() {
return content;
}
}
@@ -0,0 +1,21 @@
package com.alexeypro.samples.health;

import com.yammer.metrics.core.HealthCheck;

public class TemplateHealthCheck extends HealthCheck {
private final String template;

public TemplateHealthCheck(String template) {
super("template");
this.template = template;
}

@Override
protected Result check() throws Exception {
final String saying = String.format(template, "TEST");
if (!saying.contains("TEST")) {
return Result.unhealthy("template doesn't include a name");
}
return Result.healthy();
}
}
@@ -0,0 +1,33 @@
package com.alexeypro.samples.resources;

import com.alexeypro.samples.Saying;
import com.google.common.base.Optional;
import com.yammer.metrics.annotation.Timed;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.atomic.AtomicLong;

@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
private final String template;
private final String defaultName;
private final AtomicLong counter;

public HelloWorldResource(String template, String defaultName) {
this.template = template;
this.defaultName = defaultName;
this.counter = new AtomicLong();
}

@GET
@Timed
public Saying sayHello(@QueryParam("name") Optional<String> name) {
return new Saying(counter.incrementAndGet(),
String.format(template, name.or(defaultName)));
}
}
@@ -0,0 +1,27 @@
package com.alexeypro.samples.services;

import com.alexeypro.samples.HelloWorldConfiguration;
import com.alexeypro.samples.resources.HelloWorldResource;
import com.alexeypro.samples.health.TemplateHealthCheck;
import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.config.Environment;

public class HelloWorldService extends Service<HelloWorldConfiguration> {
public static void main(String[] args) throws Exception {
new HelloWorldService().run(args);
}

private HelloWorldService() {
super("hello-world");
}

@Override
protected void initialize(HelloWorldConfiguration configuration,
Environment environment) {
final String template = configuration.getTemplate();
final String defaultName = configuration.getDefaultName();
environment.addResource(new HelloWorldResource(template, defaultName));
environment.addHealthCheck(new TemplateHealthCheck(template));
}

}

0 comments on commit cf25881

Please sign in to comment.