Browse Source

Merge pull request #405 from zznate/vertx-update

Vertx update
Michael Hixson 12 years ago
parent
commit
49191e85f1
3 changed files with 32 additions and 32 deletions
  1. 2 2
      vertx/App.groovy
  2. 28 27
      vertx/WebServer.java
  3. 2 3
      vertx/setup.py

+ 2 - 2
vertx/App.groovy

@@ -3,7 +3,7 @@
 def persistorConf = [
   address: 'hello.persistor',
   db_name: 'hello_world',
-  host: 'localhost'
+  host: '127.0.0.1'
 ]
 
 def permitted =
@@ -24,7 +24,7 @@ def permitted =
 container.with {
 
   // Deploy the busmods
-  deployModule('vertx.mongo-persistor-v1.2.1', persistorConf, 8)
+  // deployModule('vertx.mongo-persistor-v1.2.1', persistorConf, 8)
 
   // Start the web server
 

+ 28 - 27
vertx/WebServer.java

@@ -1,18 +1,21 @@
 import java.io.IOException;
 import java.nio.charset.*;
 import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
 import java.util.Random;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import org.codehaus.jackson.map.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.vertx.java.core.Handler;
+import org.vertx.java.core.buffer.Buffer;
 import org.vertx.java.core.eventbus.Message;
 import org.vertx.java.core.http.HttpServerRequest;
 import org.vertx.java.core.json.JsonArray;
 import org.vertx.java.core.json.JsonObject;
-import org.vertx.java.deploy.Verticle;
+import org.vertx.java.platform.Verticle;
 
 public class WebServer
 	extends    Verticle
@@ -21,7 +24,7 @@ public class WebServer
   private final ObjectMapper mapper = new ObjectMapper();
 
   @Override
-  public void start() throws Exception
+  public void start()
   {
     this.getVertx().createHttpServer().requestHandler(this).listen(8080);
   }
@@ -29,45 +32,43 @@ public class WebServer
   @Override
   public void handle(HttpServerRequest req)
   {
-    if (req.path.equals("/json"))
+    if (req.path().equals("/json"))
     {
       handleJson(req);
     }
-    else if (req.path.equals("/db"))
+    else if (req.path().equals("/db"))
     {
       handleDb(req);
     }
     else
     {
-      req.response.statusCode = 404;
-      req.response.end();
+      req.response().setStatusCode(404);
+      req.response().end();
     }
   }
 
-  public static class HelloMessage
-  {
-    public final String message = "Hello, world";
-  }
 
   private void handleJson(HttpServerRequest req)
   {
-    String result;
+    Buffer buffer;
     try
     {
-      result = mapper.writeValueAsString(new HelloMessage());
+      Map<String, String> data = new HashMap<String, String>();
+      data.put("message", "Hello, world");
+      buffer = new Buffer(mapper.writeValueAsBytes(data));
     }
     catch (IOException e)
     {
-      req.response.statusCode = 500;
-      req.response.end();
+      req.response().setStatusCode(500);
+      req.response().end();
       return;
     }
     
-    int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
-    req.response.putHeader("Content-Type", "application/json; charset=UTF-8");
-    req.response.putHeader("Content-Length", contentLength);
-    req.response.write(result);
-    req.response.end();
+
+    req.response().putHeader("Content-Type", "application/json; charset=UTF-8");
+    req.response().putHeader("Content-Length", Integer.toString(buffer.length()));
+    req.response().write(buffer);
+    req.response().end();
   }
 
   private void handleDb(final HttpServerRequest req)
@@ -112,7 +113,7 @@ public class WebServer
     @Override
     public void handle(Message<JsonObject> reply)
     {
-      final JsonObject body = reply.body;
+      final JsonObject body = reply.body();
 
       if ("ok".equals(body.getString("status")))
       {
@@ -127,15 +128,15 @@ public class WebServer
         {
           final String result = mapper.writeValueAsString(worlds);
           final int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
-          this.req.response.putHeader("Content-Type", "application/json; charset=UTF-8");
-          this.req.response.putHeader("Content-Length", contentLength);
-          this.req.response.write(result);
-          this.req.response.end();
+          this.req.response().putHeader("Content-Type", "application/json; charset=UTF-8");
+          this.req.response().putHeader("Content-Length", Integer.toString(contentLength));
+          this.req.response().write(result);
+          this.req.response().end();
         }
         catch (IOException e)
         {
-          req.response.statusCode = 500;
-          req.response.end();
+          req.response().setStatusCode(500);
+          req.response().end();
         }
       }
     }

+ 2 - 3
vertx/setup.py

@@ -7,9 +7,8 @@ import os
 def start(args):
   setup_util.replace_text("vertx/App.groovy", "host: '.*'", "host: '" + args.database_host + "'")
 
-  try:
-    subprocess.check_call("javac WebServer.java -cp $VERTX_HOME/lib/vertx-core-1.3.1.final.jar:$VERTX_HOME/lib/vertx-platform-1.3.1.final.jar:$VERTX_HOME/lib/mustache.jar:$VERTX_HOME/lib/jackson-core-asl-1.9.4.jar:$VERTX_HOME/lib/jackson-mapper-asl-1.9.4.jar:$VERTX_HOME/lib/guava-11.0.2.jar", shell=True, cwd="vertx")
-    subprocess.Popen("vertx run App.groovy -repo vert-x.github.io", shell=True, cwd="vertx")
+  try:    
+    subprocess.Popen("vertx run WebServer.java", shell=True, cwd="vertx")
     return 0
   except subprocess.CalledProcessError:
     return 1