Browse Source

Merge pull request #1682 from yunspace/master

Dropwizard 0.7.1 Update and MongoDB project merge
Zane K 10 years ago
parent
commit
05b3582529
27 changed files with 258 additions and 461 deletions
  1. 0 1
      .travis.yml
  2. 0 25
      frameworks/Java/dropwizard-mongodb/benchmark_config.json
  3. 0 20
      frameworks/Java/dropwizard-mongodb/hello-world.yml
  4. 0 3
      frameworks/Java/dropwizard-mongodb/install.sh
  5. 0 84
      frameworks/Java/dropwizard-mongodb/pom.xml
  6. 0 8
      frameworks/Java/dropwizard-mongodb/setup.sh
  7. 0 11
      frameworks/Java/dropwizard-mongodb/source_code
  8. 0 27
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldConfiguration.java
  9. 0 47
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldService.java
  10. 0 40
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/core/World.java
  11. 0 21
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoHealthCheck.java
  12. 0 45
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/Helper.java
  13. 0 30
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/JsonResource.java
  14. 0 66
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/WorldResource.java
  15. 24 3
      frameworks/Java/dropwizard/benchmark_config.json
  16. 10 2
      frameworks/Java/dropwizard/hello-world.yml
  17. 8 2
      frameworks/Java/dropwizard/pom.xml
  18. 11 3
      frameworks/Java/dropwizard/source_code
  19. 37 6
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java
  20. 9 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java
  21. 34 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/MongoConfiguration.java
  22. 5 5
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/MongoManaged.java
  23. 4 1
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/Fortune.java
  24. 10 3
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/World.java
  25. 37 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoFortuneResource.java
  26. 65 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoWorldResource.java
  27. 4 8
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java

+ 0 - 1
.travis.yml

@@ -64,7 +64,6 @@ env:
     - "TESTDIR=Java/activeweb"
     - "TESTDIR=Java/activeweb"
     - "TESTDIR=Java/curacao"
     - "TESTDIR=Java/curacao"
     - "TESTDIR=Java/dropwizard"
     - "TESTDIR=Java/dropwizard"
-    - "TESTDIR=Java/dropwizard-mongodb"
     - "TESTDIR=Java/gemini"
     - "TESTDIR=Java/gemini"
     - "TESTDIR=Java/grizzly-bm"
     - "TESTDIR=Java/grizzly-bm"
     - "TESTDIR=Java/grizzly-jersey"
     - "TESTDIR=Java/grizzly-jersey"

+ 0 - 25
frameworks/Java/dropwizard-mongodb/benchmark_config.json

@@ -1,25 +0,0 @@
-{
-  "framework": "dropwizard-mongodb",
-  "tests": [{
-    "default": {
-      "setup_file": "setup",
-      "json_url": "/json",
-      "db_url": "/db",
-      "query_url": "/db?queries=",
-      "port": 9000,
-      "approach": "Realistic",
-      "classification": "Fullstack",
-      "database": "MongoDB",
-      "framework": "dropwizard",
-      "language": "Java",
-      "orm": "Full",
-      "platform": "Jetty",
-      "webserver": "Jetty",
-      "os": "Linux",
-      "database_os": "Linux",
-      "display_name": "dropwizard-mongodb",
-      "notes": "mongodb implementation of dropwizard example",
-      "versus": ""
-    }
-  }]
-}

+ 0 - 20
frameworks/Java/dropwizard-mongodb/hello-world.yml

@@ -1,20 +0,0 @@
-http:
-  port: 9000
-
-  requestLog:
-
-    # Settings for logging to stdout.
-    console:
-      # If true, log requests to stdout.
-      enabled: false
-
-logging:
-
-  # The default level of all loggers. Can be OFF, ERROR, WARN, INFO, DEBUG, TRACE, or ALL.
-  level: OFF
-
-  console:
-
-    # If true, write log statements to stdout.
-    enabled: false
-

+ 0 - 3
frameworks/Java/dropwizard-mongodb/install.sh

@@ -1,3 +0,0 @@
-#!/bin/bash
-
-fw_depends java7 maven

+ 0 - 84
frameworks/Java/dropwizard-mongodb/pom.xml

@@ -1,84 +0,0 @@
-<?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.danielt</groupId>
-    <artifactId>dropwizard-mongodb</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>org.mongojack</groupId>
-			<artifactId>mongojack</artifactId>
-			<version>2.0.0</version>
-		</dependency>
-
-    </dependencies>
-
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-compiler-plugin</artifactId>
-                <version>2.3.2</version>
-                <configuration>
-                    <source>1.7</source>
-                    <target>1.7</target>
-                </configuration>
-            </plugin>
-            <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>

+ 0 - 8
frameworks/Java/dropwizard-mongodb/setup.sh

@@ -1,8 +0,0 @@
-#!/bin/bash
-
-# load java environment variables
-source $IROOT/java7.installed
-
-mvn clean package
-
-java -jar target/dropwizard-mongodb-0.0.1-SNAPSHOT.jar server hello-world.yml &

+ 0 - 11
frameworks/Java/dropwizard-mongodb/source_code

@@ -1,11 +0,0 @@
-./dropwizard-mongodb/src/main/java/com/example/helloworld/
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoHealthCheck.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoManaged.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldService.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/WorldResource.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/JsonResource.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/core
-./dropwizard-mongodb/src/main/java/com/example/helloworld/core/World.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldConfiguration.java

+ 0 - 27
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldConfiguration.java

@@ -1,27 +0,0 @@
-
-package com.example.helloworld;
-
-import javax.validation.constraints.Max;
-import javax.validation.constraints.Min;
-
-import org.hibernate.validator.constraints.NotEmpty;
-
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.yammer.dropwizard.config.Configuration;
-
-public class HelloWorldConfiguration
-    extends Configuration
-{
-	@JsonProperty
-    @NotEmpty
-    public String mongohost = "localhost";
- 
-    @JsonProperty
-    @Min(1)
-    @Max(65535)
-    public int mongoport = 27017;
- 
-    @JsonProperty
-    @NotEmpty
-    public String mongodb = "hello_world";
-}

+ 0 - 47
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldService.java

@@ -1,47 +0,0 @@
-
-package com.example.helloworld;
-
-import java.net.UnknownHostException;
-
-import org.mongojack.JacksonDBCollection;
-
-import com.example.helloworld.core.World;
-import com.example.helloworld.db.MongoHealthCheck;
-import com.example.helloworld.db.MongoManaged;
-import com.example.helloworld.resources.JsonResource;
-import com.example.helloworld.resources.WorldResource;
-import com.mongodb.DB;
-import com.mongodb.Mongo;
-import com.yammer.dropwizard.Service;
-import com.yammer.dropwizard.config.Bootstrap;
-import com.yammer.dropwizard.config.Environment;
-
-public class HelloWorldService
-    extends Service<HelloWorldConfiguration>
-{
-
-  public static void main(String[] args) throws Exception
-  {
-    new HelloWorldService().run(args);
-  }
-
-  @Override
-  public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap)
-  {
-    bootstrap.setName("hello-world");
-  }
-
-  @Override
-  public void run(HelloWorldConfiguration config, Environment environment) throws UnknownHostException
-  {
-    Mongo mongo = new Mongo(config.mongohost, config.mongoport);
-    MongoManaged mongoManaged = new MongoManaged(mongo);
-    environment.manage(mongoManaged);
-    environment.addHealthCheck(new MongoHealthCheck(mongo));
-    DB db = mongo.getDB(config.mongodb);
-    JacksonDBCollection<World, String> worlds = JacksonDBCollection.wrap(db.getCollection("world"), World.class, String.class);
-    environment.addResource(new WorldResource(worlds));
-    environment.addResource(new JsonResource());
-  }
-
-}

+ 0 - 40
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/core/World.java

@@ -1,40 +0,0 @@
-
-package com.example.helloworld.core;
-
-import javax.persistence.*;
-
-import org.mongojack.Id;
-
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-
-@Entity
-@Table(name = "World")
-@JsonIgnoreProperties(ignoreUnknown = true)
-public class World
-{
-  @Id
-  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;
-  }
-}

+ 0 - 21
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoHealthCheck.java

@@ -1,21 +0,0 @@
-package com.example.helloworld.db;
-
-import com.mongodb.Mongo;
-import com.yammer.metrics.core.HealthCheck;
- 
-public class MongoHealthCheck extends HealthCheck {
- 
-    private Mongo mongo;
- 
-    public MongoHealthCheck(Mongo mongo) {
-        super("MongoDBHealthCheck");
-        this.mongo = mongo;
-    }
- 
-    @Override
-    protected Result check() throws Exception {
-        mongo.getDatabaseNames();
-        return Result.healthy();
-    }
- 
-}

+ 0 - 45
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/Helper.java

@@ -1,45 +0,0 @@
-package com.example.helloworld.resources;
-
-import java.util.concurrent.ThreadLocalRandom;
-
-import com.google.common.base.Optional;
-
-/**
- * Provides utility methods for the benchmark tests.
- * Taken from undertow-edge project.
- */
-final class Helper {
-  private Helper() {
-    throw new AssertionError();
-  }
-
-  /**
-   * Returns the value of the "queries" request parameter, which is an integer
-   * bound between 1 and 500 with a default value of 1.
-   *
-   * @param exchange the current HTTP exchange
-   * @return the value of the "queries" request parameter
-   */
-  static int getQueries(Optional<String> queries) {
-    String value = queries.orNull();
-    if (value == null) {
-      return 1;
-    }
-    try {
-      int parsedValue = Integer.parseInt(value);
-      return Math.min(500, Math.max(1, parsedValue));
-    } catch (NumberFormatException e) {
-      return 1;
-    }
-  }
-
-  /**
-   * Returns a random integer that is a suitable value for both the {@code id}
-   * and {@code randomNumber} properties of a world object.
-   *
-   * @return a random world number
-   */
-  static int randomWorld() {
-    return 1 + ThreadLocalRandom.current().nextInt(10000);
-  }
-}

+ 0 - 30
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/JsonResource.java

@@ -1,30 +0,0 @@
-
-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
-{
-  // Response message class (copied from 'servlet' test)
-  public final static class HelloMessage {
-    public final String message;
-
-    public HelloMessage(String m) { message = m; }
-  }
-
-  public JsonResource() { }
-
-  @GET
-  public HelloMessage sayHello()
-  {
-    return new HelloMessage("Hello, World!");
-  }
-}

+ 0 - 66
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -1,66 +0,0 @@
-package com.example.helloworld.resources;
-
-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 org.mongojack.DBCursor;
-import org.mongojack.JacksonDBCollection;
-
-import com.example.helloworld.core.World;
-import com.google.common.base.Optional;
-import com.google.common.primitives.Ints;
-import com.mongodb.BasicDBObject;
-import com.mongodb.DBObject;
-
-@Path("/db")
-@Produces(MediaType.APPLICATION_JSON)
-public class WorldResource
-{
-
-  private JacksonDBCollection<World, String> collection;
- 
-  public WorldResource(JacksonDBCollection<World, String> collection)
-  {
-    this.collection = collection;
-  }
-
-  @GET
-  public Object dbTest(@QueryParam("queries") Optional<String> queries)
-  {
-    if (!queries.isPresent()) 
-    {
-      DBObject query = new BasicDBObject();
-      query.put("_id", Helper.randomWorld());
-      DBCursor<World> dbCursor = collection.find(query);
-      return (dbCursor.hasNext()) ? dbCursor.next() : null;
-    }
-    Integer totalQueries = Ints.tryParse(queries.orNull());
-    if (totalQueries != null) 
-    {
-      if (totalQueries > 500) 
-      {
-        totalQueries = 500;
-      }
-      else if (totalQueries < 1) 
-      {
-        totalQueries = 1;
-      }
-    } 
-    else 
-    {
-      totalQueries = 1;
-    }
-    final World[] worlds = new World[totalQueries];
-    for (int i = 0; i < totalQueries; i++)
-    {
-      DBObject query = new BasicDBObject();
-      query.put("_id", Helper.randomWorld());
-      DBCursor<World> dbCursor = collection.find(query);
-      worlds[i] = (dbCursor.hasNext()) ? dbCursor.next() : null;
-    }
-    return worlds;
-  }
-}

+ 24 - 3
frameworks/Java/dropwizard/benchmark_config.json

@@ -9,7 +9,7 @@
       "fortune_url": "/fortunes",
       "fortune_url": "/fortunes",
       "update_url": "/db/update?queries=",
       "update_url": "/db/update?queries=",
       "plaintext_url": "/plaintext",
       "plaintext_url": "/plaintext",
-      "port": 9000,
+      "port": 9090,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Fullstack",
       "classification": "Fullstack",
       "database": "MySQL",
       "database": "MySQL",
@@ -21,8 +21,29 @@
       "os": "Linux",
       "os": "Linux",
       "database_os": "Linux",
       "database_os": "Linux",
       "display_name": "dropwizard",
       "display_name": "dropwizard",
-      "notes": "",
+      "notes": "dropwizard with mysql using hibernate",
+      "versus": ""
+    },
+    "mongodb": {
+      "setup_file": "setup",
+      "db_url": "/mongo/db",
+      "query_url": "/mongo/db?queries=",
+      "fortune_url": "/mongo/fortunes",
+      "update_url": "/mongo/db/update?queries=",
+      "port": 9090,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MongoDB",
+      "framework": "dropwizard",
+      "language": "Java",
+      "orm": "Full",
+      "platform": "Jetty",
+      "webserver": "Jetty",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "dropwizard-mongodb",
+      "notes": "dropwizard with mongodb using mongojack",
       "versus": ""
       "versus": ""
     }
     }
   }]
   }]
-}
+}

+ 10 - 2
frameworks/Java/dropwizard/hello-world.yml

@@ -3,7 +3,7 @@ server:
   applicationContextPath: /
   applicationContextPath: /
   connector:
   connector:
     type: http
     type: http
-    port: 9000
+    port: 9090
     useServerHeader: true
     useServerHeader: true
 
 
   requestLog:
   requestLog:
@@ -23,7 +23,7 @@ database:
   password: benchmarkdbpass
   password: benchmarkdbpass
 
 
   # the JDBC URL
   # the JDBC URL
-  url: jdbc:mysql://localhost:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true
+  url: jdbc:mysql://127.0.0.1:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true
 
 
   # any properties specific to your JDBC driver:
   # any properties specific to your JDBC driver:
   properties:
   properties:
@@ -40,3 +40,11 @@ database:
 
 
   # whether or not idle connections should be validated
   # whether or not idle connections should be validated
   checkConnectionWhileIdle: false
   checkConnectionWhileIdle: false
+
+  properties:
+    hibernate.dialect: org.hibernate.dialect.MySQLDialect
+
+mongo:
+  host: 127.0.0.1
+  port: 27017
+  db: hello_world

+ 8 - 2
frameworks/Java/dropwizard/pom.xml

@@ -9,8 +9,9 @@
     <properties>
     <properties>
         <jdk.version>1.7</jdk.version>
         <jdk.version>1.7</jdk.version>
 
 
-        <dropwizard.version>0.7.0</dropwizard.version>
-        <mysql-connector-java.version>5.1.30</mysql-connector-java.version>
+        <dropwizard.version>0.7.1</dropwizard.version>
+        <mysql-connector-java.version>5.1.35</mysql-connector-java.version>
+        <mongojack.version>2.1.0</mongojack.version>
     </properties>
     </properties>
 
 
     <dependencies>
     <dependencies>
@@ -34,6 +35,11 @@
             <artifactId>mysql-connector-java</artifactId>
             <artifactId>mysql-connector-java</artifactId>
             <version>${mysql-connector-java.version}</version>
             <version>${mysql-connector-java.version}</version>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>org.mongojack</groupId>
+            <artifactId>mongojack</artifactId>
+            <version>${mongojack.version}</version>
+        </dependency>
     </dependencies>
     </dependencies>
 
 
     <build>
     <build>

+ 11 - 3
frameworks/Java/dropwizard/source_code

@@ -1,12 +1,17 @@
 ./dropwizard/src/main/java/com/example/helloworld/
 ./dropwizard/src/main/java/com/example/helloworld/
 ./dropwizard/src/main/java/com/example/helloworld/config
 ./dropwizard/src/main/java/com/example/helloworld/config
-./dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration
+./dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java
+./dropwizard-mongodb/src/main/java/com/example/helloworld/config/MongoConfiguration.java
 ./dropwizard/src/main/java/com/example/helloworld/db
 ./dropwizard/src/main/java/com/example/helloworld/db
 ./dropwizard/src/main/java/com/example/helloworld/db/model
 ./dropwizard/src/main/java/com/example/helloworld/db/model
-./dropwizard/src/main/java/com/example/helloworld/db/model/Fortune
-./dropwizard/src/main/java/com/example/helloworld/db/model/World
+./dropwizard/src/main/java/com/example/helloworld/db/model/Fortune.java
+./dropwizard/src/main/java/com/example/helloworld/db/model/World.java
 ./dropwizard/src/main/java/com/example/helloworld/db/FortuneDAO.java
 ./dropwizard/src/main/java/com/example/helloworld/db/FortuneDAO.java
 ./dropwizard/src/main/java/com/example/helloworld/db/WorldDAO.java
 ./dropwizard/src/main/java/com/example/helloworld/db/WorldDAO.java
+./dropwizard/src/main/java/com/example/helloworld/db/mongo
+./dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoManaged.java
+./dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoWorldDAO.java
+./dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoFortuneDAO.java
 ./dropwizard/src/main/java/com/example/helloworld/resources
 ./dropwizard/src/main/java/com/example/helloworld/resources
 ./dropwizard/src/main/java/com/example/helloworld/resources/api
 ./dropwizard/src/main/java/com/example/helloworld/resources/api
 ./dropwizard/src/main/java/com/example/helloworld/resources/api/HelloMessage
 ./dropwizard/src/main/java/com/example/helloworld/resources/api/HelloMessage
@@ -16,5 +21,8 @@
 ./dropwizard/src/main/java/com/example/helloworld/resources/JsonResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/JsonResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/TextResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/TextResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java
+./dropwizard/src/main/java/com/example/helloworld/resources/MongoFortuneResource.java
+./dropwizard/src/main/java/com/example/helloworld/resources/MongoWorldResource.java
+./dropwizard/src/main/java/com/example/helloworld/resources/Helper.java
 ./dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java
 ./dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java
 ./dropwizard/src/main/resources/fortunes.mustache
 ./dropwizard/src/main/resources/fortunes.mustache

+ 37 - 6
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java

@@ -1,20 +1,27 @@
 package com.example.helloworld;
 package com.example.helloworld;
 
 
 import com.example.helloworld.config.HelloWorldConfiguration;
 import com.example.helloworld.config.HelloWorldConfiguration;
+import com.example.helloworld.config.MongoConfiguration;
 import com.example.helloworld.db.FortuneDAO;
 import com.example.helloworld.db.FortuneDAO;
+import com.example.helloworld.db.MongoManaged;
+import com.example.helloworld.db.WorldDAO;
 import com.example.helloworld.db.model.Fortune;
 import com.example.helloworld.db.model.Fortune;
 import com.example.helloworld.db.model.World;
 import com.example.helloworld.db.model.World;
-import com.example.helloworld.db.WorldDAO;
-import com.example.helloworld.resources.FortuneResource;
-import com.example.helloworld.resources.JsonResource;
-import com.example.helloworld.resources.TextResource;
-import com.example.helloworld.resources.WorldResource;
+import com.example.helloworld.resources.*;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.mongodb.DB;
+import com.mongodb.MongoClient;
 import io.dropwizard.Application;
 import io.dropwizard.Application;
 import io.dropwizard.db.DataSourceFactory;
 import io.dropwizard.db.DataSourceFactory;
 import io.dropwizard.hibernate.HibernateBundle;
 import io.dropwizard.hibernate.HibernateBundle;
+import io.dropwizard.jackson.Jackson;
 import io.dropwizard.setup.Bootstrap;
 import io.dropwizard.setup.Bootstrap;
 import io.dropwizard.setup.Environment;
 import io.dropwizard.setup.Environment;
 import io.dropwizard.views.ViewBundle;
 import io.dropwizard.views.ViewBundle;
+import org.mongojack.JacksonDBCollection;
+import org.mongojack.internal.MongoJackModule;
+
+import java.net.UnknownHostException;
 
 
 public class HelloWorldService extends Application<HelloWorldConfiguration> {
 public class HelloWorldService extends Application<HelloWorldConfiguration> {
 
 
@@ -36,10 +43,34 @@ public class HelloWorldService extends Application<HelloWorldConfiguration> {
     }
     }
 
 
     @Override
     @Override
-    public void run(HelloWorldConfiguration config, Environment environment) {
+    public void run(HelloWorldConfiguration config, Environment environment) throws UnknownHostException {
         environment.jersey().register(new JsonResource()); // Test type 1: JSON serialization
         environment.jersey().register(new JsonResource()); // Test type 1: JSON serialization
         environment.jersey().register(new WorldResource(new WorldDAO(hibernate.getSessionFactory()))); // Test types 2, 3 & 5: Single database query, Multiple database queries & Database updates
         environment.jersey().register(new WorldResource(new WorldDAO(hibernate.getSessionFactory()))); // Test types 2, 3 & 5: Single database query, Multiple database queries & Database updates
         environment.jersey().register(new FortuneResource(new FortuneDAO(hibernate.getSessionFactory()))); // Test type 4: Fortunes
         environment.jersey().register(new FortuneResource(new FortuneDAO(hibernate.getSessionFactory()))); // Test type 4: Fortunes
         environment.jersey().register(new TextResource()); // Test type 6: Plaintext
         environment.jersey().register(new TextResource()); // Test type 6: Plaintext
+
+        setUpMongo(config.getMongo(), environment);
+    }
+
+    private void setUpMongo(MongoConfiguration config, Environment environment) throws UnknownHostException {
+        final MongoClient mongoClient = new MongoClient(config.getHost(), config.getPort());
+        environment.lifecycle().manage(new MongoManaged(mongoClient));
+
+        final DB db = mongoClient.getDB(config.getDb());
+        final ObjectMapper mongoJackMapper = MongoJackModule.configure(Jackson.newObjectMapper());
+        final JacksonDBCollection<World, Long> worlds = JacksonDBCollection.wrap(
+                db.getCollection("World"),
+                World.class,
+                Long.class,
+                mongoJackMapper);
+
+        final JacksonDBCollection<Fortune, Long> fortunes = JacksonDBCollection.wrap(
+                db.getCollection("Fortune"),
+                Fortune.class,
+                Long.class,
+                mongoJackMapper);
+
+        environment.jersey().register(new MongoWorldResource(worlds));
+        environment.jersey().register(new MongoFortuneResource(fortunes));
     }
     }
 }
 }

+ 9 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java

@@ -14,7 +14,16 @@ public class HelloWorldConfiguration extends Configuration {
     @JsonProperty
     @JsonProperty
     private DataSourceFactory database = new DataSourceFactory();
     private DataSourceFactory database = new DataSourceFactory();
 
 
+    @Valid
+    @NotNull
+    @JsonProperty
+    private MongoConfiguration mongo = new MongoConfiguration();
+
     public DataSourceFactory getDatabaseConfiguration() {
     public DataSourceFactory getDatabaseConfiguration() {
         return database;
         return database;
     }
     }
+
+    public MongoConfiguration getMongo() {
+        return mongo;
+    }
 }
 }

+ 34 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/MongoConfiguration.java

@@ -0,0 +1,34 @@
+package com.example.helloworld.config;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.hibernate.validator.constraints.NotEmpty;
+
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+
+public class MongoConfiguration {
+    @JsonProperty
+    @NotEmpty
+    private String host;
+
+    @JsonProperty
+    @Min(1)
+    @Max(65535)
+    private int port;
+
+    @JsonProperty
+    @NotEmpty
+    private String db;
+
+    public String getHost() {
+        return host;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public String getDb() {
+        return db;
+    }
+}

+ 5 - 5
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoManaged.java → frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/MongoManaged.java

@@ -1,13 +1,13 @@
 package com.example.helloworld.db;
 package com.example.helloworld.db;
 
 
-import com.mongodb.Mongo;
-import com.yammer.dropwizard.lifecycle.Managed;
- 
+import com.mongodb.MongoClient;
+import io.dropwizard.lifecycle.Managed;
+
 public class MongoManaged implements Managed {
 public class MongoManaged implements Managed {
  
  
-    private Mongo mongo;
+    private MongoClient mongo;
  
  
-    public MongoManaged(Mongo mongo) {
+    public MongoManaged(MongoClient mongo) {
         this.mongo = mongo;
         this.mongo = mongo;
     }
     }
  
  

+ 4 - 1
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/Fortune.java

@@ -1,5 +1,7 @@
 package com.example.helloworld.db.model;
 package com.example.helloworld.db.model;
 
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+
 import javax.persistence.*;
 import javax.persistence.*;
 
 
 @Entity
 @Entity
@@ -7,9 +9,10 @@ import javax.persistence.*;
 public class Fortune implements Comparable<Fortune> {
 public class Fortune implements Comparable<Fortune> {
 
 
     @Id
     @Id
-    @GeneratedValue(strategy = GenerationType.AUTO)
+    @JsonProperty
     private long id;
     private long id;
 
 
+    @JsonProperty
     @Column(name = "message", nullable = false)
     @Column(name = "message", nullable = false)
     private String message;
     private String message;
 
 

+ 10 - 3
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/World.java

@@ -1,19 +1,26 @@
 package com.example.helloworld.db.model;
 package com.example.helloworld.db.model;
 
 
-import javax.persistence.*;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
 
 
 @Entity
 @Entity
 @Table(name = "World")
 @Table(name = "World")
 public class World {
 public class World {
 
 
     @Id
     @Id
-    @GeneratedValue(strategy = GenerationType.AUTO)
+    @JsonProperty
     private long id;
     private long id;
 
 
+    @JsonProperty
     @Column(name = "randomNumber", nullable = false)
     @Column(name = "randomNumber", nullable = false)
     private long randomNumber;
     private long randomNumber;
 
 
-    public World() {}
+    public World() {
+    }
 
 
     public long getId() {
     public long getId() {
         return id;
         return id;

+ 37 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoFortuneResource.java

@@ -0,0 +1,37 @@
+package com.example.helloworld.resources;
+
+import com.example.helloworld.db.model.Fortune;
+import com.example.helloworld.resources.views.FortuneView;
+import com.google.common.collect.Lists;
+import org.mongojack.DBProjection;
+import org.mongojack.DBQuery;
+import org.mongojack.JacksonDBCollection;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import java.util.Collections;
+import java.util.List;
+
+@Path("/mongo/fortunes")
+@Produces(MediaType.TEXT_HTML + ";charset=UTF-8")
+public class MongoFortuneResource {
+
+    private final JacksonDBCollection<Fortune, Long> fortuneCollection;
+
+    public MongoFortuneResource(JacksonDBCollection<Fortune, Long> fortuneCollection) {
+        this.fortuneCollection = fortuneCollection;
+    }
+
+    @GET
+    public FortuneView dbTest() {
+        final List<Fortune> fortunes = Lists.newArrayListWithExpectedSize(32);
+
+        fortunes.addAll(fortuneCollection.find(DBQuery.empty(), DBProjection.include("_id", "message")).toArray());
+        fortunes.add(new Fortune("Additional fortune added at request time."));
+
+        Collections.sort(fortunes);
+        return new FortuneView(fortunes);
+    }
+}

+ 65 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoWorldResource.java

@@ -0,0 +1,65 @@
+package com.example.helloworld.resources;
+
+import com.example.helloworld.db.model.World;
+import com.google.common.base.Optional;
+import org.mongojack.DBProjection;
+import org.mongojack.DBQuery;
+import org.mongojack.DBUpdate;
+import org.mongojack.JacksonDBCollection;
+
+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 static com.example.helloworld.resources.Helper.getQueries;
+
+@Path("/mongo/db")
+@Produces(MediaType.APPLICATION_JSON)
+public class MongoWorldResource {
+    private final JacksonDBCollection<World, Long> worldsCollection;
+
+    public MongoWorldResource(JacksonDBCollection<World, Long> worldsCollection) {
+        this.worldsCollection = worldsCollection;
+    }
+
+    @GET
+    public Object dbTest(@QueryParam("queries") Optional<String> queries) {
+        int totalQueries = getQueries(queries);
+        final World[] worlds = new World[totalQueries];
+
+        for (int i = 0; i < totalQueries; i++) {
+            worlds[i] = worldsCollection.findOneById(
+                    (long) Helper.randomWorld(),
+                    DBProjection.include("_id", "randomNumber")
+            );
+        }
+        if (!queries.isPresent()) {
+            return worlds[0];
+        } else {
+            return worlds;
+        }
+    }
+
+    @GET
+    @Path("/update")
+    public World[] updateTest(@QueryParam("queries") Optional<String> queries) {
+        int totalQueries = getQueries(queries);
+        final World[] worlds = new World[totalQueries];
+
+        for (int i = 0; i < totalQueries; i++) {
+            // use findAndModify() similar to nodejs-mongodb-raw
+            worlds[i] = worldsCollection.findAndModify(
+                    DBQuery.is("_id", (long) Helper.randomWorld()),
+                    DBProjection.include("_id", "randomNumber"),
+                    null,
+                    false,
+                    DBUpdate.set("randomNumber", (long) Helper.randomWorld()),
+                    true,
+                    false
+            );
+        }
+        return worlds;
+    }
+}

+ 4 - 8
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -1,5 +1,8 @@
 package com.example.helloworld.resources;
 package com.example.helloworld.resources;
 
 
+import com.example.helloworld.db.WorldDAO;
+import com.example.helloworld.db.model.World;
+import com.google.common.base.Optional;
 import io.dropwizard.hibernate.UnitOfWork;
 import io.dropwizard.hibernate.UnitOfWork;
 
 
 import javax.ws.rs.GET;
 import javax.ws.rs.GET;
@@ -8,10 +11,6 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MediaType;
 
 
-import com.example.helloworld.db.WorldDAO;
-import com.example.helloworld.db.model.World;
-import com.google.common.base.Optional;
-
 @Path("/db")
 @Path("/db")
 @Produces(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public class WorldResource {
 public class WorldResource {
@@ -24,10 +23,9 @@ public class WorldResource {
     @GET
     @GET
     @UnitOfWork
     @UnitOfWork
     public Object dbTest(@QueryParam("queries") Optional<String> queries) {
     public Object dbTest(@QueryParam("queries") Optional<String> queries) {
-    	int totalQueries = Helper.getQueries(queries);
+        int totalQueries = Helper.getQueries(queries);
         final World[] worlds = new World[totalQueries];
         final World[] worlds = new World[totalQueries];
 
 
-        // TODO: Is parallelising this cheating?
         for (int i = 0; i < totalQueries; i++) {
         for (int i = 0; i < totalQueries; i++) {
             final long worldId = Helper.randomWorld();
             final long worldId = Helper.randomWorld();
             worlds[i] = worldDAO.findById(worldId).orNull();
             worlds[i] = worldDAO.findById(worldId).orNull();
@@ -46,7 +44,6 @@ public class WorldResource {
         int totalQueries = Helper.getQueries(queries);
         int totalQueries = Helper.getQueries(queries);
         final World[] worlds = new World[totalQueries];
         final World[] worlds = new World[totalQueries];
 
 
-        // TODO: Is parallelising this cheating?
         for (int i = 0; i < totalQueries; i++) {
         for (int i = 0; i < totalQueries; i++) {
             final long worldId = Helper.randomWorld();
             final long worldId = Helper.randomWorld();
 
 
@@ -54,7 +51,6 @@ public class WorldResource {
             world.setRandomNumber(Helper.randomWorld());
             world.setRandomNumber(Helper.randomWorld());
             worlds[i] = worldDAO.update(world);
             worlds[i] = worldDAO.update(world);
         }
         }
-
         return worlds;
         return worlds;
     }
     }
 }
 }