Browse Source

Implemented the /db and /json resources.

Still not compatible with the test runner suite so that will be my next task.
Mike Megally 12 years ago
parent
commit
32d23b550c

+ 40 - 0
dropwizard/hello-world.yml

@@ -0,0 +1,40 @@
+template: Hello, %s!
+defaultName: Stranger
+
+database:
+  # the name of your JDBC driver
+  driverClass: com.mysql.jdbc.Driver
+
+  # the username
+  user: root
+
+  # the password
+  password: password
+
+  # the JDBC URL
+  url: jdbc:mysql://localhost/hello_world
+
+  # any properties specific to your JDBC driver:
+  properties:
+    charSet: UTF-8
+
+  # the maximum amount of time to wait on an empty pool before throwing an exception
+  maxWaitForConnection: 1s
+
+  # the SQL query to run when validating a connection's liveness
+  validationQuery: "/* MyService Health Check */ SELECT 1"
+
+  # the minimum number of connections to keep open
+  minSize: 8
+
+  # the maximum number of connections to keep open
+  maxSize: 32
+
+  # whether or not idle connections should be validated
+  checkConnectionWhileIdle: false
+
+  # how long a connection must be held before it can be validated
+  checkConnectionHealthWhenIdleFor: 10s
+
+  # the maximum lifetime of an idle connection
+  closeConnectionIfIdleFor: 1 minute

+ 79 - 0
dropwizard/pom.xml

@@ -0,0 +1,79 @@
+<?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.xekm</groupId>
+    <artifactId>hello-world</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.yammer.dropwizard</groupId>
+            <artifactId>dropwizard-core</artifactId>
+            <version>0.6.2</version>
+        </dependency>
+        <dependency>
+            <groupId>com.yammer.dropwizard</groupId>
+            <artifactId>dropwizard-hibernate</artifactId>
+            <version>0.6.2</version>
+        </dependency>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>5.1.6</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <version>2.3.2</version>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
+                        </manifest>
+                    </archive>
+                </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.example.helloworld.HelloWorldService</mainClass>
+                                </transformer>
+                            </transformers>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 43 - 0
dropwizard/src/main/java/com/example/helloworld/HelloWorldConfiguration.java

@@ -0,0 +1,43 @@
+
+package com.example.helloworld;
+
+import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
+
+import org.hibernate.validator.constraints.NotEmpty;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.yammer.dropwizard.config.Configuration;
+import com.yammer.dropwizard.db.DatabaseConfiguration;
+
+public class HelloWorldConfiguration
+    extends Configuration
+{
+  @NotEmpty
+  @JsonProperty
+  private String                template;
+
+  @Valid
+  @NotNull
+  @JsonProperty
+  private DatabaseConfiguration database    = new DatabaseConfiguration();
+
+  @NotEmpty
+  @JsonProperty
+  private String                defaultName = "Stranger";
+
+  public String getTemplate()
+  {
+    return template;
+  }
+
+  public String getDefaultName()
+  {
+    return defaultName;
+  }
+
+  public DatabaseConfiguration getDatabaseConfiguration()
+  {
+    return database;
+  }
+}

+ 48 - 0
dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java

@@ -0,0 +1,48 @@
+
+package com.example.helloworld;
+
+import com.example.helloworld.core.World;
+import com.example.helloworld.db.WorldDAO;
+import com.example.helloworld.resources.JsonResource;
+import com.example.helloworld.resources.WorldResource;
+import com.yammer.dropwizard.Service;
+import com.yammer.dropwizard.config.Bootstrap;
+import com.yammer.dropwizard.config.Environment;
+import com.yammer.dropwizard.db.DatabaseConfiguration;
+import com.yammer.dropwizard.hibernate.HibernateBundle;
+
+public class HelloWorldService
+    extends Service<HelloWorldConfiguration>
+{
+  private final HibernateBundle<HelloWorldConfiguration> hibernate = new HibernateBundle<HelloWorldConfiguration>(
+                                                                       World.class)
+                                                                   {
+                                                                     @Override
+                                                                     public DatabaseConfiguration getDatabaseConfiguration(
+                                                                         HelloWorldConfiguration configuration)
+                                                                     {
+                                                                       return configuration.getDatabaseConfiguration();
+                                                                     }
+                                                                   };
+
+  public static void main(String[] args) throws Exception
+  {
+    new HelloWorldService().run(args);
+  }
+
+  @Override
+  public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap)
+  {
+    bootstrap.setName("hello-world");
+    bootstrap.addBundle(hibernate);
+  }
+
+  @Override
+  public void run(HelloWorldConfiguration config, Environment environment)
+  {
+    final WorldDAO dao = new WorldDAO(hibernate.getSessionFactory());
+    environment.addResource(new WorldResource(dao));
+    environment.addResource(new JsonResource());
+  }
+
+}

+ 36 - 0
dropwizard/src/main/java/com/example/helloworld/core/World.java

@@ -0,0 +1,36 @@
+
+package com.example.helloworld.core;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name = "World")
+public class World
+{
+  @Id
+  @GeneratedValue(strategy = GenerationType.AUTO)
+  private long id;
+
+  @Column(name = "randomNumber", nullable = false)
+  private long randomNumber;
+
+  public long getId()
+  {
+    return id;
+  }
+
+  public void setId(long id)
+  {
+    this.id = id;
+  }
+
+  public long getRandomNumber()
+  {
+    return this.randomNumber;
+  }
+
+  public void setRandomNumber(long randomNumber)
+  {
+    this.randomNumber = randomNumber;
+  }
+}

+ 28 - 0
dropwizard/src/main/java/com/example/helloworld/db/WorldDAO.java

@@ -0,0 +1,28 @@
+
+package com.example.helloworld.db;
+
+import org.hibernate.SessionFactory;
+
+import com.example.helloworld.core.World;
+import com.google.common.base.Optional;
+import com.yammer.dropwizard.hibernate.AbstractDAO;
+
+public class WorldDAO
+    extends AbstractDAO<World>
+{
+  public WorldDAO(SessionFactory factory)
+  {
+    super(factory);
+  }
+
+  public Optional<World> findById(Long id)
+  {
+    return Optional.fromNullable(get(id));
+  }
+
+  /*
+   * public World create(World world) { return persist(world); } public
+   * List<World> findAll() { return
+   * list(namedQuery("com.example.helloworld.core.Person.findAll")); }
+   */
+}

+ 27 - 0
dropwizard/src/main/java/com/example/helloworld/health/TemplateHealthCheck.java

@@ -0,0 +1,27 @@
+
+package com.example.helloworld.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();
+  }
+}

+ 28 - 0
dropwizard/src/main/java/com/example/helloworld/resources/JsonResource.java

@@ -0,0 +1,28 @@
+
+package com.example.helloworld.resources;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+@Path("/json")
+@Produces(MediaType.APPLICATION_JSON)
+public class JsonResource
+{
+  private final Map<String, String> MESSAGE = new HashMap<String, String>();
+
+  public JsonResource()
+  {
+    MESSAGE.put("message", "Hello, world!");
+  }
+
+  @GET
+  public Map<String, String> sayHello()
+  {
+    return MESSAGE;
+  }
+}

+ 42 - 0
dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -0,0 +1,42 @@
+
+package com.example.helloworld.resources;
+
+import java.util.Random;
+
+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 com.example.helloworld.core.World;
+import com.example.helloworld.db.WorldDAO;
+import com.google.common.base.Optional;
+import com.yammer.dropwizard.hibernate.UnitOfWork;
+
+@Path("/db")
+@Produces(MediaType.APPLICATION_JSON)
+public class WorldResource
+{
+  private WorldDAO worldDAO = null;
+
+  public WorldResource(WorldDAO worldDAO)
+  {
+    this.worldDAO = worldDAO;
+  }
+
+  @GET
+  @UnitOfWork
+  public World[] dbTest(@QueryParam("queries") Optional<Integer> queries)
+  {
+    final int totalQueries = queries.or(1);
+    final World[] worlds = new World[queries.or(1)];
+    final Random random = new Random(System.currentTimeMillis());
+
+    for (int i = 0; i < totalQueries; i++)
+    {
+      worlds[i] = this.worldDAO.findById((long)random.nextInt(10000)).orNull();
+    }
+    return worlds;
+  }
+}